Created
February 17, 2009 22:39
-
-
Save nicksieger/66038 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb | |
index 901b171..2416f3b 100644 | |
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb | |
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb | |
@@ -91,11 +91,20 @@ module ActiveRecord | |
# | |
# #connection can be called any number of times; the connection is | |
# held in a hash keyed by the thread id. | |
- def connection | |
- if conn = @reserved_connections[current_connection_id] | |
- conn | |
+ def connection(&block) | |
+ fresh_connection = false | |
+ unless conn = @reserved_connections[current_connection_id] | |
+ fresh_connection = true | |
+ conn = @reserved_connections[current_connection_id] = checkout | |
+ end | |
+ if block_given? | |
+ begin | |
+ yield conn | |
+ ensure | |
+ release_connection if fresh_connection | |
+ end | |
else | |
- @reserved_connections[current_connection_id] = checkout | |
+ conn | |
end | |
end | |
@@ -321,9 +330,9 @@ module ActiveRecord | |
# active or defined connection: if it is the latter, it will be | |
# opened and set as the active connection for the class it was defined | |
# for (not necessarily the current class). | |
- def retrieve_connection(klass) #:nodoc: | |
+ def retrieve_connection(klass, &block) #:nodoc: | |
pool = retrieve_connection_pool(klass) | |
- (pool && pool.connection) or raise ConnectionNotEstablished | |
+ (pool && pool.connection(&block)) or raise ConnectionNotEstablished | |
end | |
# Returns true if a connection that's accessible to this class has | |
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb | |
index bbc290f..9ad69df 100644 | |
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb | |
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb | |
@@ -16,8 +16,19 @@ module ActiveRecord | |
# Returns the connection currently associated with the class. This can | |
# also be used to "borrow" the connection to do database work that isn't | |
# easily done without going straight to SQL. | |
- def connection | |
- self.class.connection | |
+ # | |
+ # If called with a block, the connection is automatically released | |
+ # after the block is yielded. This block version is useful outside | |
+ # of normal actionpack dispatcher operation where connection | |
+ # cleanup must be managed by the application. | |
+ # | |
+ # User.connection do | |
+ # User.all.each do |u| | |
+ # # do something with each user | |
+ # end | |
+ # end # connection is released when the block completes | |
+ def connection(&block) | |
+ self.class.connection(&block) | |
end | |
# Establishes the connection to the database. Accepts a hash as input where | |
@@ -111,16 +122,16 @@ module ActiveRecord | |
# Returns the connection currently associated with the class. This can | |
# also be used to "borrow" the connection to do database work unrelated | |
# to any of the specific Active Records. | |
- def connection | |
- retrieve_connection | |
+ def connection(&block) | |
+ retrieve_connection(&block) | |
end | |
def connection_pool | |
connection_handler.retrieve_connection_pool(self) | |
end | |
- def retrieve_connection | |
- connection_handler.retrieve_connection(self) | |
+ def retrieve_connection(&block) | |
+ connection_handler.retrieve_connection(self, &block) | |
end | |
# Returns true if +ActiveRecord+ is connected. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment