Test your understanding of MySQL transactions and isolation levels.
I have written an article titled MySQL InnoDB Transaction isolation level and dirty read, Repeatable read, phantom read
Http://blog.csdn.net/seven_3306/article/details/27085275
This is a question I have figured out about MySQL transaction and isolation level. Please fill in the question mark section:
If you can correctly explain the following results, you should have a certain understanding of MySQL transactions and isolation levels.
If not, you may not understand the transaction and isolation level of MySQL.
In addition, this question helps you understand why this problem occurs sometimes:
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
After understanding the cause of the error, you can solve the problem.
MySQL database has tables
Create table t (a int) engine = innodb, charset = utf8;
Insert into t (a) values (0 );
MySQL runs the following command in two sessions in the following order:
Session |
Session B |
|
|
Set session transaction isolation level read committed; |
|
Set autocommit = 0; |
Set session transaction isolation level read committed; |
|
Set autocommit = 0; |
SELECT a FROM t; |
|
Result: 0 |
|
|
|
|
SELECT a FROM t; |
|
Result: 0 |
|
|
|
|
UPDATE t SET a = a + 1; |
|
SELECT a from t; |
|
Result :? |
|
|
|
|
|
|
SELECT a from t; |
|
Result :? |
|
|
|
UPDATE t set a = a + 5; |
|
Result:
|
|
|
|
|
COMMIT; (ROLLBACK) |
|
|
SELECT a FROM t; |
|
Result :? (If session A is rollback, It Is ?) |
|
|
|
COMMIT |
|
|
|
SELECT a FROM t; |
|
Result :? (If session A is rollback, It Is ?) |
SELECT a FROM t; |
|
Result :? (If session A is rollback, It Is ?) |
|
The result is:
Session |
Session B |
|
|
Set session transaction isolation level read committed; |
|
Set autocommit = 0; |
Set session transaction isolation level read committed; |
|
Set autocommit = 0; |
SELECT a FROM t; |
|
Result: 0 |
|
|
|
|
SELECT a FROM t; |
|
Result: 0 |
|
|
|
|
UPDATE t SET a = a + 1; |
|
SELECT a from t; |
|
Result: 1 |
|
|
|
|
|
|
SELECT a from t; |
|
Result: 0 |
|
|
|
UPDATE t set a = a + 5; |
|
Result: The lock is blocked. Wait for session A to commit the transaction, that is, wait for session A to release the lock. 1. If session A does not release the lock after a certain period of time (committing a transaction ), Session B times out due to Lock wait: ERROR 1205 (HY000): Lock wait timeout exceeded; Try restarting transaction 2. If session A commits a transaction before session B's lock wait timeout, session B executes update a = A + 1; Innodb Lock wait time: show variables like '% innodb_lock_wait_timeout %'; (Global, Session level) 3. If session A executes rollback before session B lock wait timeout, session B executes update a = a + 1;
|
|
|
|
|
COMMIT; (ROLLBACK) |
|
|
SELECT a FROM t; |
|
Result: (1) an error is returned. Ii. 6. If session A is rollback, the value is 5) |
|
|
|
COMMIT |
|
|
|
SELECT a FROM t; |
|
Result: (1) an error is returned. Ii. 6. If session A is rollback, the value is 5. |
SELECT a FROM t; |
|
Result 1: If session B lock wait times out, the value is 1. 2. If session B does not time out and session A commits A transaction, it is 6 3. If session A is rollback, the value is 5) |
|
What is the result of changing to another transaction isolation level?