Skip to content

Instantly share code, notes, and snippets.

@nel
Created February 3, 2009 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nel/57640 to your computer and use it in GitHub Desktop.
Save nel/57640 to your computer and use it in GitHub Desktop.
Manage ActiveRecord connection pool with rack handler
#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