MySQLInnoDB transactions and locks _ MySQL

Source: Internet
Author: User
MySQLInnoDB transaction and lock details bitsCN.com

Question: Why is transaction introduced?

1>.Data integrity

2>.Data security

3>.Make full use of system resources to improve the system's concurrent processing capability

1.Transaction features

Transactions have four features: Atomiocity, Consistency, Isolation, and Durability.

1.1Atomicity

A transaction is the logical unit of work of a database. all operations in a transaction are either done or not done.

1.2Consistency

The result of transaction execution must be that the database changes from a consistent state to another consistent state.

1.3Isolation

The execution of a transaction cannot be disturbed by other transactions. That is, the internal operations of a transaction and the data used for other

Transactions are isolated, and the transactions executed concurrently do not interfere with each other.

1.4Durability

Once a transaction is successfully committed, modifications to data in the database are persistent. Other

Operations or faults should not affect the execution results.

2. MySQLInnoDBTransactions and locks in the engine

2.1 SELECT ...... LOCK IN SHARE MODE

Add a shared lock to the data searched in the session transaction. If the data searched in the session transaction has been exclusive locked by other session transactions, the shared lock will wait for the end of the transaction to be added. if the wait time is too long, the lock wait timeout required by the transaction will be displayed.

2.2 SELECT ..... FOR UPDATE

When a read update is added to the data searched in a session transaction, other session transactions cannot be added with other locks and must wait until the end of the transaction.

2.3 INSERT, UPDATE, DELETE

The session transaction adds an exclusive lock to the data operated by the DML statement, and the transactions of other sessions will wait for it to release the exclusive lock.

2.4 gap and next key locks(Gap Lock)

The InnoDB engine automatically adds a gap Lock (or range lock) to the shared lock, update lock, and exclusive lock in session transactions when they need to be added to a range value domain ), lock non-existing data to prevent Phantom writing.

Note:

The situations described in 2.1, 2.2, 2.3, and 2.4 are also related to the transaction isolation level set by MySQL.

3.Four transaction isolation modes

3.1 READ UNCOMMITED

Dirty reads are allowed during the SELECT statement, that is, the SELECT statement reads data modified by other transactions but not committed.

3.2 READ COMMITED

The SELECT statement cannot be read repeatedly, that is, the same query statement is executed twice in the same transaction, when the data queried by other transactions is modified and committed, the data read twice is inconsistent.

3.3 REPEATABLE READ

The SELECT statement can be read repeatedly, that is, the same query statement is executed twice in the same transaction, and the data obtained is always consistent.

3.4 SERIALIZABLE

The only difference from repeatable reads is that the normal SELECT statement is changed to SELECT… by default .... Lock in share mode. That is, it adds sharing to the data involved in the query statement, blocking other transactions from modifying real data.

4.Example of verifying transactions and locking

Next, we will use the InnoDB engine in MySQL to explain how to implement the ACID feature and the impact between transactions at different isolation levels. Example table structure:

Create table 'account '(

'Id' int (11) not null AUTO_INCREMENT,

'Vaccount _ id' varchar (32) not null,

'Gmt _ create' datetime not null,

Primary key ('id '),

KEY 'idx _ vaccount_parameter_vaccountid' ('vaccount _ id ')

) ENGINE = InnoDB AUTO_INCREMENT = 1 default charset = utf8 COLLATE utf8_general_ci;

Then, we can write 10 million accounts with reasonable creation date distribution to the table account to facilitate testing.

Tx_isolation : Set global tx_isolation = 'read-uncommitted' ID Transaction 1 Transaction 1 output Transaction 2 Transaction 2 output1 start transaction; 2 SELECT VACCOUNT_ID from account where ID = 1001; caimao101510 start transaction; 3 UPDATE account set VACCOUNT_ID = 'uncommitted' where ID = 1001; 4 SELECT VACCOUNT_ID from account where ID = 1001; uncommitted5SELECT VACCOUNT_ID from account where ID = 1001; uncommitted 6 ROLLBACK; 7 SELECT VACCOUNT_ID from account where ID = 1001; caimao101510 8 COMMIT; Tx_isolation : Set global tx_isolation = 'read-committed' ID Transaction 1 Transaction 1 output Transaction 2 Transaction 2 output1 start transaction; 2 SELECT VACCOUNT_ID from account where ID = 1001; caimao101510 3 start transaction; 4 UPDATE account set VACCOUNT_ID = 'uncommitted' where ID = 1001; 5 SELECT VACCOUNT_ID from account where ID = 1001; uncommitted6SELECT VACCOUNT_ID from account where ID = 1001; caimao101510 7 COMMIT; 8 SELECT VACCOUNT_ID from account where ID = 1001; uncommitted 9 COMMIT; Tx_isolation : Set global tx_isolation = 'repeatable-read' ID Transaction 1 Transaction 1 output Transaction 2 Transaction 2 output1 start transaction; 2 SELECT VACCOUNT_ID from account where ID = 1001; caimao101510 3 start transaction; 4 UPDATE account set VACCOUNT_ID = 'uncommitted' where ID = 1001; 5 SELECT VACCOUNT_ID from account where ID = 1001; uncommitted6SELECT VACCOUNT_ID from account where ID = 1001; caimao101510 7 COMMIT; 8 SELECT VACCOUNT_ID from account where ID = 1001; caimao101510 9 COMMIT; Tx_isolation : Set global tx_isolation = 'serializable' ID Transaction 1 Transaction 1 output Transaction 2 Transaction 2 output1 start transaction; 2 SELECT VACCOUNT_ID from account where ID = 1001; caimao101510 3 start transaction; 4 UPDATE account set VACCOUNT_ID = 'uncommitted' where ID = 1001; STATE: updating5SELECT VACCOUNT_ID from account where ID = 1001; caimao101510 TRANSACTION 2 timeout 6 COMMIT; 7 start transaction; 8 UPDATE account set VACCOUNT_ID = 'uncommitted' where ID = 1001; 9 start transaction; 10 SELECT VACCOUNT_ID from account where ID = 1001; STATE: statistics11 transaction 2 timeout 12 commit; Tx_isolation : Set global tx_isolation = 'repeatable-read' ID Transaction 1 Transaction 1 output Transaction 2 Transaction 2 output1 start transaction; 2 select max (ID) FROM account; 124999 3 start transaction; 4 UPDATE account set gmt_create = date_add (gmt_create, interval + 1 day) where id> = 124999; 5 insert into account (VACCOUNT_ID, gmt_create) values ('ugene ', now (); STATE: update6 TRANSACTION 2 timeout 7 start transaction; 8 SELECT * FROM account where id = 124998; 2007-10-20 13: 479 UPDATE account set gmt_create = date_add (gmt_create, interval + 1 day) where id = 124998; execution successful 10 SELECT * FROM account where id = 124998; 2007-10-21 13: 4711 COMMIT; 12 COMMIT; 1 start transaction; 2 UPDATE account set gmt_create = date_add (gmt_create, interval-1 day) WHERE gmt_create> '2017-07-01 '; 3 start transaction; 4 SELECT * FROM account WHERE gmt_create> '2017-07-10 'LIMIT 1; 2009-10-2 13: 2009 SELECT * FROM account WHERE gmt_create> '2017-07-10' LIMIT 1; 2009-10-1 STATE: update6 insert into account (VACCOUNT_ID, gmt_create) values ('gmt _ create_test ', now (); 7 transaction 2 timeout 8 COMMIT; 9 SELECT * FROM account WHERE gmt_create> '2017-07-10 'LIMIT 1; 2009-10-1 Update transactions without indexing conditions1 start transaction; UPDATE account set gmt_create = date_add (gmt_create, interval-1 day) WHERE gmt_create> '2017-07-01 'AND gmt_create <'2017-07-10'; start transaction; insert into account (VACCOUNT_ID, gmt_create) values ('gmt _ create_interval ', now (); Transaction 2 times out COMMIT;

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.