Activerecord Connection Warning. (Database Connections Will Not Be Closed Automatically)

ActiveRecord connection warning. (Database connections will not be closed automatically)

You need to add a middleware to your stack.
Just add this line to your config.ru rack up file:

use ActiveRecord::ConnectionAdapters::ConnectionManagement

Found the answer here: https://github.com/puma/puma/issues/59

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

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

closing ActiveRecord connection on a dead thread

Ensure connection closing =)

Thread.new do
begin
raise "foo"
ensure
begin
if (ActiveRecord::Base.connection && ActiveRecord::Base.connection.active?)
ActiveRecord::Base.connection.close
end
rescue
end
end
end

How do I reuse connections in ActiveRecord?

One way of cleaning connections is to call ActiveRecord::Base.clear_active_connections! after each use of Subscriber.find_by_email and Subscriber.create, like so.

after do
ActiveRecord::Base.clear_active_connections!
end

Somewhere else, some people suggest using middleware ActiveRecord::ConnectionAdapters::ConnectionManagement. That does not work in Thin because Thin starts processing each request in a thread and finishes processing the request in another thread. ConnectionManagement only returns to the pool those connections that are active for the current thread and Thin applies ConnectionManagement in the second thread instead of the first.

EDIT:

I made an explanation of why using middleware ActiveRecord::Connectionadapters::ConnectionManagement won't work with Thin in threaded mode, which you can find here.

2016.05.18 EDIT:

I followed up this issue with Thin author and this issue is not resolved after issue 307.

Replacing ActiveRecord::ConnectionAdapters::ConnectionManagement in ActiveRecord 5

You are correct, the ConnectionManagement middleware has been removed from ActiveRecord 5 (PR #23807), so you will need to replicate similar functionality when setting up ActiveRecord outside of Rails. There are several ways to do this:

1. ConnectionManagement Rack middleware

The ConnectionManagement class is not very complicated. You can copy and paste the implementation somewhere in your local application, and include it as usual into your Rack middleware stack:

class ConnectionManagement
def initialize(app)
@app = app
end

def call(env)
testing = env['rack.test']

status, headers, body = @app.call(env)
proxy = ::Rack::BodyProxy.new(body) do
ActiveRecord::Base.clear_active_connections! unless testing
end
[status, headers, proxy]
rescue Exception
ActiveRecord::Base.clear_active_connections! unless testing
raise
end
end

use ConnectionManagement

2. (Sinatra-specific) connection-management after hook

Within a Sinatra app, the block you suggested should work:

after do
ActiveRecord::Base.clear_active_connections!
end

Note that this is also the approach currently used by the sinatra-activerecord integration gem to support ActiveRecord 5 (see issue #73).

3. ActionDispatch::Executor Rack middleware

Finally, you can use the same code Rails is now using for ActiveRecord connection management by adding ActionDispatch::Executor into your Rack middleware stack, and calling ActiveRecord::QueryCache#install_executor_hooks to insert the hook used to clear ActiveRecord connections:

require 'action_dispatch/middleware/executor'
use ActionDispatch::Executor, ActiveSupport::Executor
ActiveRecord::QueryCache.install_executor_hooks


Related Topics



Leave a reply



Submit