MySQL (two) MySQL transaction


MySQL transaction

First, the basic introduction affairs

1, concepts:

如果一个包含多个步骤的业务操作,被事务管理,要么同时成功,要么同时失败。

2, the operation

	(1)开启事务:start transaction;
	(2)回滚:rollback;
	(3)提交:commit;

3, Example: Joe Smith John Doe to transfer 500 yuan

  -- 0.开启事务
    start transaction;
    --1.张三账户-500
    update account  set balance =balance-500 where name="zhangsan"
    --2.李四账户+500
     update account  set balance =balance+500 where name="lisi"
     --3.出现异常,回滚
     rollback;
     --4.无异常,提交
     commit;

4, MySQL database default auto-commit transaction

4.1, two ways of transaction commit:

1. Auto submit
oracle default manual submission
mysql is automatically submitted
a DML (additions and deletions) statement will automatically submit a transaction.
2. Manually submit
first open transaction, and then submitted

4.2, modify the default transaction submission:

     1.查看事务的默认提交方式:
         select @@autocommit;
         1 代表自动提交
         0 代表手动提交 
     2.修改默认提交方式(修改成手动)
         set @@autocommit = 0;  

Second, the transaction four properties (ACID)

  • Atomicity
  • consistency
  • Isolation
  • Endurance

1, atomicity (Atomicity)

Atomicity refers to all operations in the transaction included either all succeed, or all fail rolled back.
Therefore, the operation of the transaction, if successful, it must be fully applied to the database if the operation fails it can not have any impact on the database.

2. Consistency (Consistency)

Consistency refers to the transaction must make the database from one consistent state transitions to another consistent state ;
that is to say the implementation of a transaction prior to and implementation of the post must be in consistent state.
For example:
assume that both user A and user B's money add up to a total of 1000, then no matter how transfers between A and B, turn several accounts, after the end of the transaction the money add up to two users should have to be 1000, this is transactional consistency.

3, isolation (Isolation)

Isolation when a plurality of users concurrent access to the database, such as when operating simultaneously with a table, a database for each user transaction open, operation not being disturbed by other transactions, to be isolated from each other between the plurality of concurrent transactions.

4, persistent (Durability)

Persistence means that once a transaction is committed, then changes to the data in the database is permanent, it commits the transaction will not be lost even in the case of a database system experienced a failure of the operation.

Third, the isolation level

The higher the isolation level, the more it can ensure the integrity and consistency of the data, but the impact on concurrent performance is also greater.
MySQL's default isolation level is Repeatable read. Repeatable read

Database transaction isolation levels are four, from low to high order:

1, Read uncommitted (not authorized to read, read uncommitted)

If a transaction has been started writing data, another transaction is allowed to simultaneously write, but allows other transactions to read the trip data.
The isolation level can be "exclusive write locks" to achieve.
This avoids lost updates, but may appear dirty read .
Dirty read : That transaction B to read data transaction A uncommitted.

2, Read committed (authorized to read, read submission)

Read data transaction allows other transactions continue to access the rows of data, but write uncommitted transactions will prevent other transactions from accessing the row.
The isolation level to avoid dirty reads , but it may appear non-repeatable read.
Non-repeatable read : Transaction A pre-read data, transaction B immediately update the data and submit the transaction, while Transaction A reads the data again, the data has changed.

3, Repeatable read (repeatable read) (MySQL default)

Repeatable read refers to a transaction, reading the same data multiple times.
When this transaction is not over, another transaction also access the same data.
Then, read data between the two in the first transaction even if the second transaction to modify the data, the first two read data transaction is the same.
This occurs twice in a data read transaction is the same, so called repeatable read.
Read data transaction would be prohibited by a write transaction (but allowing the read transaction), the write transaction to prohibit any other transactions.
This avoids the non-repeatable reads and dirty reads, but sometimes may appear phantom read.
(Read transaction data) that can be "shared read locks" and "exclusive write locks" to achieve by.

4, Serializable (serialization)

Provide strict transaction isolation.
It requires serialization of the transaction, a transaction can be performed by one, rather than concurrently.
If only "row-level locking" can not be achieved transaction serialization, it must ensure that the newly inserted data is not just execute a transaction query access to through other mechanisms through.
Serialization is the highest transaction isolation level, but the price is also the most expensive, low performance, rarely use, under that level, the branch order, not only to avoid dirty reads, non-repeatable read, but also to avoid the phantom read.

Fourth, the optimistic and pessimistic locking lock

Pessimistic locking, someone wants to harm I
optimistic locking, Justice has long arms

Published 44 original articles · won praise 5 · Views 889

Guess you like

Origin blog.csdn.net/qq_40634246/article/details/104734334