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

Instance Method Details

#end_transaction!Object (private)

End the database transaction

See Also:



48
49
50
# File 'lib/db_mod/transaction.rb', line 48

def end_transaction!
  @in_transaction = false
end

#start_transaction!Object (private)

Start the database transaction, or fail if one is already open.



38
39
40
41
42
43
# File 'lib/db_mod/transaction.rb', line 38

def start_transaction!
  fail DbMod::Exceptions::AlreadyInTransaction if @in_transaction
  @in_transaction = true

  query 'BEGIN'
end

#transactionObject (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.

Returns:

  • (Object)

    the result of yield



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