Ending a Transaction
Go Up to Working with Transactions
When a transaction’s tasks are complete, or an error prevents a transaction from completing, the transaction must be ended to set the database to a consistent state. There are two statements that end transactions:
COMMIT
makes a transaction’s changes permanent in the database. It signals that a transaction completed all its actions successfully.ROLLBACK
undoes a transaction’s changes, returning the database to its previous state, before the transaction started.ROLLBACK
is typically used when one or more errors occur that prevent a transaction from completing successfully.
Both COMMIT
and ROLLBACK
close the record streams associated with the transaction, reinitialize the transaction name to zero, and release system resources allocated for the transaction. Freed system resources are available for subsequent use by any application or program.
COMMIT
and ROLLBACK
have additional benefits. They clearly indicate program logic and intention, make a program easier to understand, and most importantly, assure that a transaction’s changes are handled as intended by the programmer.
ROLLBACK
is frequently used inside error-handling routines to clean up transactions when errors occur. It can also be used to roll back a partially completed transaction prior to retrying it, and it can be used to restore a database to its prior state if a program encounters an unrecoverable error.
If the program ends before a transaction ends, a transaction is automatically rolled back, but databases are not closed. If a program ends without closing the database, data loss or corruption is possible. Therefore, open databases should always be closed by issuing explicit
DISCONNECT
, COMMIT RELEASE
, or ROLLBACK RELEASE
statements.For more information about DISCONNECT
, COMMIT RELEASE
, and ROLLBACK RELEASE
, see Working with Databases (Embedded SQL Guide).