47.27.3. Poznámky k sesion managementu

The ActiveRecordStore backend for CGI::Session allows you to plug in custom session classes. See session/active_record_store.rb in Action Pack. The default session class is a normal Active Record class named Session. An alternative, SqlBypass, is provided which duck-types with the AR class but uses straight SQL over the db connection. You can use it as a starting point for pessimistic locking.

class MyPessimisticSession < CGI::Session::ActiveRecordStore::SqlBypass
  # Pick a database connection for straight SQL access.
  self.connection = ActiveRecord::Base.connection

  # Override the finder class method to do a SELECT ... FOR UPDATE.
  def self.find_by_session_id(session_id)
    @@connection.select_one "..."
  end
end

# Use our custom session class.
CGI::Session::ActiveRecordStore.session_class = MyPessimisticSession

# Use the ActiveRecordStore for CGI sessions.
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update(
  :database_manager => CGI::Session::ActiveRecordStore
)

As a footnote, I believe the default session backend (PStore) serializes access since it uses file locking. Perhaps it's worth a second look?

Licence Creative Commons
Tento dokument Ruby, jehož autorem je Radek Hnilica, podléhá licenci Creative Commons Uveďte autora-Nevyužívejte dílo komerčně-Zachovejte licenci 3.0 Česká republika .