Add Database#checkout, Connection#release for non block connection use (#38)

This commit is contained in:
Brian J. Cardiff 2017-02-12 16:30:32 -03:00 committed by GitHub
parent 09b8997636
commit 3fa1eac6c5
3 changed files with 55 additions and 5 deletions

View file

@ -71,9 +71,16 @@ module DB
protected def after_release
end
# return this connection to the pool
# managed by the database. Should be used
# only if the connection was obtained by `Database#checkout`.
def release
@database.return_to_pool(self)
end
# :nodoc:
def release_from_statement
@database.return_to_pool(self) if @auto_release && !@transaction
self.release if @auto_release && !@transaction
end
# :nodoc:

View file

@ -94,18 +94,26 @@ module DB
end
# yields a connection from the pool
# the connection is returned to the pool after
# the connection is returned to the pool
# when the block ends
def using_connection
connection = @pool.checkout
connection.auto_release = false
connection = self.checkout
begin
yield connection
ensure
return_to_pool connection
connection.release
end
end
# returns a connection from the pool
# the returned connection must be returned
# to the pool by explictly calling `Connection#release`
def checkout
connection = @pool.checkout
connection.auto_release = false
connection
end
# yields a `Transaction` from a connection of the pool
# Refer to `BeginTransaction#transaction` for documentation.
def transaction