Rails 4多线程和connection_pool问题

时间:2015-05-21 14:03:11

标签: ruby-on-rails multithreading ruby-on-rails-4 activerecord connection-pooling

我正在尝试多线程我的rails应用程序但是遇到了一些与connection_pool有关的问题。我启动一个线程并执行一个数据库查询,但它似乎永远不会关闭在线程中创建的数据库连接。这是我的代码:

class A
def self.foo
    Thread.new do
      nc1 = ActiveRecord::Base.connection_pool.connections.size
      nw = ""
      nc2 = ""

      ActiveRecord::Base.connection_pool.with_connection do |conns|
        nw = Person.count
        nc2 = ActiveRecord::Base.connection_pool.connections.size
      end

      nc3 = ActiveRecord::Base.connection_pool.connections.size
      puts "First there were #{nc1} connections, after things there were #{nc2} and now finally there are #{nc3} connections, there are #{nw} people in the db"  
    end
  end 
end

当我执行10.times {A.foo}时,它会给我这个输出。

First there were 1 connections, after things there were 3 and now finally there are 5 connections, there are 5325 people in the db
First there were 1 connections, after things there were 4 and now finally there are 5 connections, there are 5325 people in the db
First there were 1 connections, after things there were 2 and now finally there are 5 connections, there are 5325 people in the db
First there were 1 connections, after things there were 5 and now finally there are 5 connections, there are 5325 people in the db
First there were 1 connections, after things there were 5 and now finally there are 5 connections, there are 5325 people in the db
First there were 1 connections, after things there were 5 and now finally there are 5 connections, there are 5325 people in the db
First there were 1 connections, after things there were 5 and now finally there are 5 connections, there are 5325 people in the db
First there were 1 connections, after things there were 5 and now finally there are 5 connections, there are 5325 people in the db
First there were 1 connections, after things there were 5 and now finally there are 5 connections, there are 5325 people in the db
First there were 1 connections, after things there were 5 and now finally there are 5 connections, there are 5325 people in the db

最后我跑了:

ActiveRecord::Base.connection_pool.connections.size
5

现在根据documentation with_connection应该接受一个块并使用一个连接执行它然后关闭,但根据我的输出它不会。我真的不明白。

有没有人有任何解决方案或想法可能会发生这种情况?使用“connection_pool.connections.size”正确的方法来检查有多少连接?

还有其他方法可以在rails中实现多线程数据库查询吗?

1 个答案:

答案 0 :(得分:0)

好的,我只是不明白connection_pool是如何工作的。连接池保存自创建池以来已打开的连接,无论它们是否正在使用。所以我的问题的答案是你不能(通过今天实现的方式)connection_pool来查看正在使用哪些连接。相反,我修补了连接池本身以包含此功能。

如果有人有兴趣,这是我在config / initializers / connection_pool_patch.rb中的补丁:

module ActiveRecord
  module ConnectionAdapters
    class ConnectionPool
      def num_available
        @available.size
      end
    end
  end
end

module ActiveRecord
  module ConnectionAdapters
    class ConnectionPool
      class Queue
        def size
          @queue.size
        end
      end
    end
  end
end

它公开了包含连接池中可用(即当前未使用但已打开)连接的私有列表@available的大小。 Usage = ActiveRecord :: Base.connection_pool.num_available