Tuesday, December 9, 2008

Transactions in Gaphor

The Gaphor UML modeling tool has support for transactions. There is a decorator called transactional that wraps the specified function in a transaction. If an exception is raised, the entire transaction is rolled-back.

The transaction.py module also defines the Transaction class as well as a TransactionError exception.

Here we can see that the Transaction class provides the ITransaction interface and also depends on the TransactionError exception. This exception is raised by the Transaction._close() method if there are no transactions on the transaction stack or if the only transaction on the stack is not the current transaction being closed.

There are also three events that are initiated by the Transaction class; TransactionBegin, TransactionCommit, and TransactionRollback.

Here is an example using the transactional decorator.
#Gaphor transaction decorator.

from gaphor.transaction import transactional

class MyDB:
def update(self, data):
"""Update my hypothetical DB with data."""
print "Updating..."

if __name__=="__main__":
An important note about the example and the transactional decorator. There are no return values from the transactional-decorated functions or methods. This makes sense since the return value may no longer be valid if the transaction was rolled-back.