Four levels of isolation characteristics and Mysql database transactions

A. Four characteristics

1. The concept of transaction

Transaction (Transaction) refers to a sequence of operations, a plurality of operating the operation sequence either do or do not, is an indivisible unit of work, the database environment logic operation unit, the transaction by the DBMS management subsystem is responsible for handling affairs.

There are currently used storage engine InnoDB (the default storage engine after MySQL5.5) and MyISAM (default before MySQL5.5 storage engine), which supports InnoDB transaction processing mechanism, MyISAM does not support.

2. Characteristics of the transaction

Transaction processing ensures that all operations in the sequence unless the transaction are completed successfully, or not permanently update the data - oriented resources. More reliable by the combination of a set of related operations into a sequence of either all succeed or all fail, you can simplify the error recovery and make the application.

But not all of the sequence of operations can be called the transaction because the transaction to be a sequence of operations that must be met transaction atomicity (Atomicity), consistency (Consistency), isolation (Isolation) and persistent (Durability) . The four properties, referred to as the ACID properties.

 

 

1. Atomicity:

Atom is the smallest particles in nature, has a characteristic can not be divided. All operations in the transaction can be seen as an atomic transaction application is not subdivided minimal logic execution thereof.

Use transaction data to modify the sequence of operations, either all executed or not executed whole. In general, the operation of a transaction have a common goal, and are interdependent. If the database system only perform some of these operations, it may undermine the overall objective of affairs, and eliminates the possibility of atomic systems only deal with part of the operation.

 

2. Consistency:

Consistency refers to the result of the execution of a transaction from the database must be consistent state to another consistent state variable. When the database contains only the outcome of the transaction successfully committed, the database is in a consistent state. Consistency is ensured by atomicity.

For example: When making transfers, only to ensure the roll-out and consistent transfer of money to make up the transaction. That happened and the total data still match before the transaction occurs.

 

3. Isolation:

Isolation refers to the interfering each transaction performed, the internal operation of any transaction other concurrent transactions, is isolated. In other words: between concurrently executing transactions can neither see each other in the middle of the state, can not influence each other.

For example: When making transfers, account only if A and B turn out into the account to see success after the operations are performed to reduce the amount of A accounts as well as an increase in the amount of B account. And other matters that can not produce any effect of the transaction for the transfer operation.

 

4. endurance:

Persistence refers to transactions Once submitted, any changes made to the data, must be recorded in the permanent memory is usually saved to the physical database, even if the database fails, the data submitted should be able to recover. But if the database fails due to external causes, such as the hard disk is damaged, then the data previously submitted are likely to be lost.

 

II. Transaction isolation level

Concurrency issues affairs

Dirty read (Dirty read)

: When a transaction is accessing data, and the data has been modified, and this modification has not been submitted to the database, then another transaction also access this data and then use this data. Because this data is not submitted, then another transaction read this data is "dirty data", based on what you did "dirty data" may be incorrect.

Non-repeatable read (Unrepeatableread):

It refers to a transaction in reading the same data multiple times. When this transaction is not over, another transaction also access the data. So, between the two read data in the first transaction, due to the modification of the second transaction led to the first transaction data may be read twice not the same. This happened twice within a transaction read data is not the same situation, so called non-repeatable read.

Magic Reading (Phantom read):

Magic Reading and unrepeatable reads the like. It occurs in a transaction (T1) is read several lines of data, followed by another concurrent transaction (T2) is inserted into some of the data. In the following query, the first transaction (T1) will find more than a few original records do not exist, as if the same happened hallucinations, so called phantom reads.

Non-repeatability and distinction phantom read:

Non-repeatable read focus is modified, the new focus is phantom read or delete.

Solve the problem of non-repeatable read only lock to meet the conditions of the line, the need to solve the phantom read lock table

Example 1 (the same condition, you had to read the data, read out again discovered the value is not the same): 1 Mr A in the affairs of their wages for the operation of reading 1000 is not yet complete, Mr. B in the affairs 2 a modification to the wage of 2000, the result for a read their wages into a 2000 salary; this is non-repeatable read.

例2(同样的条件, 第1次和第2次读出来的记录数不一样 ):假某工资单表中工资大于3000的有4人,事务1读取了所有工资大于3000的人,共查到4条记录,这时事务2 又插入了一条工资大于3000的记录,事务1再次读取时查到的记录就变为了5条,这样就导致了幻读

事务的隔离级别

事务的隔离级别用于决定如何控制并发用户读写数据的操作。数据库是允许多用户并发访问的,如果多个用户同时开启事务并对同一数据进行读写操作的话,有可能会出现脏读、不可重复读和幻读问题,所以MySQL中提供了四种隔离级别来解决上述问题。

事务的隔离级别从低到高依次为 READ UNCOMMITTED、READ COMMITTED、REPEATABLE READ以及SERIALIZABLE,隔离级别越低,越能支持高并发的数据库操作。

 

2.在spring注解开发中,使用 isolation 表示事务的隔离级别

 

Guess you like

Origin www.cnblogs.com/arsn/p/11301043.html