Home > Database > Mysql Tutorial > body text

Detailed explanation of transactions in MySQL

黄舟
Release: 2017-09-11 11:39:06
Original
1386 people have browsed it

MySQL transaction:

1.MySQL transaction control statement

(1).Open transaction

begin;
Copy after login

(2).Submit transaction

commit;
Copy after login

(3).Rollback the transaction

rollback;
Copy after login

(4).Set whether the transaction is automatically committed

set autocommit = {0 | 1}; // 0不自动提交,1自动提交
Copy after login

For example:

begin;// 开启事务
insert into table_name values(内容);
commit;// 提交事务
Copy after login

This is a simple transaction that inserts data

begin;// 开启事务
insert into table_name values(内容);// 后悔了,不想插入数据了
rollback;// 回滚,取消这次的事务操作
Copy after login

This is a simple rollback transaction that inserts data

2.MySQL transaction isolation statement

View the isolation level of MySQL

show variables like 'tx_isolation';
Copy after login

(1). read-uncommitted (read uncommitted content)

Dirty reading (problems caused);

The content of a user's two query results is different.

Set the default isolation level (you need to log in again after the setting is completed):

set global transaction isolation level read uncommitted;
Copy after login

Two users log in to the database at the same time and start transactions respectively. One user's transaction performs addition, deletion and modification operations, and the other user's transaction only performs query operations. The former performs the operation without committing, and the latter can see the operation results. However, if the former rolls back the transaction at this time, the latter's query will see different results from the last time (also That is, the result of no operation), this is the dirty read problem.

(2).read committed (read the submitted content)

Transactions can only see the changes made by the committed transaction. The same query may return different results, which causes non-repeatable read problems.

Set the default isolation level (you need to log in again after the setting is completed):

set global transaction isolation level read committed;
Copy after login

In a transaction, I saw this Two different query results (similar to dirty read).

(3).repeatable read(repeatable read)

can ensure that multiple instances of the same transaction read data concurrently , you will see the same data row (Mornar isolation level of the database).

Set the default isolation level (you need to log in again after the setting is completed):

set global transaction isolation level repeatable read;
Copy after login

That’s it once During a transaction, no matter how another user modifies the data, the results queried in this transaction are always the same.

***Implicit transaction commit***:

alter table users engine = innodb;// 会造成事务的隐式提交
Copy after login

3. InnoDB lock mechanism

(1). There are no primary keys and indexes in a table. A transaction will lock all records in the table, and other transactions cannot operate the table.

(2). If there is a primary key or index in a table, a transaction will lock a certain row of data in the table that it operates, and will not lock the entire table. If other transactions operate on other data in the table, this time Not affected. It should be noted that the row-level lock here is a gap lock. That is to say, if you modify certain rows of data, such as modifying n rows of data with id<10.=, then, id = 11 The data will also be locked, which needs to be noted.

The above is the detailed content of Detailed explanation of transactions in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template