ObjectExtender User's Guide and Reference

Nested transactions

All transactions have a parent transaction. This is the transaction from which they were created with the beginChild method.

The shared transaction has no parent transaction. It represents a persisted view of the world. It could be thought as a proxy or gateway to the actual database which is shared with other users.

The top-level transaction can be thought of as a special transaction which is a child of the shared transaction. When it is committed, its changes are merged into the shared transaction where they become persisted and permanent.

It is not just the shared transaction which can create child transactions. Any transaction can create child transactions providing support for different levels of nested transactions. The following code creates a top-level transaction, and then creates a child of the top-level transaction. A department is created in the top-level child and committed through to the shared transaction.

 
 
     | topLevel topLevelChild |
     topLevel := Transaction begin.
     topLevelChild := topLevel beginChild.
     TstDepartmentHome singleton create department: 'Geography'.
     topLevelChild commit.
 
 

When the top-level child is committed it merges its changes into its parent, in the above case, the parent is the top-level transaction. Inspecting allInstances from the home collection, you will see that the Geography department has not yet been persisted.

 
 
     TstDepartmentHome singleton allInstances.
 
 
 

If the top-level transaction is committed, the changes are applied to the shared transaction, and the new department will become persisted.

 
 
     | topLevel topLevelChild |
     topLevel := Transaction begin.
     topLevelChild := topLevel beginChild.
     TstDepartmentHome singleton create department: 'Geography'.
     topLevelChild commit.
     topLevel commit.
 
 

Intuitively, if we did not commit the top-level child, but only committed tht top-level transaction, we might expect that the Geography department would not become persisted. We have not explicitly committed the top-level child transaction (the one in which the department was created) and therefore it has not been applied to the top-level transaction. However, when a transaction is committed, all of its child transactions are also committed.

 
 
     | topLevel topLevelChild |
     topLevel := Transaction begin.
     topLevelChild := topLevel beginChild.
     TstDepartmentHome singleton create department: 'Geography'.
     topLevel commit.
 
 
 

The result of this is that the Geography department is persisted and becomes visible in the shared transaction.

 
 
     TstDepartmentHome singleton allInstances.
 
 
 

When a transaction is committed, it first commits all of its child transactions in the order they were created, and then commits itself. Thus, by committing the topLevel transaction, the Geography department is first pushed from the topLevelChild into the topLevel and from there to the shared transaction.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]