Managing Transactions in MySQL
Ramkumar Lakshminarayanan
Copyright HP Education The World’s Most Popular Open Source Database 1
Transactions
In almost all applications that access MySQL
databases, multiple users concurrently attempt to
view and modify data. The simultaneous operations
may result in data that is inconsistent and
inaccurate. Using transactions avoid these problems
by isolating each operation.
Copyright HP Education The World’s Most Popular Open Source Database 2
In the context of SQL and relational databases, a transaction
is a set of one or more SQL statements that perform a set
of related actions. The statements are grouped together and
treated as a single unit whose success or failure depends on
the successful execution of each statement in the
transaction.
In addition to ensuring the proper execution of a set of SQL
statements, a transaction locks the tables involved in the
transaction so that other users cannot modify any rows that
are involved in the transaction.
Copyright HP Education The World’s Most Popular Open Source Database 3
Copyright HP Education The World’s Most Popular Open Source Database 4
MySQL provides the START TRANSACTION to create a
transaction, and COMMIT, ROLLBACK statements to
end it. The COMMIT statement saves changes to the
database, and the ROLLBACK statement will undo any
changes made during the transaction and database is
reverted to the pre-transaction state.
Copyright HP Education The World’s Most Popular Open Source Database 5
The START TRANSACTION Statement
The START TRANSACTION statement requires no
clauses or options:
START TRANSACTION
START TRANSACTION statement notifies MySQL that
the statements that follow should be treated as a unit,
until the transaction ends, successfully or otherwise.
Note: A BEGIN statement can also be used to start a
transaction.
Copyright HP Education The World’s Most Popular Open Source Database 6
The COMMIT Statement
The COMMIT statement is used to terminate a
transaction and to save all changes made by the
transaction to the database. There are no additional
clauses or options:
COMMIT
The following transaction is made of two INSERT
statements, followed by a COMMIT statement:
Copyright HP Education The World’s Most Popular Open Source Database 7
START TRANSACTION;
INSERT INTO Studio VALUES (101, 'MGM Studios');
INSERT INTO Studio VALUES (102, 'Wannabe
Studios');
COMMIT;
SELECT * FROM Studio;
Ensure the table type is as of InnoDB before this trying
this example.
Copyright HP Education The World’s Most Popular Open Source Database 8
START TRANSACTION;
UPDATE Studio SET title = 'Temporary Studios'
WHERE id = 101;
UPDATE Studio SET title = 'Studio with no buildings'
WHERE id = 102;
SELECT * FROM Studio;
ROLLBACK;
SELECT * FROM Studio;
Copyright HP Education The World’s Most Popular Open Source Database 9
Adding Savepoints to Your Transaction
The SAVEPOINT and ROLLBACK TO SAVEPOINT
statements isolate portions of a transaction. The
SAVEPOINT statement defines a marker in a transaction,
and the ROLLBACK TO SAVEPOINT statement allows
you to roll back a transaction to a predetermined marker
(savepoint).
Copyright HP Education The World’s Most Popular Open Source Database 10
Start Transactions;
SAVEPOINT savepoint1
INSERT INTO Studio VALUES (105, 'Noncomformant
Studios');
INSERT INTO Studio VALUES (106, 'Studio Cartel');
SELECT * FROM Studio;
ROLLBACK TO SAVEPOINT savepoint1;
INSERT INTO Studio VALUES (105, 'Moneymaking
Studios');
INSERT INTO Studio VALUES (106, 'Studio Mob');
SELECT * FROM Studio;
COMMIT;
Copyright HP Education The World’s Most Popular Open Source Database 11
A dirty read can take place when:
Transaction A modifies data in a table.
Around the same time, another Transaction B reads the table,
before those modifications are committed to the database.
Transaction A rolls back (cancels) the changes,
returning the database to its original state.
Transaction B now has data inconsistent with the
database.
Worse, Transaction B may modify the data based on its initial
read, which is incorrect or dirty read.
Copyright HP Education The World’s Most Popular Open Source Database 12
Isolation Level
The transaction isolation level determine the degree to
which other transactions can “see” details inside an
in-progress transaction and are arranged in
hierarchical order.
Serializable
Repeatable Read
Read Committed
Read Uncommitted
Copyright HP Education The World’s Most Popular Open Source Database 13
SERIALIZABLE — In the SERIALIZABLE isolation level
of MySQL, data reads are implicitly run with a read
lock
REPEATABLE READ
provides more isolation from a transaction, ensuring
that data reads are the same throughout the
transaction even if the data has been changed and
committed by a different transaction. The
SERIALIZABLE isolation level provides the slowest
performance but also the most isolation
Copyright HP Education The World’s Most Popular Open Source Database 14
READ COMMITTED provides some isolation and
slightly slower performance, because only committed
data changes are seen by other transactions.
However, READ COMMITTED does not address the
issue of data changing in the middle of a transaction.
READ UNCOMMITTED is the easiest isolation level to
implement and provides the fastest performance. The
problem with READ UNCOMMITTED is that it
provides no isolation between transactions.
Copyright HP Education The World’s Most Popular Open Source Database 15
The mysqld isolation level is set by the tx_isolation
system variable.
mysql> SHOW SESSION VARIABLES LIKE ’tx_isolation’;
+---------------+------------------+
| Variable_name | Value |
+---------------+------------------+
| tx_isolation | READ-UNCOMMITTED |
+---------------+------------------+
1 row in set (0.00 sec)
Copyright HP Education The World’s Most Popular Open Source Database 16
mysql> SELECT @@session.tx_isolation;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| READ-UNCOMMITTED |
+------------------------+
1 row in set (0.00 sec)
mysql> SELECT VARIABLE_NAME, VARIABLE_VALUE
-> FROM INFORMATION_SCHEMA.SESSION_VARIABLES
-> WHERE VARIABLE_NAME=’tx_isolation’;
Copyright HP Education The World’s Most Popular Open Source Database 17
the server (global) or connection (session) isolation
level you can execute either of the
following SET commands:
SET @@{global|session}.tx_isolation= {read-
uncommitted|
read-committed|repeatable-read|serializable}
SET {GLOBAL | SESSION} TRANSACTION
ISOLATION LEVEL {READ
UNCOMMITTED | READ COMMITTED |
REPEATABLE READ | SERIALIZABLE}
Copyright HP Education The World’s Most Popular Open Source Database 18