The @transactional in spring

Source: Internet
Author: User

@transactional in spring is based on the mechanism of dynamic agent, which provides a transparent transaction management mechanism, which is convenient and quick to solve the problems encountered in development.

A general use is to comment on a method or interface or class by using the following code:

@Transactional (propagation=propagation.not_supported)

  

The propagation supports 7 different propagation mechanisms:

REQUIRED: If there is a transaction, the current transaction is supported. If there is no transaction, a new transaction is opened.

SUPPORTS: If there is a transaction, the current transaction is supported. If there is no transaction, the execution is non-transactional. However, for transaction managers with transactional synchronization, Propagation_supports is slightly different from not using transactions.

Not_supported: Always executes in a non-transactional manner and suspends any existing transactions.

RequiresNew: Always open a new transaction. If a transaction already exists, the existing transaction is suspended.

MANDATORY: If a transaction already exists, the current transaction is supported. Throws an exception if there is no active transaction.

Never: Always executes non-transacted and throws an exception if there is an active transaction

NESTED: If an active transaction exists, it is run in a nested transaction. If there is no active transaction, it is performed by the required property.

Here are some things to be aware of, must have to look at, or encounter a variety of pits don't say Bo Master didn't remind you OH:

    1. Add @transactional annotations where transaction management is required. @Transactional annotations can be applied to interface definitions and interface methods, class definitions, and public methods of classes.
    2. @Transactional annotations can only be applied to methods on public visibility. If you use @Transactional annotations on protected, private, or package-visible methods, it will not error, but this annotated method will not show the configured transaction settings.
    3. Note that only @Transactional annotations appear to be less than open transaction behavior, which is simply a kind of meta-data. The configuration element must be used in the configuration file to actually open the transaction behavior.
    4. Controls whether an interface-based or class-based proxy is created through the element's "Proxy-target-class" property value. If the "Proxy-target-class" value is set to "true", then the class-based proxy will function (this requires the Cglib library Cglib.jar in classpath). If the "Proxy-target-class" value is set to "false" or if this attribute is omitted, then the standard JDK interface-based proxy will work.
    5. The spring team recommends using @Transactional annotations on specific classes (or methods of classes) instead of using any interfaces that the class implements. Using @Transactional annotations on an interface only takes effect if you set up an interface-based proxy. Because annotations are not inherited, this means that if a class-based proxy is being used, the transaction's settings will not be recognized by the class-based proxy, and the object will not be wrapped by the transaction proxy.
    6. @Transactional transactions are opened, either interface-based or class-based proxies are created. So in the same class one method calls another method with a transactional method, and the transaction does not work

Material management is essential for enterprise applications, so that anomalies can occur, and it also guarantees data consistency.

Spring supports both programmatic transaction management and declarative transaction management.

Programmatic transaction management uses Transactiontemplate or uses the underlying platformtransactionmanager directly. For programmatic transaction management, Spring recommends the use of transactiontemplate.

Declarative transaction management is built on the AOP. The essence is to intercept the method before and after it, and then create or join a transaction before the target method starts, committing or rolling back the transaction according to execution after the target method is executed. The greatest advantage of declarative transactions is that you do not need to programmatically manage transactions, so that you do not need to doping transaction-managed code in your business logic code, just make relevant transaction rule declarations in the configuration file (or through @transactional annotations). You can apply transaction rules to the business logic.

It is obvious that declarative transaction management is better than programmatic transaction management, which is the non-intrusive development approach advocated by spring. Declarative transaction management makes business code non-polluting, a common Pojo object that can be fully transactional supported with annotations. The only disadvantage of declarative transactions, compared to programmatic transactions, is that the finer granularity of the latter only works at the method level and cannot be scoped to the code block as a programmatic transaction. But even with this requirement, there are many workarounds, such as the ability to separate code blocks that require transaction management into methods, and so on.

There are two common ways of declarative transaction management, one is an XML configuration file based on the TX and AOP namespaces, and the other is based on @transactional annotations. The annotation-based approach is obviously easier to use and more refreshing.

The @transactional in spring

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.