How to manage opening and closing database connections while working with activerecords and multiple threads

后端 未结 2 1800
误落风尘
误落风尘 2020-12-15 10:12

I am trying to implement a multithreaded method in rails such that I can create/update multiple records very quickly.

This is the outline of my program.



        
相关标签:
2条回答
  • 2020-12-15 10:26

    To prevent connection leak in multi threads, you have to manage the connection manually. You can try:

    Thread.new do
      ActiveRecord::Base.connection_pool.with_connection do
        # Do whatever
      end
    end
    

    One problem of ActiveRecord::Base.connection_pool.with_connection is that it always prepare a connection even if the code inside does not need it.

    We can improve it a little bit by using ActiveRecord::Base.connection_pool.release_connection:

    Thread.new do
      begin
        # Do whatever
      ensure
        ActiveRecord::Base.connection_pool.release_connection
      end
    end
    
    0 讨论(0)
  • 2020-12-15 10:37

    if you want to run in N threads, make sure you have N connections in the pool

    #  ActiveRecord::Base.connection_config
    #  # => {pool: 5, timeout: 5000, database: "db/development.sqlite3", adapter: "sqlite3"}
    

    otherwise you will be able to fetch a connection from the pool.

    0 讨论(0)
提交回复
热议问题