FreshConnection

Description

FreshConnection supports of connect with Mysql slave servers via Load Balancers. Currently, I have only tested with Rails2.3.14. In the future, I plan to support Rails3.

All connections will be disconnected every time at the end of the action.

How to use at Rails

Gemfile

gem "fresh_connection", "=0.0.2"

config/database.yml

production:
  adapter: mysql
  encoding: utf8
  reconnect: true
  database: kaeru
  pool: 5
  username: master
  password: master
  host: localhost
  socket: /var/run/mysqld/mysqld.sock

  slave:
    username: slave
    password: slave
    host: slave

slave is config to connect to slave servers. Others will use the master setting. If you want to change, write in the slave.

config/environment.rb

require 'fresh_connection'
ActionController::Dispatcher.middleware.swap ActiveRecord::ConnectionAdapters::ConnectionManagement, FreshConnection::Rack::ConnectionManagement

config/initializers/fresh_connection.rb

FreshConnection::SlaveConnection.ignore_models = %w|Model1 Model2|

If models that ignore access to slave servers is exist, You can write model name at FreshConnection::SlaveConnection.ignore_models.

Synopis

Read query will be access to slave server.

Article.where(:id => 1)

If you want to access to master saver, use readonly(false).

Article.where(:id => 1).readonly(false)

In transaction, Always will be access to master server.

Article.transaction do
  Article.where(:id => 1)
end