Transaction Control
A transaction is a complete, logical unit of work. It usually involves adding, deleting or updating one or more logically related records in one or more databases. With Uniface, you can explicitly define a transaction with the commit ProcScript instruction. If an error occurs, a transaction can be rolled back with the rollback ProcScript instruction.
A Uniface transaction is the processing which occurs between two commit ProcScript instructions, or between the start of an application and the first commit. If an error occurs, the rollback statement can undo changes made in the database. The database remains as if the transaction had never taken place.
DBMS Behavior
Not all DBMSs support commit
and rollback. For DBMSs that do not completely support this function,
commit
and rollback
unlock only occurrences in the database; the
two instructions are thus identical. Be particularly careful with locks when using record-level
systems, because the
write action releases the lock as well, which means that you can unlock an
occurrence before the commit has taken place. For more information, see Locking Strategies.
Defining a Transaction
When using the commit and rollback ProcScript instructions, first decide what constitutes a logical unit of work. For many applications, a transaction begins when a component starts, and ends when the user tries to ^STORE. In this case, a commit instruction is usually issued after a successful store.
The following ProcScript defines this operation.
Error conditions are indicated by negative values returned to $status
. The
ProcScript checks $status and either commits the transaction or rolls the
transaction back, displaying the error in the message frame.
store if ($status < 0) message "Error # %%$status <STORE> not done." rollback else message "Transaction successfully completed." commit if ($status < 0) message "Commit error, rollback performed." rollback endif endif
Locking
Occurrences in the database that are updated by a transaction remain locked until a commit or rollback instruction is issued. The commit ProcScript instruction unlocks all occurrences. For this reason, the transaction should usually be as short as possible in order to allow other users access to data. The rollback ProcScript instruction also unlocks all occurrences.
Automatic Rollback at Application End
When an application ends, Uniface automatically issues a rollback, to take into account any errors which may cause the application to end. Therefore, you should ensure that all transactions have been completed, that is, that a commit instruction is issued before the normal completion of an application.
An easy way to ensure this is to place a commit instruction after the run instruction in the apStart trigger in the application shell.