The basic elements of mysql transactions and isolation levels

Note: just a personal understanding of notes, does not have the authority accuracy

The basic elements of mysql transactions and isolation levels

A. The four basic elements of the transaction

Atomic: the operations in a transaction are either executed or rolled back

2. Consistency: A and B 500 you deposit, mutual transfers total 1000 remain unchanged, there are requirements for developers, can not write error logic

3. Isolation: multi-transaction concurrency, to ensure that each transaction without disturbing each other, there are four isolation levels, the trade-off between concurrency and interference (requires in-depth knowledge learning lock)

4. Persistence: Once committed transaction, data is persisted to the database must not be rolled back unless the transaction doing the opposite, mysql achieved through the log.

II. Transaction isolation level

1. Read Uncommitted

A transaction reads to update the contents of B uncommitted transactions

Disadvantage: if a rollback B, A are read into the intermediate state B, can cause dirty read

2. Read Committed

A transaction can read updates to B transaction commits,

Pros: Because only you can read the contents of B submitted, so to solve the problem of dirty read 1

Disadvantages: The results before and after the transaction A query may be inconsistent, can cause non-repeatable read

3. Repeatable read

A snapshot transaction reads a record is always the first query, regardless B update transaction commits, will not affect the results of A

Pros: A read is because the first query snapshot, so to solve the problem of non-repeatable reads 2, it will not appear dirty reads 1

Drawback: B insert a new record A transaction is less than the query, but additions and deletions A change of affairs will read the latest recording, can cause phantom read

Extended: Repeatable read under lock gap (Gap Lock) You can lock a range to prevent data inserted to prevent some phantom read (need to continue to learn in depth knowledge lock)

4. serialized

A query data, B data can be queried, but B can not be falsified data soared

Advantages: avoiding the phantom read 3, of course, a dirty reads and non-repeatable read does not occur 2

Cons: No concurrent CRUD

III. Different problems caused by isolation

Dirty read: read someone else's content uncommitted

Non-repeatable read: read the contents of others update

Phantom read: not read the contents of other people insert or delete, but additions and deletions to the presence of Shique

IV. Operation

1. Review the current session and global isolation level

select @@global.tx_isolation, @@tx_isolation;

2. Set the current global or session isolation level

SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL

{

REPEATABLE READ

| READ COMMITTED

| READ UNCOMMITTED

| SERIALIZABLE

}

 

 

Published 42 original articles · won praise 25 · views 70000 +

Guess you like

Origin blog.csdn.net/qq812858143/article/details/103285503