No release in over 3 years
Low commit activity in last 3 years
Refresh ActiveRecord connection on each rack request.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0
>= 0
>= 0

Runtime

 Project Readme

activerecord-refresh_connection

Build Status Coverage Status

Refresh ActiveRecord connection on each rack request

Installation

Add the following to your Gemfile:

gem 'activerecord-refresh_connection'

And then execute:

$ bundle

How to Use

This gem provides a rack middleware ActiveRecord::ConnectionAdapters::RefreshConnectionManagement which disconnects all connections in each rack request, which results in refreshing all connections in each rack request.

Rails 5

NOTE: activerecord-refresh_connection does not work with puma, and webrick server in rails 5.

# config/application.rb
class Application < Rails::Application
  config.middleware.insert_before ActionDispatch::Executor,
    ActiveRecord::ConnectionAdapters::RefreshConnectionManagement

  ## If you would like to clear connections after 5 requests:
  # config.middleware.insert_before ActionDispatch::Executor,
  #   ActiveRecord::ConnectionAdapters::RefreshConnectionManagement, max_requests: 5
end

Middleware check.

bundle exec rake middleware

Use unicorn server to run your application.

Rails 4

NOTE: activerecord-refresh_connection does not work with puma server in rails 4.

Swap the default rails ConnectionManagement.

# config/application.rb
class Application < Rails::Application
  config.middleware.swap ActiveRecord::ConnectionAdapters::ConnectionManagement,
    "ActiveRecord::ConnectionAdapters::RefreshConnectionManagement"

  ## If you would like to clear connections after 5 requests:
  # config.middleware.insert_before ActiveRecord::ConnectionAdapters::ConnectionManagement,
  #   "ActiveRecord::ConnectionAdapters::RefreshConnectionManagement", max_requests: 5
  # config.middleware.delete ActiveRecord::ConnectionAdapters::ConnectionManagement
end

Middleware check.

bundle exec rake middleware

Sinatra

# config.ru
require 'activerecord-refresh_connection'

use ActiveRecord::ConnectionAdapters::RefreshConnectionManagement

## If you would like to clear connections after 5 requests:
# use ActiveRecord::ConnectionAdapters::RefreshConnectionManagement, max_requests: 5

run App

See Also

ChangeLog

See CHANGELOG.md for details.

Development

Run example

cd example/xxxx
bundle
bundle exec foreman start

Watch show processlist in mysql console to see connections are killed on each access.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Copyright

Copyright (c) 2014 Naotoshi Seo. See LICENSE.txt for details.