首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 4多线程和connection_pool问题

Rails 4多线程和connection_pool问题
EN

Stack Overflow用户
提问于 2015-05-21 22:03:11
回答 1查看 614关注 0票数 2

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

代码语言:javascript
复制
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.x {A.foo}时,它会给出以下输出。

代码语言:javascript
复制
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

最后我跑起来:

代码语言:javascript
复制
ActiveRecord::Base.connection_pool.connections.size
5

现在根据documentation的说法,with_connection应该通过一个连接来执行它,然后它会关闭,但是根据我的输出,它没有,我真的不明白。

有没有人有任何解决方案或想法,为什么会发生这种情况?使用"connection_pool.connections.size“是检查有多少连接的正确方式吗?

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

EN

回答 1

Stack Overflow用户

发布于 2015-06-10 18:10:33

好吧,我只是不理解connection_pool是如何工作的。连接池保存自创建池以来已打开的连接,无论它们是否正在使用。因此,我的问题的答案是,您无法(以目前的实现方式)通过connection_pool来查看哪些连接正在被活跃地使用。相反,我对连接池本身进行了修补,以包含此功能。

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

代码语言:javascript
复制
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的大小,该私有列表保存了连接池中可用(即当前未使用但已打开)的连接。用法= ActiveRecord::Base.connection_pool.num_available

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30376078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档