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. For more information about transactions, see Overview of transactions.
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 (which indicates that the autocommit mode is disabled) for a tenant session, you do not need to explicitly issue the BEGIN command.
If the value of the autocommit parameter is 1 (which indicates that the autocommit mode is enabled) for a tenant session, each SQL statement is an independent transaction. If you want a transaction that includes more than one statement, you must explicitly run the BEGIN command to initiate a transaction. The automatic commit mode is disabled until the execution of the
COMMITorROLLBACKstatement.
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 after you close and reconnect the session.
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.