Transaction understanding and transaction in spring

Source: Internet
Author: User

I. Understanding of isolation Levels

1. Dirty Reading

First, it is understood that a transaction has changed the data, although the transaction has not yet been committed, but at this point the data found by the other statements is changed after the transaction. Verify the following:

A simple user table

We run the following statement 1233 line, open the transaction, but not yet committed

We can see that even though the transaction has not yet been committed, it is also the transaction-modified data at this point.

Look again, run the following 562 lines of statements

So, dirty reading, is a transaction B read the data is another transaction A has not yet committed data , this situation is called dirty read, because if a transaction rollback, then the B transaction read data is incorrect data.

2. Non-repeatable reading

Non-repeatable reading means that in the case of a data in a database, multiple queries within a transaction scope return different data values because the query interval is modified and committed by another transaction.

3. Phantom Reading

Transaction a modifies the conditional bar data in the table, when transaction B inserts a qualifying data and submits it to the database, then if transaction a then looks at the result of the modification, finds that there is a data that has not been modified, or a transaction query meets the criteria, and finds 10, At this point B transaction deleted or inserted a qualifying data, that a transaction re-check, found that the result is 9 or 11, there is a hallucination, this situation is phantom reading, also known as virtual read.

Non-repeatable reads and Phantom reads are data that have been read by the committed transaction operation, while dirty reads are data that read UNCOMMITTED transaction operations.

Second, the transaction processing in spring

Spring provides good support for transactions, so let's take a look at how the transactions in spring are handled

1. Propagate Properties

propagation_required--supports the current transaction and creates a new transaction if there is no current transaction. This is the most common choice.

propagation_supports--supports the current transaction and is executed in a non-transactional manner if no transaction is currently in use.

The propagation_mandatory--supports the current transaction and throws an exception if there is no current transaction.

propagation_requires_new--a new transaction, suspending the current transaction if a transaction is currently present.

The propagation_not_supported--executes the operation in a non-transactional manner, suspending the current transaction if a transaction is currently present.

propagation_never--is executed in a non-transactional manner and throws an exception if a transaction is currently present

propagation_nested---New transaction and suspends the current transaction if there is a transaction currently in place. The difference from Propagation_requires_new is that the parent transaction is rolled back, and the child transaction also needs to be rolled back.

2. Isolation LEVEL

default (default)
This is a Platfromtransactionmanager default isolation level that uses the default transaction isolation level of the database. The other four isolation levels corresponding to JDBC should be


Read_uncommitted (read not submitted)
This is the lowest isolation level for transactions, which allows another transaction to see the uncommitted data for this transaction. This isolation level produces dirty reads, non-repeatable reads, and Phantom reads.

Read_committed (read submitted)
Ensure that a transaction modified data is committed before it can be read by another transaction. Another transaction cannot read uncommitted data for the transaction. This level of transaction isolation avoids dirty reads, but it is possible that non-repeatable reads and Phantom reads are the most commonly used levels.

Repeatable_read (repeatable reading)
Guarantees that the transaction does not modify data that has been read by another transaction but that the transaction has not yet been committed or rolled back. This level of transaction isolation prevents dirty reads and cannot be read repeatedly. However, Phantom reads may occur. In addition to ensuring that one transaction cannot read data that is not committed by another transaction, it guarantees non-repeatable reads, but consumes large resources.

SERIALIZABLE (serialization)
This is the most cost-effective, but most reliable, transaction isolation level. Transactions are processed for sequential execution. In addition to preventing dirty reading, non-repeatable reading, but also avoids phantom reading. This approach consumes the most resources.

Transaction understanding and transaction in spring

Related Article

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.