Fencing Transactions

The programming practice of fencing logical transactions protects database integrity during a system interruption. A logical transaction is a logical unit that is not complete unless all parts of the transaction are captured. For instance, the logical transaction "transfer funds between accounts" consists of a debit update to one account and a credit update to another account.

Establishing fences around a logical transaction assures that the transaction is committed as a unit, thereby avoiding logical inconsistencies. These logical inconsistencies, sometimes referred to as application-level database integrity problems, manifest themselves as run-time errors, inappropriate branching, and incorrect reports.

In GT.M, there are two methods of defining application transaction boundaries. One method is using the transaction commands TSTART and TCOMMIT. The other method uses the GT.M journal fencing commands ZTSTART and ZTCOMMIT.

TSTART or ZTSTART activate transaction fence control. Subsequent SETs and KILLs across all accessed regions are marked as belonging to a fenced transaction. The TCOMMIT or ZTCOMMIT commands respectively, close the fenced transaction and write the TCOMMIT and ZTCOMMIT journal records in all the journal files of all regions involved in the transaction.

If a TSTART has been processed, and another TSTART is encountered prior to a TCOMMIT, then the second TSTART does not cause another fence to be emitted to the journal file, instead increments a "transaction depth" counter accessible as $TLEVEL. Succeeding TCOMMIT directives cause decrementing of this transaction depth counter. The TCOMMIT fence is emitted to the journal file when the "transaction depth" returns to zero (0) (such occurrences are referred to as "nested" transactions or sub-transactions).