The isolation level of things and things

Source: Internet
Author: User

What is a thing

A thing is a sequence of operations that accesses a database, and the database application system accesses the database through a set of things. the proper execution of a thing makes a database transition from one state to another .

Things must obey the acid principles established by ISO/IEC. Acid is an abbreviation for atomicity (atomicity), consistency (consistency), isolation (isolation), persistence (durability), and these four states mean:

1. Atomic Nature

Is indivisible, things are either all executed, or all are not executed. If all the children of a thing are committed successfully, all database operations are committed, the database state changes, and if there is a child failure, the database operation of the other child is rolled back, that is, the database returns to the state before the thing is executed, and no state transitions occur

2. Consistency

The execution of things makes the database transition from a correct state to another correct state

3, the isolation of

Before a thing is properly committed, it is not allowed to make changes to the data available to any other thing, that is, before the thing is properly committed, its possible results should not be shown to other things.

4. Persistence

When things are correctly committed, the results will be kept in the database forever, even if there are other failures after the things are committed, and the results of the processing will be saved.

The role of Things

Management of Things is critical for enterprise applications, which ensures that every operation is reliable, even if there is an unusual access condition that does not disrupt the integrity of the background data. Just like ATM ATMs in banks, usually ATMs can serve customers normally, but it is also unavoidable to encounter the operation and the sudden failure of the situation, at this time, things must ensure that the operation of the account before the failure to take effect, just as the user has not used the ATM machine at all, To ensure that the interests of users and banks are protected from loss.

Problems that can arise when things happen in parallel

For example, things A and B manipulate the same resource, thing A has several sub-things, and thing B also has a number of sub-things, and things A and B in high concurrency, there will be a variety of problems. "A variety of problems," summed up the main is five: the first category of missing updates, the second category of missing updates, dirty read, non-repeatable read, Phantom read. Of the five, the first category of missing updates, the second category of missing updates is not important, do not talk about dirty reading, non-repeatable reading and phantom reading.

1. Dirty Reading

The so-called dirty reading, refers to the thing a read the thing B has not submitted data , such as the bank to take money, things a open things, at this time to switch to things B, things B open things--take 100 yuan, at this time to switch back to things a, things a read is the database of the original data, Because things B took away 100 dollars, and did not submit, the database inside the balance of accounts must be the original balance, this is dirty read.

2. Non-repeatable reading

The so-called non-repeatable reading, that is, in a thing read two times a certain data, read out the inconsistent data . Or take money from the bank as an example, things a open things--to detect the bank card balance of 1000 yuan, at this time switch to things B things B open things--things B take away 100--and submit, the database inside the balance becomes 900 yuan, this time switch back to things a, Things a again check the account balance is 900 yuan, so for thing a, in the same thing two reads the account balance data inconsistent, this is not repeatable read.

3. Phantom Reading

The so-called Phantom Reading, refers to in the operation of a thing found the data is not manipulated . Like student information, thing a opens things up--modifies all students to sign in on the same day as false, switching to thing B, thing B, and thing b inserting a student data, switching back to thing a, when thing a commits, finds a piece of data that has not been modified by itself. This is the illusion of reading, as if there was a hallucination. The premise of Phantom reading is that there is a mistake in the Random House to insert and delete the operation.

Things isolation Level

The level of separation of things, is to solve the above several problems were born. Why there is a thing isolation level, because the higher the thing isolation level, the less problems will arise in concurrency, but the higher the performance consumption will be, so it is often necessary to make a tradeoff between concurrency and performance . So there are several isolation levels of things so that different projects can choose the right thing isolation level based on the concurrency of their projects, and compensate for concurrency problems that arise outside of the isolation level of things.

There are 4 things in the isolation level, but as spring will give the user 5 kinds, take a look:

1. DEFAULT

The default isolation level, where each database supports different levels of things isolation, will use the default thing isolation level of the underlying database if spring configures things with isolation set to this value. By the way, if you use MySQL, you can use "select @ @tx_isolation" To see the default thing Isolation level

2, read_uncommitted

READ UNCOMMITTED, that is able to read the data is not committed, so it is obvious that this level of isolation mechanism can not solve the dirty read, non-repeatable read, magic read any one, so rarely used

3, read_commited

Read Committed, which is able to read the data that has been submitted, can naturally prevent dirty reading, but cannot limit the non-repeatable read and Phantom read

4, Repeatable_read

Repeat read, that is, after the data read out lock, similar to "SELECT * from XXX for update", the explicit data read out is to update the use, so add a lock to prevent others to modify it. Repeatable_read meaning is similar, read a piece of data, this thing does not end, other things can not change this record, so that the dirty read, non-repeatable reading problem, but the problem of phantom reading can not be solved

5, Serlalizable

serialization, the highest level of things isolation, no matter how many things, one after another to run all the children of a thing before you can carry out all the things in the other thing, so that the problem of dirty reading, non-repeatable reading and phantom reading.

On-line charts are used to list the concurrency problems resolved by the Thing isolation level:

It must be stressed again, not things. The higher the isolation level is, the higher the level of the isolation of things, which means that it is necessary to spend the means to lock to ensure the correctness of things, then the efficiency will be reduced, so the actual development often in the efficiency and concurrency between the correctness of a trade-off, the general situation will be set to Read_ Commited, at this time to avoid dirty reading, concurrency is also good, and then through some other means to solve the problem of non-repeatable reading and phantom reading.

The isolation level of things and things

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.