mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Add Database#checkout, Connection#release for non block connection use (#38)
This commit is contained in:
parent
09b8997636
commit
3fa1eac6c5
3 changed files with 55 additions and 5 deletions
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue