How to avoid the problem of concurrent transactions?

 

通过设置事务的隔离级别
v代表未解决,x代表已解决


                          Dirty read unrepeatable reads phantom read 
                          
    . 1 , the READ UNCOMMITTED √ √ √ 
    
    2 , the READ COMMITTED X √ √ 
    
    . 3 , the READ XX √ to REPEATABLE                   
    
    . 4 , SERIALIZABLE (serialization) XXX

 

Detailed:

Read uncommitted

A transaction can read data uncommitted transaction B

A problem might arise:

Dirty read: B things modification data a = 1, but not submitted, A things read data a = 1, A things get the display data, this time things B rollback, modify the data again a = 2, then submit it the actual data a = 2, but to get things a is a = 1.

 

Read committed

A transaction to wait after the transaction commits B to read data

A problem might arise:

Non-repeatable read: A reading things a = 1, A things a read again, this time modified B things a = 2, A things waiting to read a B submitted after this case a = 2, within a range of two transaction a return to the same query but different data.

 

Repeatable read

When repeated reading, data read is to start things in A (open transaction), B is not allowed to modify the operation of things (corresponding to the UPDATE operation is modified)

A data reading things a = 1, B at this time will not be allowed to modify the data object a, object A until finished, B things can perform data modify a.

A problem might arise:

Magic Reading: A reading things in the total amount of a whole table, due Repeatable read corresponds to the update operation, so insert new data, two things can be done simultaneously, which can lead to something A query again, the data is a happening Variety.

 

Serialization Serializable

Serializable transaction isolation level is the highest, at this level, execution serialization order of the transaction, to avoid dirty reads, non-repeatable read and phantom read. But this is inefficient transaction isolation level, database comparison consumption performance, is not generally used.

 

 

Most database default transaction isolation level is Read committed, such as Sql Server, Oracle.

Mysql default isolation level is Repeatable read.

 

reference:

https://www.cnblogs.com/ubuntu1/p/8999403.html

 

Guess you like

Origin www.cnblogs.com/sea-stream/p/11297934.html