12.07.2015 Views

Using Caché Globals - InterSystems Documentation

Using Caché Globals - InterSystems Documentation

Using Caché Globals - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Managing Transactions3.11 Managing Transactions<strong>Caché</strong> provides the primitive operations needed to implement full transaction processing using globals.<strong>Caché</strong> Objects and SQL make use of these features automatically. If you are directly writing transactionaldata into globals, you can make use of these operations.The transaction commands are TSTART, which defines the start of a transaction; TCOMMIT, whichcommits the current transaction; and TROLLBACK, which aborts the current transaction and undoesany changes made to globals since the start of the transaction.For example, the following <strong>Caché</strong> ObjectScript code defines the start of a transaction, sets a numberof global nodes, and then commits or rolls back the transaction depending on the value of ok:TSTARTSet ^Data(1) = "Apple"Set ^Data(2) = "Berry"If (ok) {TCOMMIT}Else {TROLLBACK}The TSTART writes a transaction start marker in the <strong>Caché</strong> journal file. This defines the startingboundary of the transaction. If the variable ok is true (nonzero) in the above example, then theTCOMMIT command marks the successful end of the transaction and a transaction completion markeris written to the journal file. If ok is false (0), then the TROLLBACK command will undo every setor kill operation made since the start of the transaction. In this case, ^Data(1) and ^Data(2) are restoredto their previous values.Note that no data is written at the successful completion of a transaction. This is because all modificationsto the database during a transaction are carried out as normal during the course of a transaction.Only in the case of a rollback is the data in the database affected. This implies that the transaction inthis example has limited isolation; that is, other processes can see the modified global values beforethe transaction is committed. This is typically referred to as an uncommitted read. Whether this is goodor bad depends on application requirements; in many cases this is perfectly reasonable behavior. If anapplication requires a higher degree of isolation, then this is accomplished by using locks. This isdescribed in the following section.3.11.1 Locks and TransactionsTo create isolated transactions—that is, to prevent other processes from seeing modified data beforea transaction is committed—requires the use of locks. Within <strong>Caché</strong> ObjectScript, you can directlyacquire and release locks by means of the LOCK command. Locks work by convention; for a given<strong>Using</strong> <strong>Caché</strong> <strong>Globals</strong> 29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!