This topic describes transaction basic information and transaction control statements.
Overview of transactions
A database transaction is a series of database operations that are treated as a unit. The database is transformed from one consistent state to another after a transaction is executed. Generally, the SQL statements in a transaction include both DML statements and query statements. A transaction that contains only query statements is generally called a read-only transaction.
You can execute the ROLLBACK statement to roll back a transaction before it is committed.
Transaction control statements
The basic transaction control statements include:
BEGIN: explicitly starts a transaction. It is optional.
If the value of the autocommit parameter is 0 for a tenant session, the autocommit mode is disabled. In this case, you do not need to explicitly issue the
BEGINcommand to combine multiple SQL statements into one transaction.If the value of the autocommit parameter is 1 for a tenant session, the autocommit mode is enabled. In this case, each SQL statement is an independent transaction. To combine multiple SQL statements into one transaction, you need to explicitly issue the
BEGINcommand. In this case, the autocommit mode is disabled and will be resumed after aCOMMITorROLLBACKstatement is executed.
SAVEPOINT: marks a savepoint in a transaction. After the transaction is executed, you can roll back the transaction to the savepoint. Savepoints are optional. A transaction can have multiple savepoints.
COMMIT: commits and ends the current transaction, permanently applies all the changes in the transaction, deletes all the savepoints, and releases the locks that the transaction holds.
ROLLBACK: rolls back all the changes in a transaction or the changes after a specific savepoint, deletes all the savepoints in the statements that are rolled back, and releases the locks that the transaction holds.
In the CLI environment of OceanBase Client (OBClient), you can run a transaction control command after the SQL prompt, or modify the autocommit parameter at the session level to control whether to automatically commit transactions.
If you run
SET autocommitto set the autocommit parameter, the parameter takes effect immediately for the current session and becomes invalid after the session is closed.If you run
SET GLOBAL autocommitto set the autocommit parameter at the tenant level, the parameter takes effect only after the current session is closed.
Note
If the autocommit parameter of a session is set to 0 and no transaction is to be explicitly committed, when the program terminates abnormally, OceanBase Database automatically rolls back the last uncommitted transaction.