SQL reference (MySQL and MariaDB dialects)

START TRANSACTION, COMMIT, ROLLBACK and SAVEPOINT

The transaction statements of the MySQL and MariaDB front-ends, what they guarantee (atomic) and what they do not (isolation).

The front-ends have MySQL's transaction statements. A transaction is atomic (all of it or none of it) and isolated one table at a time (D57 and the table claims of txnlock).

Syntax#

SQL
START TRANSACTION [transaction_characteristic ...]
BEGIN [WORK]
COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
ROLLBACK [WORK]
SAVEPOINT identifier
ROLLBACK [WORK] TO [SAVEPOINT] identifier
RELEASE SAVEPOINT identifier
SET autocommit = { 0 | 1 }
SET [GLOBAL | SESSION] TRANSACTION transaction_characteristic [, ...]
LOCK TABLES ... | UNLOCK TABLES

Everything after START TRANSACTION, COMMIT and ROLLBACK up to the end of the statement is read and ignored (WITH CONSISTENT SNAPSHOT, READ ONLY, AND CHAIN, RELEASE).

Arguments#

NameMeaningDefault
autocommit1: every statement commits itself. 0: a transaction is open until COMMIT or ROLLBACK.1
identifierName of a savepoint. Unknown name: error 1305.none

Remarks#

  • How it works. Every write leaves undo records so that a failing statement is all or nothing. Inside a transaction they are also kept in the session's log. ROLLBACK replays the log backwards, ROLLBACK TO replays down to the savepoint, COMMIT forgets it. A connection that ends with a transaction open rolls it back. Writes inside a transaction wait for the disk once, at COMMIT.
  • Isolation is per table. A session that writes a table inside a transaction holds it until COMMIT or ROLLBACK; other sessions that read or write it wait (error 1205 after innodb_lock_wait_timeout, default 50 seconds; error 1213 at once for a deadlock). There are no dirty reads and no lost updates. @@transaction_isolation reports READ-COMMITTED: a later statement sees what others committed in between, and a long transaction holds up everybody who needs its tables. The setting isolation = none turns the claims off. A crash of the connector in the middle of a transaction leaves what had been written.
  • FOR UPDATE, FOR SHARE, LOCK IN SHARE MODE, LOCK TABLES and UNLOCK TABLES are accepted and lock nothing. LOCK TABLES commits an open transaction.
  • SET TRANSACTION ISOLATION LEVEL ... and SET TRANSACTION READ ONLY are accepted and change nothing.
  • Implicit commit (as in MySQL): CREATE, DROP, ALTER, TRUNCATE, account statements, GRANT and LOCK TABLES end the open transaction.
  • A statement that fails inside a transaction undoes its own rows and leaves the transaction open.
  • The status flags of every answer carry the in-transaction and autocommit bits that drivers read.
  • Real isolation (row versions, a read view per transaction) needs the core engine and is not built (level 4b in the plan).

XA transactions#

XA START 'xid', XA END 'xid', XA PREPARE 'xid', XA COMMIT 'xid' [ONE PHASE], XA ROLLBACK 'xid' and XA RECOVER [CONVERT XID] (MySQL) run with the states and errors of the servers (1397, 1398, 1399, 1400, 1440). XA PREPARE makes the writes durable and hands the transaction to a list shared by all sessions, so another session can commit or roll it back. After a restart of the front-end the writes of a prepared transaction are there, as if committed; the prepared state is lost (XA RECOVER is empty). JOIN, RESUME and SUSPEND are error 1398. Isolation is as for any transaction here: none.

Examples#

Example:

SQL
START TRANSACTION;
UPDATE acct SET bal = bal - 30 WHERE id = 1;
UPDATE acct SET bal = bal + 30 WHERE id = 2;
ROLLBACK;                       -- both updates are taken back

START TRANSACTION;
INSERT INTO acct VALUES (5, 5, 'eve');
SAVEPOINT s1;
INSERT INTO acct VALUES (6, 6, 'fay');
ROLLBACK TO SAVEPOINT s1;       -- row 6 is gone, row 5 stays
COMMIT;

SET autocommit = 0;
INSERT INTO acct VALUES (7, 7, 'gus');
ROLLBACK;

See also#