Fast sql based session storage for Rails - Updated for mysql2
Switch branches/tags
Nothing to show
Pull request Compare This branch is 6 commits ahead of skaes:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


== SqlSessionStore


Only Mysql, Postgres and Oracle are currently supported (others work,
but you won't see much performance improvement).

== Step 1

If you have generated your sessions table using rake db:sessions:create, go to Step 2

If you're using an old version of sql_session_store, run
    script/generate sql_session_store DB
where DB is mysql, postgresql or oracle

Then run
    rake migrate
    rake db:migrate
for new versions of Rails.

== Step 2

Initialisation depends on the Rails version being used:

=== Older Rails versions

If you are using a version before 2.3, add the code below after the
initializer config section:

      update(:database_manager => SqlSessionStore)

Then, depending on your database type, add

    SqlSessionStore.session_class = MysqlSession

    SqlSessionStore.session_class = PostgresqlSession
    SqlSessionStore.session_class = OracleSession

after the initializer section in environment.rb

=== Rails 2.3

Rails 2.3 uses rack based sessions, which require that the session
storage manager shows up in a certain position in the middleware
stack. The Rails initializer has special code built in to ensure this
for active record store. Therfore, the following steps will ensure that
sql session store ends up in the correct place:

* set up active recordstore as the session store in the config section
* replace active record store with sql session store after the initializer section do |config|
    config.action_controller.session_store = :active_record_store
  ActionController::Dispatcher.middleware.swap(:"ActiveRecord::SessionStore", SqlSessionStore)
  ActionController::Base.session_store = SqlSessionStore
  SqlSessionStore.session_class = MysqlSession

It is really important that sql session store ends up in the same place in
the middleware stack as active record store. More specifically: it
must be placed below the connection management middleware.

Your middleware stack should look similar to the following one:

  use Rack::Lock
  use ActionController::Failsafe
  use ActionController::Reloader
  use ActiveRecord::ConnectionAdapters::ConnectionManagement
  use ActiveRecord::QueryCache
  use SqlSessionStore
  use ActionController::ParamsParser
  use Rack::MethodOverride
  use Rack::Head

You can run rake middlware to verify your setup.

== Step 3 (optional)

If you want to use a database separate from your default one to store
your sessions, specify a configuration in your database.yml file (say
sessions), and establish the connection on SqlSession in

   SqlSession.establish_connection :sessions


1. The class name SQLSessionStore has changed to SqlSessionStore to
   let Rails work its autoload magic.

2. You will need the binary drivers for Mysql or Postgresql.
   These have been verified to work:

   * ruby-postgres ( with postgreql 8.1
   * ruby-mysql 2.7.1 with Mysql 4.1
   * ruby-mysql 2.7.2 up to 2.7.7 with Mysql 5.0