DEV Community

Discussion on: Active Record, Sidekiq, pools and threads

Collapse
 
janko profile image
Janko Marohnić

Notice that the real problem is that ActiveRecord doesn't automatically release checked out connections back into the connection pool. You can see that by running the following script:

require "active_record"

ActiveRecord::Base.establish_connection(
  adapter:  "sqlite3",
  database: ":memory:"
)

threads = []

10.times do
  threads << Thread.new do
    ActiveRecord::Base.connection.execute "SELECT 1"
  end
end

threads.each(&:join)
Enter fullscreen mode Exit fullscreen mode
ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds (waited 5.001 seconds); all pooled connections were in use
Enter fullscreen mode Exit fullscreen mode

Contrast that with Sequel which checks out connections just fine:

require "sequel"

DB = Sequel.sqlite

threads = []

10.times do
  threads << Thread.new do
    DB.run "SELECT 1"
  end
end

threads.each(&:join)
Enter fullscreen mode Exit fullscreen mode
(no errors)
Enter fullscreen mode Exit fullscreen mode