Four characteristics of the transaction and isolation levels

1. The characteristics of the four transactions (ACID): abbreviation for the four basic elements of the database transaction properly executed. Comprising: Atomicity (Atomicity), consistency (Consistency), isolation (Isolation), persistent (Durability). Support a transaction (Transaction) database will need to have these four characteristics, or in the process of which the transaction (Transaction processing) can not guarantee the accuracy of the data, the transaction process is likely to meet the requirements for counterparties.

1.1 Atomicity (Atomicity)

The first atomic, this is the easiest. He said that all operations together make up an atom within a package of things, either all succeed, or all fail. This is the most basic features to ensure that the database because other factors lead to abnormal or downtime.

1.2 Consistency (Consistency)

Second consistency, this is the most misunderstood of all, a lot of blog like to use the example of a bank transfer has been speaking of the so-called agreement is based on atomic.

Atomicity only guarantee the identity of all operations in one thing, it was all life and death, will not you die, I'm still alive. However, there is no guarantee atomicity and everyone living together the same time, we die together. Computer instruction is a sequential, so it decided to submit a thing, will go through a process of time, so if something were to submit a half, I read a database, will read the intermediate results?

In order to prevent such a situation, the consistency of the database before and after the things it provides for submission of things, always will be, there may be state before and after the submission of the state of things and things submission, from a consistent state to another consistent state, but can not state occurs in the middle of the process. That something is the result of the implementation of a quantum state, rather than a linear state.

And the database will be submitted to a process of things, if the time of filing, there is a time lag, in the first seconds of submission, a deletion process is not yet complete to the third seconds to complete, will visit the first seconds of the third seconds people visit a different result? Inconsistent, the state of chaos? This is the only guarantee consistency was there before and after the state of the state, it will not appear intermediate states.

1.3 Isolation (Isolation)

Isolation of things, based on atomic and consistency, as things are atomized, quantized, so that things can be in the form of a plurality of packets of atoms concurrently, but each thing interference.

However, since more things could operate the same resources, different things in order to ensure isolation, there will be many lock program, of course, is to achieve the database, how to achieve them, we do not have to get to the bottom.

1.4 Persistent (Durability)

Persistent, after submission of a thing, the database state has changed forever, as long as this thing has submitted, even if submitted after the downtime, he indeed submitted, does not appear because

Just goes down and let the submission does not take effect, to submit something, he is like indelible tattoo, cured forever, unless you destroy the hard drive.

2. Do not consider the issue of isolation that may arise

 

Now highlight the isolation under the transaction, when multiple threads (or more clients) are data transaction operation database open, the database system to be able to isolate the operation to ensure that each thread gets accuracy of the data.

 

If you do not consider the transaction isolation, the types of problems occur:

 

2.1 dirty read

 

Dirty read means to read another data uncommitted transaction is a transaction in process.

 

2.2 Non-repeatable read

 

Non-repeatable read refers to a database of data, multiple queries within a single transaction but return different data values, which is due in the course of the transaction, the data is modified and submitted another transaction.

 

2.3 Magic Reading

 

Magic Reading is a phenomenon that occurs when a transaction non-independent execution. For example, transaction T1 to a field in a table of all the lines do the operation from "1" to "2", then the transaction T2 and a new record is inserted, and the value of this field is "1" and committed to the database. At this time, the operation transaction T1 users to see if we just modify the data, you will find there is a line not modify, in fact, this line is added from the transaction T2, like hallucinations, like, this is the magic had read.

 

Phantom reads and non-repeatable read are read another transaction has been submitted, the difference is non-repeatable read query data items are the same, but the magic of reading for a group of data (such as the number of data) .

 

3. The transaction isolation level

The Read Uncommitted 3.1 : read data has not been submitted: Which problems can not be solved

The Read committed 3.2 : read the data already submitted: You can solve dirty reads ---- oracle, sql server, postgresql default

Repeatable the Read 3.3 : Rereading read: can solve dirty reads and non-repeatable read --- mysql default

3.4 serializable: serialization: dirty reads to solve non-repeatable read and dummy read lock table corresponds ---

The four isolation levels above the highest Serializable, the lowest level is Read uncommitted. Of course, the higher the isolation level, the lower the efficiency.

 

01: Mysql default isolation levels are: Repeatable read: Repeatable read;

02: oracle database only support seralizable (serialized) level and Read committed (); Read committed is the default level;

Design on the scene below four isolation levels:

01: Read uncommitted read uncommitted; the company wages, leading to 5000 yuan hit singo account, but the transaction has not been submitted, but singo just to view account and found that wages have been credited into account, is 5,000 yuan a whole, very happy. But unfortunately, the leadership of the amount of wages issued singo found wrong, is $ 2000, then quickly roll back the transaction, the revised amount, will commit the transaction, real wages last singo only 2,000 yuan, singo futile.

02: Read committed Read Committed; singo is paid cards to spend, the system reads the card does have $ 2000, at a time when her wife also happens to online transfers, to pay 2000 yuan singo card to another account and submit before singo transaction, when singo debit, the system checks to pay singo card has no money, a failed charge, singo very puzzled, obviously Cary money, why ......

03: Repeatable read Repeatable read when singo is paid cards to spend, once the system starts reading the payroll card information (ie, the transaction began), singo wife can not modify the record, that is not in this singo wife when the transfer.

04: repeatable read may occur phantom reads: singo wife work in the banking sector, she often see credit card records singo by the bank's internal systems. One day, she was to query the total amount of consumption singo month credit card (select sum (amount) from transaction where month = month) is 80 yuan, while singo after this time just outside Hu eat Hesse pay at the cash register, consumer $ 1000, that is 1,000 yuan a new consumption record (insert transaction ...), and submit the transaction, then singo wife will print singo month credit card details to A4 paper, but found that total spending for the 1080 yuan , singo wife was surprised, thinking hallucinations, phantom reads thus produced.

Serializabale: highest transaction isolation level, the cost of the highest cost, low performance, rarely used, at this level, the branch order to avoid the above situation arising.

Guess you like

Origin www.cnblogs.com/codebj/p/11031813.html