@transactional depth analysis of spring in the second

Source: Internet
Author: User

5. The isolation of @Transactional
The problem with isolation levels is that in an application, when there are multiple transactions running at the same time, problems need to be addressed and handled well. So first of all, what are the general problems? Let's take a look first.
    • Dirty Reads (Dirty read)
One thing updates some of the data in the database, another thing reads it, and the previous thing is rolled back for some reason, so the second thing reads "dirty" data.
    • Non-repeatable read (Non-repeatable Read)
A thing needs two times to query the same data, but there may be another thing in the middle of two queries that changes the data, causing the data to be read out of the previous two times inconsistent.
    • Phantom Read (Phantom Read)
One thing queries the same table two times, but there may be another thing in the middle of the two query that inserts some new data into the table, causing the two queries of the previous thing to be inconsistent
Here's a look at the values that isolation has in @transactional in spring:
    • Default isolation level for each database is used
    • Read uncommited: Reading uncommitted data (dirty read, non-repeatable read, Phantom Read)
    • Read commited: Reading submitted data (non-repeatable read, Phantom Read)
    • REPEATABLE READ: Repeatable reading (phantom reading will occur)
    • Serializable: Serialization

The corresponding relationship to the problem that is prone to the transaction is as follows:

How to set up specific isolation boundaries depends on the degree to which the business system is specifically tolerated. Serializable is the most stringent, but the efficiency is the lowest; Read uncommited is the most efficient, but it is easy to have a variety of problems, the middle of the 2 levels in between the two, it is essentially a balance between efficiency and error probability and compromise.

6. Timeout of @Transactional

Used to set the length of time a transaction is processed, to block a potentially long-time blocking system, or to consume system resources. Unit is seconds. If the timeout is set, the transaction is rolled back and the transactiontimedoutexception exception is thrown.

Spring Transaction Timeout = When the transaction started to the last statement creation time + The last statement execution time-out time (that is, its querytimeout).

For the calculation of transaction time-out time, refer to: http://jinnianshilongnian.iteye.com/blog/1986023

7. The ReadOnly of @Transactional

By default, False, which can be displayed as true, tells the program to use a read-only operation under this method, and if other non-read operations are performed, it will run out of exception, which is tightly suited for cases where only the ReadOnly identity is used, when it is in use with the propagation mechanism. The read-only setting is overwritten, such as in the case of required. When using REQUIRED propagation mode, a read-only connection exception is thrown. This is true when using JDBC. When using an ORM-based framework, the read-only flag is only a hint to the database, and an ORM framework-based directive (Hibernate in this case) sets the flush mode of the object cache to never, which means that the object cache should not be synchronized with the database in this unit of work. However, REQUIRED propagation mode overwrites all of this content, allowing the transaction to start and work as if the read-only flag was not set.

Specific details can be consulted: http://robinsoncrusoe.iteye.com/blog/825531

8. The rollbackforclassname/rollbackfor of @Transactional

Spring defaults to a transaction rollback of the run-time exception (RuntimeException). This exception is unchecked and will not be rolled back if you encounter checked unexpectedly.

Used to indicate which exception class or exception class name the condition of the rollback is. The usage is relatively simple, and these are no longer mentioned.

9. The norollbackforclassname/norollbackfor of @Transactional
The function is identical to 8, which indicates that the transaction rollback operation of the database is not performed in the case of a particular exception being thrown.

10. Summary

After detailed analysis of the various properties of the @transactional, I believe you have a certain understanding of their specific meanings and application scenarios, and will no longer be so confused and hesitant to use them. Thank you very much for spring's offer of these convenient ways of managing your affairs and getting rid of the tedious physical work Improve the efficiency of development.

In the process of this information, refer to a lot of blog text, no longer listed, and thank you, more is hope to stand on the shoulders of these giants, technology points and application points can be explained clearly.

@transactional depth analysis of spring in the second

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.