Transactions
Group statements with START TRANSACTION, COMMIT, ROLLBACK and savepoints, and learn exactly which guarantees Raft DB gives and which it does not.
The statements give the same output as MySQL 9.7. Read the last section before you rely on transactions.
Commit and rollback#
DROP DATABASE IF EXISTS tut4;
CREATE DATABASE tut4;
USE tut4;
CREATE TABLE acct (id INT PRIMARY KEY, bal INT NOT NULL, owner VARCHAR(10), UNIQUE KEY u_owner (owner));
INSERT INTO acct VALUES (1, 100, 'ann'), (2, 50, 'bob');
START TRANSACTION;
UPDATE acct SET bal = bal - 30 WHERE id = 1;
UPDATE acct SET bal = bal + 30 WHERE id = 2;
SELECT id, bal FROM acct ORDER BY id;
ROLLBACK;
SELECT id, bal FROM acct ORDER BY id;Inside the transaction the two rows show 70 and 80; after ROLLBACK they are 100 and 50 again. BEGIN is the same as START TRANSACTION. COMMIT keeps the changes.
ROLLBACK also undoes inserts, deletes, changes to unique keys, REPLACE, ON DUPLICATE KEY UPDATE and multi-table writes made in the transaction:
BEGIN;
INSERT INTO acct VALUES (3, 10, 'cy');
DELETE FROM acct WHERE id = 2;
UPDATE acct SET owner = 'zed' WHERE id = 1;
SELECT id, bal, owner FROM acct ORDER BY id;
ROLLBACK;Savepoints#
START TRANSACTION;
INSERT INTO acct VALUES (5, 5, 'eve');
SAVEPOINT s1;
INSERT INTO acct VALUES (6, 6, 'fay');
SAVEPOINT s2;
DELETE FROM acct WHERE id = 3;
ROLLBACK TO SAVEPOINT s2;
ROLLBACK TO s1;
RELEASE SAVEPOINT s1;
COMMIT;(In the script row 3 was inserted and committed earlier, so the DELETE finds a row.) ROLLBACK TO s2 undoes the delete only; ROLLBACK TO s1 then also undoes the insert of row 6. Row 5 is committed.
Autocommit#
SET autocommit = 0;
INSERT INTO acct VALUES (7, 7, 'gus');
SELECT @@autocommit;
ROLLBACK;
SELECT id FROM acct WHERE id = 7;With autocommit = 0 a transaction starts with the first statement and lasts until COMMIT or ROLLBACK; row 7 is gone after the rollback. Statements that define things (CREATE, DROP, ALTER, TRUNCATE, accounts and GRANT, LOCK TABLES) commit the open transaction first, as in MySQL, and the script checks it with CREATE TABLE.
A statement that fails half-way inside a transaction is undone as a statement, and the transaction goes on.
What a transaction is in Raft DB, and what it is not#
| Guarantee | Raft DB |
|---|---|
Atomicity (all or nothing, ROLLBACK) | Yes. Each statement keeps the undo records of what it wrote; inside a transaction they are kept in the session's log and replayed backwards on ROLLBACK. A connection that ends with a transaction open rolls it back. |
Durability at COMMIT | Yes: writes are answered when applied and the front-end waits for the disk once, at COMMIT. |
| Isolation | Per table. A session that writes a table inside a transaction holds it until COMMIT or ROLLBACK; another session that reads or writes it waits (error 1205 after innodb_lock_wait_timeout, 1213 for a deadlock). There are no dirty reads. @@transaction_isolation reports READ-COMMITTED; repeatable reads are not given. |
| Conflict detection | None. Two sessions can overwrite each other's rows. |
SELECT ... FOR UPDATE, LOCK IN SHARE MODE, LOCK TABLES | Accepted, and they lock nothing. |
| Crash of the front-end during a transaction | What had been written stays written; there is no commit record to undo from. |
Snapshot reads, where readers never wait, are not built: they need row versions carried through the shards and the write-ahead log, a commit record and a read view per transaction. The building block (raftdb-mvcc) exists but is not connected; it is a project of its own (D57). Design your application so that it does not depend on isolation (single writer per row, optimistic checks in the WHERE clause, INSERT with a unique key as the lock).