Understand database transaction ISOLATION level and dirty read, not repeatable read, Phantom read __ database

Source: Internet
Author: User

4 attributes of a database transaction:
Atomicity (atomic):
both succeed or fail;
Consistency (consistency): After a transaction operation, the database is in the same state as the business rules; for example, after the A,b account transfers, the total amount remains unchanged;
Isolation (Isolation): transactions in operations do not affect each other;
Persistence (Durability): After a transaction is committed, it is persisted to the database.

Dirty read, not repeatable read, Phantom read
Phantom Read Phantom Read:
transaction 1 When the record is read, 2 adds records and commits, and transaction 1 reads again to see the new record of transaction 2;
non-repeatable read unrepeatable read: transaction 1 When a record is read, 2 updates the record and commits, and transaction 1 reads again to see the modified record of transaction 2;
dirty reads dirty read: transaction 1 updated the record, but did not commit, transaction 2 read the updated row, and then the transaction T1 Rollback, now T2 read is invalid.

Transaction ISOLATION LEVEL Description:
READ UNCOMMITTED: Phantom reading, not repeatable reading and dirty reading are allowed;
Read Committed: Allows Phantom and non repeatable reads, but does not allow dirty reads;
REPEATABLE READ: Allows for phantom reading, but does not allow unreadable and dirty reads;
SERIALIZABLE: Phantom reading, not repeatable reading and dirty reading are not allowed;
Oracle defaults to READ committed.

How to perceive these "esoteric" terms in a perceptual way. Personally, as a Java programmer, these words are more bluffing.

first, the isolation level describes how the transaction is quarantined. You can simply imagine that a person for some reason (such as national security) need to be quarantined, the more security-related confidentiality, the higher the need for a level of isolation, the less he and the outside channels of communication.

for a database, each transaction consumes some resources, such as the operational rights to the table/data, and the isolation level of the transaction describes the extent to which the transaction is consumed by the resource.

If the isolation level of the database is reae_uncommitted, other threads can see the uncommitted data, so dirty reads occur;

If the database isolation level is set to read_committed, that is, the data that is not submitted is invisible to others and avoids dirty reads; however, the data being read is only read-locked, unlocked after reading, and allows other transactions to modify the data being read by this transaction, regardless of whether the current transaction has ended. Results in a non repeatable read.

Repeatable read you can avoid non-repeatable reads because you lock the data that is being manipulated and only wait until the end of the transaction to release the lock;
Repeatable read can only guarantee that the data being manipulated by this transaction is not modified by other transactions, but there is no guarantee that other transactions will commit the new data. It is possible that thread 1 will be able to submit new data to the table T1 when the table is T1 (especially a statistical transaction), which can result in inconsistent results for threads 12 of times, just as in the case of hallucinations.

Serializable because the scope lock is obtained and the transaction is executed sequentially, the Phantom reads are guaranteed to not occur.
Thus, the higher the isolation level, the less interference by other things, the worse the concurrency performance.

Set syntax

Oracle:

SET TRANSACTION Isolation Level serializable| READ committed| READ uncommitted| Repeatable READ;

Sybase:

SQL Opreation at Isolation Read Committed

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.