|
Home >> FAQs/Tutorials >> MySQL Tutorials >> Index
MySQL FAQs - Transaction Management: Commit or Rollback
By: FYIcenter.com
Part:
1
2
3
4
5
6
7
8
9
10
(Continued from previous part...)
How Long a Transaction Will Wait for a Data Lock?
If you issue a UPDATE or DELETE statement on a row that has an exclusive lock owned by another session,
your statement will be blocked to wait for the other session to release the lock. But the wait will be
timed out after the predefined innodb_lock_wait_timeout period, which is 50 seconds by default.
The tutorial exercise below shows what will happen if a transaction has been blocked and the lock wait
timeout period has been reached:
(session 1)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> UPDATE fyi_links SET counts='777' where id=101;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
(Exclusive lock (X) placed on row id=101)
Switch to session 2:
(session 2)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> UPDATE fyi_links SET counts=888 where id=101;
(Blocked to wait for exclusive lock (X) on row id=101)
(50 seconds passed)
ERROR 1205 (HY000): Lock wait timeout exceeded;
try restarting transaction
(Transaction terminated with a rollback)
What Happens to Your Transactions When ERROR 1205 Occurred?
If your transaction receives the "Lock wait timeout exceeded" - ERROR 1205,
MySQL server automatically terminates your transaction and rolls back your data changes of the entire transaction.
This is why the error messages tells you to "try restarting transaction".
Note that MySQL server will not touch the other transaction that holds the lock.
That means the lock will stay as is. If you repeat your transaction right away, it will be blocked again,
and may be timed out again, if the other transaction has not been committed or rolled back yet.
What Is a Dead Lock?
A dead lock is phenomenon happens between two transactions with each of them holding a lock that blocks
the other transaction as shown in the following diagram:
(transaction 1) (transaction 2)
update row X to create lock 1
update row Y to create lock 2
update row X
(blocked by lock 1)
update row Y
(blocked by lock 2)
(dead lock created)
How To Experiment Dead Locks?
If you want to have some experience with dead locks, you can create two windows running two mysql
transactions in two sessions at the REPEATABLE READ transaction isolation level. Then run some
UPDATE statements as shown in the tutorial exercise below:
(session 1)
mysql> SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Query OK, 0 rows affected (0.00 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> UPDATE fyi_links SET notes='Lock1' where id=101;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
(Exclusive lock (X) placed on row id=101)
Switch to session 2:
(session 2)
mysql> SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Query OK, 0 rows affected (0.00 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> UPDATE fyi_links SET notes='Lock2' where id=110;
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0
(Exclusive lock (X) placed on row id=110)
mysql> UPDATE fyi_links SET notes='Good1' where id=101;
(Blocked to wait for exclusive lock (X) on row id=101)
Switch to session 1:
(session 1)
mysql> UPDATE fyi_links SET notes='Good2' where id=110;
(Tried to wait for exclusive lock (X) row id=110)
ERROR 1213 (40001): Deadlock found when trying to get lock;
try restarting transaction
(Transaction terminated with a rollback)
Switch to session 2:
(session 2)
(Exclusive lock (X) on row id=101 released)
(Block is removed on this transaction is removed)
Query OK, 1 row affected (10.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
(Continued on next part...)
Part:
1
2
3
4
5
6
7
8
9
10
|