Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sonots/activerecord-refresh_connection
Refresh ActiveRecord connection on each rack request
https://github.com/sonots/activerecord-refresh_connection
Last synced: 6 days ago
JSON representation
Refresh ActiveRecord connection on each rack request
- Host: GitHub
- URL: https://github.com/sonots/activerecord-refresh_connection
- Owner: sonots
- License: mit
- Created: 2014-06-24T06:43:39.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2021-06-07T05:32:21.000Z (over 3 years ago)
- Last Synced: 2025-01-11T14:16:17.241Z (7 days ago)
- Language: Ruby
- Size: 55.7 KB
- Stars: 129
- Watchers: 8
- Forks: 16
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# activerecord-refresh_connection
[![Build Status](https://secure.travis-ci.org/sonots/activerecord-refresh_connection.png?branch=master)](http://travis-ci.org/sonots/activerecord-refresh_connection)
[![Coverage Status](https://coveralls.io/repos/sonots/activerecord-refresh_connection/badge.png?branch=master)](https://coveralls.io/r/sonots/activerecord-refresh_connection?branch=master)Refresh ActiveRecord connection on each rack request
## Installation
Add the following to your `Gemfile`:
```ruby
gem 'activerecord-refresh_connection'
```And then execute:
```plain
$ 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.
```ruby
# 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.
```bash
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.
```ruby
# 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.
```bash
bundle exec rake middleware
```### Sinatra
```ruby
# 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: 5run App
```## See Also
* [例えば ActiveRecord の connection_pool を止める - sonots:blog](http://blog.livedoor.jp/sonots/archives/38797925.html) (Japanese)
## ChangeLog
See [CHANGELOG.md](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](../../pull/new/master)## Copyright
Copyright (c) 2014 Naotoshi Seo. See [LICENSE.txt](LICENSE.txt) for details.