Skip to content

Commit

Permalink
Ensure ActiveRecord session store's connections are checked in after …
Browse files Browse the repository at this point in the history
…each request [#1927 state:resolved]
  • Loading branch information
josh committed Feb 24, 2009
1 parent d32eb41 commit 1b22071
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
7 changes: 0 additions & 7 deletions actionpack/lib/action_controller/dispatcher.rb
Expand Up @@ -13,7 +13,6 @@ def define_dispatcher_callbacks(cache_classes)
end

if defined?(ActiveRecord)
after_dispatch :checkin_connections
to_prepare(:activerecord_instantiate_observers) { ActiveRecord::Base.instantiate_observers }
end

Expand Down Expand Up @@ -115,11 +114,5 @@ def _call(env)
def flush_logger
Base.logger.flush
end

def checkin_connections
# Don't return connection (and peform implicit rollback) if this request is a part of integration test
return if @env.key?("rack.test")
ActiveRecord::Base.clear_active_connections!
end
end
end
Expand Up @@ -351,5 +351,21 @@ def retrieve_connection_pool(klass)
retrieve_connection_pool klass.superclass
end
end

class ConnectionManagement
def initialize(app)
@app = app
end

def call(env)
@app.call(env)
ensure
# Don't return connection (and peform implicit rollback) if
# this request is a part of integration test
unless env.key?("rack.test")
ActiveRecord::Base.clear_active_connections!
end
end
end
end
end
16 changes: 15 additions & 1 deletion railties/lib/initializer.rb
Expand Up @@ -176,6 +176,9 @@ def process
# the framework is now fully initialized
after_initialize

# Setup database middleware after initializers have run
initialize_database_middleware

# Prepare dispatcher callbacks and run 'prepare' callbacks
prepare_dispatcher

Expand Down Expand Up @@ -410,7 +413,18 @@ def initialize_database
if configuration.frameworks.include?(:active_record)
ActiveRecord::Base.configurations = configuration.database_configuration
ActiveRecord::Base.establish_connection
configuration.middleware.use ActiveRecord::QueryCache
end
end

def initialize_database_middleware
if configuration.frameworks.include?(:active_record)
if ActionController::Base.session_store == ActiveRecord::SessionStore
configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement
configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::QueryCache
else
configuration.middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement
configuration.middleware.use ActiveRecord::QueryCache
end
end
end

Expand Down

1 comment on commit 1b22071

@Manfred
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So where are the tests?

Please sign in to comment.