The BEGIN TRANSACTION statement initiates a transaction. A transaction is a block of code beginning with BEGIN TRANSACTION and ending with END TRANSACTION. All statements within the transaction are either applied as a unit by a COMMIT statement, or rolled back as a unit by a ROLLBACK statement. Following a COMMIT or ROLLBACK, program execution continues at the END TRANSACTION statement.
Caché MVBasic supports two sets of transaction statements:
UniVerse-style BEGIN TRANSACTION, COMMIT, ROLLBACK, and END TRANSACTION.
UniData-style TRANSACTION START, TRANSACTION COMMIT, and TRANSACTION ABORT.
These two sets of transaction statements should not be combined.
There is a fundamental difference in the way Caché transactions operate in comparison to most other transaction systems in the MV world. In Caché, items written to a file are immediately available both to the writing process and any other process accessing the file. If the transaction is aborted either programmatically or because of some failure, then the item will be rolled back to the state prior to the start of the transaction.
Most other transaction systems in the MV world will make an item written to a file available to the process that wrote the item (in other words, if it reads the item back from the file after the write, it will be given the version that it wrote to the file), but any other process READing the item will see the version of the item as it was before a write. This is generally referred to as the isolation level. This difference may have implications for systems that wish to scan files without taking locks.
The following example performs database operations within a transaction. It sets a variable x, which determines whether the transaction should be committed or rolled back.
PRINT "Before the transaction" BEGIN TRANSACTION . . . IF x=0 THEN COMMIT END ELSE ROLLBACK PRINT "Transaction rolled back" END PRINT "This should not print" END TRANSACTION PRINT "After the transaction"