Mysql transaction characteristics and level

First, what is a transaction?

   Transaction database (abbreviation: transaction) during the execution of a logical unit is a database management system, is constituted by a finite sequence of database operations.

Second, the four properties of transactions

Are atomicity, consistency, isolation, durability.

1, atomicity (Atomicity)

Atomicity refers to all operations in the transaction included either all succeed, or all fail rolled back, the operator 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 means that the transaction must transform the database from one consistent state to another consistent state, that is to say before and after a transaction execution and implementation must be in a consistent state. For example, if both user A and user B 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 the 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, for isolation between a plurality of concurrent transactions. About the transaction isolation provides a variety of database isolation levels, you will be introduced to later.

4, persistent (Durability)

Persistence means that once a transaction is committed, then changes to the data in the database is permanent, commit the transaction will not be lost even in the case of a database system experienced a failure of the operation. For example we use JDBC database operations, after the transaction is committed method, the user is prompted transaction is complete, when we complete the program execution until prompted, you can identify the transaction has been submitted correctly, even if this time the database there is a problem, it must To complete the full implementation of our business. Otherwise, it would result in significant errors while we see the prompt completion of the transaction, but the database is not because of failure to perform the transaction. This is not allowed.

Three, mysql isolation level 

READ UNCOMMITTED (uncommitted read)

In reading this uncommitted transaction level, a read transaction to another data uncommitted transaction.
Name field e.g. A transaction updates a field, but in uncommitted transaction B, reads the id of the record 1000 time, name value 'aaa', but not the commit transaction a, so name = 'aaa' there may be rolled back, then, the data a transaction reads uncommitted transactions called dirty read (dirty Read)

 

READ COMMITTED (read committed)

Read Committed This transaction level, a transaction can read the data after another transaction commits.

E.g. A transaction is read into the id of record 1000 of the name field is AAA, then B transaction at this time to update the name value of this record, and presented, this time A transaction reads the name again, then, name value is the bbb , so in one transaction, for a field several times to read, may get different values.

A two readings before and after the transaction is inconsistent!

 

REPEATABLE READ (repeatable read)

Repeatable read this transaction level, a duplicate transaction to read the field will not change.

A read transaction, for example, the id of the record 1000 of the name is AAA, then the transaction B changed name bbb, the transaction commits and B, A name read again when the transaction will not read bbb, so a transaction is equivalent to a separate world, outside of any changes will not affect the a transactions.

However, Repeatable read can lead to phantom read occurs, what is the phantom read it, for example:
A transaction query a table, only one record table, id is 1, but this time B insert a transaction data, id 2 , a transactions because they do not know the id 2 data, so this time a also inserted a data id 2, this time will certainly insert failed. this situation is magic reading

Note: MYSQL in innoDB solve the phantom read by MVCC (multi-version concurrency control), another MYSQL default transaction level is repeatable read, Oracle and SQL Server default isolation level is Read Committed (Read committed)

 

SERIALIZABLE (serializable)

Serializable transaction level to read each line of data are added to the lock

Locking a bit is to avoid dirty reads and phantom reads, and also avoids the possibility of non-repeatable read, but because the lock, and reduce a lot of concurrency, because the same time, only one thread can get to lock is also likely to lead to a lot timeout issues.

 

 

Summary: non-repeatable reads and phantom reads are easily confused, non-repeatable read focused on the modification, phantom reads focused on added or deleted. 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

 

Isolation level:

Isolation Levels Dirty read (Dirty Read) Non-repeatable read (NonRepeatable Read) Magic Reading (Phantom Read)
Uncommitted Read (Read uncommitted) may may may    
Read Committed (Read committed) impossible may may
Repeatable Read (Repeatable read) impossible impossible may
Serialization (the Serializable) impossible impossible impossible

The higher the level, the more secure the data, but the lower the performance.

 

Guess you like

Origin www.cnblogs.com/123-shen/p/11267655.html