Class: DB::Context::Transaction
- Defined in:
- lib/db/context/transaction.rb
Overview
A database transaction context that extends Session with transaction management capabilities.
Instance Attribute Summary
Attributes inherited from Session
Instance Method Summary collapse
-
#abort ⇒ Object
Abort the transaction and return the connection to the connection pool.
-
#begin ⇒ Object
Begin a transaction.
-
#commit ⇒ Object
Commit the transaction and return the connection to the connection pool.
-
#commit? ⇒ Boolean
Commit the transaction if it’s still open, otherwise do nothing.
-
#rollback(name) ⇒ Object
Return back to a previously registered savepoint.
-
#savepoint(name) ⇒ Object
Mark a savepoint in the transaction.
Methods inherited from Session
#call, #clause, #close, #closed?, #connect!, #initialize, #query, #with_connection
Constructor Details
This class inherits a constructor from DB::Context::Session
Instance Method Details
#abort ⇒ Object
Abort the transaction and return the connection to the connection pool.
33 34 35 36 |
# File 'lib/db/context/transaction.rb', line 33 def abort self.call("ROLLBACK") self.close end |
#begin ⇒ Object
Begin a transaction.
13 14 15 16 |
# File 'lib/db/context/transaction.rb', line 13 def begin self.connect! self.call("BEGIN") end |
#commit ⇒ Object
Commit the transaction and return the connection to the connection pool.
19 20 21 22 |
# File 'lib/db/context/transaction.rb', line 19 def commit self.call("COMMIT") self.close end |
#commit? ⇒ Boolean
Commit the transaction if it’s still open, otherwise do nothing. This is a safe version of commit that checks if the transaction is still active.
26 27 28 29 30 |
# File 'lib/db/context/transaction.rb', line 26 def commit? unless self.closed? self.commit end end |
#rollback(name) ⇒ Object
Return back to a previously registered savepoint.
44 45 46 |
# File 'lib/db/context/transaction.rb', line 44 def rollback(name) self.call("ROLLBACK #{name}") end |
#savepoint(name) ⇒ Object
Mark a savepoint in the transaction.
39 40 41 |
# File 'lib/db/context/transaction.rb', line 39 def savepoint(name) self.call("SAVEPOINT #{name}") end |