Using ROLLBACK

From InterBase

Go Up to Ending a Transaction


Use ROLLBACK to restore the database to its condition prior to the start of the transaction. ROLLBACK also closes the record streams associated with the transaction, resets the transaction name to zero, and frees system resources assigned to the transaction for other uses. ROLLBACK typically appears in error-handling routines. The syntax for ROLLBACK is:

EXEC SQL
ROLLBACK [TRANSACTION name] [RELEASE [dbhandle [, dbhandle ...]]];

For example, the following C code fragment contains a complete transaction that gives all employees who have worked since December 31, 1992, a 4.3% cost-of-living salary adjustment. If all qualified employee records are successfully updated, the transaction is committed, and the changes are actually applied to the database. If an error occurs, all changes made by the transaction are undone, and the database is restored to its condition prior to the start of the transaction.

. . .
EXEC SQL
SET TRANSACTION SNAPSHOT TABLE STABILITY;
EXEC SQL
UPDATE EMPLOYEES
SET SALARY = SALARY * 1.043
WHERE HIRE_DATE < '1-JAN-1993';
if (SQLCODE && (SQLCODE != 100))
{
isc_print_sqlerror(SQLCODE, isc_$status);
EXEC SQL
ROLLBACK;
EXEC SQL
DISCONNECT;
exit(1);
}
EXEC SQL
COMMIT;
EXEC SQL
DISCONNECT;
. . .

By default, ROLLBACK affects only the default transaction, GDS__TRANS. To roll back other transactions, use their transaction names as parameters to
ROLLBACK.

Advance To: