class GitDS::Transaction
A Transaction
takes a code block and guarantees that all of its commands, or none, are executed. Transactions can be nested.
Usage:
db.transaction { |trans, idx| ... }
Attributes
Public Instance Methods
Overrides ExecCmd#index
accessor as the Transaction
index will change if batch mode is used.
# File lib/git-ds/transaction.rb, line 71 def index database.index end
A transaction is considered successful if the transaction block executes with no exceptions. If a block must exit prematurely and abort the transaction, it should use the rollback method.
Note that exceptions are propagated to the parent if transaction is nested; this permits an inner transaction to abort an outer transaction when rollback() is called.
# File lib/git-ds/transaction.rb, line 39 def perform @propagate_exceptions = false # propagation off by default return perform_top_level if not self.nested instance_eval(&self.block) index.build true end
Throw all non-rollback exceptions after aborting the transaction. This is useful for debugging transaction blocks.
By default, all exceptions are caught and discarded.
# File lib/git-ds/transaction.rb, line 56 def propagate @propagate_exceptions = true end
Abort the transaction.
# File lib/git-ds/transaction.rb, line 63 def rollback raise TransactionRollback.new end
Private Instance Methods
Top-level transactions are performed in batch mode to speed things up.
# File lib/git-ds/transaction.rb, line 80 def perform_top_level @propagate_exceptions = true rv = true begin xact = self database.batch do xact.instance_eval(&xact.block) end commit @database.notify rescue Exception => e raise e if (not e.kind_of? TransactionRollback) && @propagate_exceptions rv = false end rv end