Module: DbMod::Transaction
- Included in:
- DbMod
- Defined in:
- lib/db_mod/transaction.rb
Overview
Module which provides transaction blocks for db_mod enabled classes.
Instance Method Summary collapse
-
#end_transaction! ⇒ Object
private
End the database transaction.
-
#start_transaction! ⇒ Object
private
Start the database transaction, or fail if one is already open.
-
#transaction ⇒ Object
protected
Create a transaction on the db_mod database connection.
Instance Method Details
#end_transaction! ⇒ Object (private)
End the database transaction
43 44 45 |
# File 'lib/db_mod/transaction.rb', line 43 def end_transaction! @in_transaction = false end |
#start_transaction! ⇒ Object (private)
Start the database transaction, or fail if one is already open.
35 36 37 38 39 40 |
# File 'lib/db_mod/transaction.rb', line 35 def start_transaction! fail DbMod::Exceptions::AlreadyInTransaction if @in_transaction @in_transaction = true query 'BEGIN' end |
#transaction ⇒ Object (protected)
Create a transaction on the db_mod database connection. Calls BEGIN
then yields to the given block. Calls COMMIT
once the block yields, or ROLLBACK
if the block raises an exception.
Not thread safe. May not be called from inside another transaction.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/db_mod/transaction.rb', line 15 def transaction start_transaction! result = yield query 'COMMIT' result rescue query 'ROLLBACK' raise ensure end_transaction! end |