This topic describes basic transaction information and transaction control statements.
About 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
Basic transaction control statements:
BEGIN: explicitly starts a transaction. It is optional.
If the value of the
autocommitsystem variable is0for a tenant session, the autocommit mode is disabled. In this case, you do not need to explicitly issue theBEGINstatement to combine multiple SQL statements into one transaction.If the value of the
autocommitsystem variable is1for 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 theBEGINstatement. 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 obclient, you can execute a transaction control statement after the SQL prompt, or modify the autocommit variable at the session level to control whether to automatically commit transactions.
If you execute the
SET autocommitstatement to set the autocommit parameter, the parameter takes effect immediately for the current session and becomes invalid after the session is closed.If you execute the
SET GLOBAL autocommitstatement to set the autocommit parameter at the tenant level, the parameter takes effect only after you close and reconnect the session.Note
If the
autocommitvariable of a session is set to0and no transaction is to be explicitly committed, when the program terminates abnormally, OceanBase Database automatically rolls back the last uncommitted transaction.