Method: Hyperactive::Record::Persistent#with_transaction
- Defined in:
- lib/hyperactive/record.rb
#with_transaction(transaction, &block) ⇒ Object
Will execute block
within a transaction.
What it does is just set the @transaction instance variable before calling the block, and unsetting it after.
This means that any classes that want to be transaction sensitive need to take heed regarding the @transaction instance variable.
For example, when creating new Record instances you may want to use get_instance_with_transaction(@transaction, *args) to ensure that the new instance exists within the same transaction as yourself.
See Hyperactive::List::Head and Hyperactive::Hash::Head for examples of this behaviour.
92 93 94 95 96 97 98 99 100 |
# File 'lib/hyperactive/record.rb', line 92 def with_transaction(transaction, &block) old_transaction = @transaction if defined?(@transaction) @transaction = transaction begin return yield ensure @transaction = old_transaction end end |