Created
February 3, 2009 17:51
-
-
Save nel/57640 to your computer and use it in GitHub Desktop.
Manage ActiveRecord connection pool with rack handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This is a Rack middle to release the connections checkout from the connection pool | |
#in a rack multithreaded environment | |
# This is mandatory outside of ActionController, as ActiveRecord request you to handle the | |
# release of the connections into the pool. If you don't do it, every n thread (where n is | |
# the AR connection pool size) will be stucked waiting for a 5s AR empty pool timeout | |
module Rack | |
module ActiveRecord | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
response = @app.call(env) | |
ensure | |
ActiveRecord::Base.clear_active_connections! | |
response | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment