Skip to content

tatsuma/activerecord-refresh_connection

 
 

Repository files navigation

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

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.

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.

About

Refresh ActiveRecord connection on each rack request

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 70.7%
  • HTML 20.8%
  • CSS 5.6%
  • JavaScript 1.8%
  • CoffeeScript 1.1%