START TRANSACTION;Explicit transactions let you group related writes into a single atomic unit.
start transaction, commit, rollback, savepoints, select for update, isolation levels, and InnoDB lock troubleshooting.
Atomic writes, commit, rollback, and savepoints.
START TRANSACTION;Explicit transactions let you group related writes into a single atomic unit.
COMMIT;After `COMMIT`, changes become durable and visible according to isolation semantics.
ROLLBACK;Crucial when a multi-step operation fails partway through.
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
SAVEPOINT after_debit;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
ROLLBACK TO SAVEPOINT after_debit;
COMMIT;Savepoints are useful when a larger transaction contains optional or retryable steps.
SET autocommit = 0;Use carefully; long-lived transactions can hold locks and increase contention.
Read and write contention patterns for transactional workloads.
Prevent concurrent modifications to matched rows inside a transaction.
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;Common in balance transfers, inventory reservations, and queue consumers.
SELECT * FROM products WHERE id = 10 FOR SHARE;Use shared locks when you want consistency without taking an exclusive update lock.
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;Isolation level affects phenomena such as dirty reads, nonrepeatable reads, and phantoms.
SHOW ENGINE INNODB STATUS\GA classic source of deadlock and locking information during production troubleshooting.
SET SESSION innodb_lock_wait_timeout = 10;Shorter timeouts are often helpful in APIs that should fail fast under contention.