On two transaction definition methods of spring

1、 Declarative

This method does not need to make any modification to the original business. The configuration can be completed by defining the matching of intercepting methods in the XML file. The requirement is that the naming of methods in business processing should be regular, such as setXXX, xxxupdate, etc. The detailed configuration is as follows:

2、 Annotation type

In this method, you only need to define a transaction management object (such as datasourcetransactionmanager) in the spring configuration file, then add the < TX: annotation driven / > node to reference the transaction management object, and then use @ transactional to label the classes and methods that need transaction processing. Examples are as follows:

3、 < TX: annotation driven / >

The < TX: annotation driven / > provided by spring is used to enable support for annotation transaction management, so that @ transactional annotation metadata on bean classes can be identified. It has the following attributes:

1、

Transaction manager: Specifies the name of the transaction manager. The default is transaction manager. When other names are used, they need to be explicitly specified;

Proxy target class: indicates the code mechanism to be used. The default value of false is JDK proxy. If true, cglib proxy will be used

Order: defines the transaction notification order, which is ordered by default LOWEST_ Precision, which means that the order decision is handed over to AOP for processing.

2、@Transaction

Spring uses @ transaction to specify transaction attributes, which can be specified on interfaces, classes or methods. If @ transaction is specified on both classes and methods, the transaction attributes on methods are used preferentially. The specific attributes are as follows:

Value: Specifies the name of the transaction manager. By default, the transaction manager specified by < TX: annotation driven / > is used to support the multi transaction manager environment;

Propagation: Specifies the transaction propagation behavior. The default value is required. Propagation. Is used Required specifies;

Isolation: Specifies the transaction isolation level. The default value is "default". Isolation is used Default specifies;

Readonly: Specifies whether the transaction is read-only. False by default indicates that the transaction is not read-only;

Timeout: Specifies the transaction timeout, in seconds. The default value of - 1 means that the transaction timeout will depend on the underlying transaction system;

Rollbackfor: specifies a group of exception classes. In case of such exceptions, the transaction will be rolled back;

Rollback forclassname: specifies a group of exception class names, whose meaning is exactly the same as that of rollback for attribute in < TX: method >;

Norollbackfor: specifies a group of exception classes. Even if such exceptions are encountered, the transaction will be committed, that is, the transaction will not be rolled back;

Norollbackforclassname: specifies a group of exception class names, whose meaning is exactly the same as that of the no rollback for attribute in < TX: method >;

The @ transaction annotation provided by spring also uses the surround notification transactioninterceptor to open and close transactions within the transaction management.

The following points should be paid special attention to when using @ transactional annotation for transaction management:

If @ transactional annotation is specified on the interface, implementation class or method, the priority order is method > implementation class > interface;

It is recommended to use @ transactional only on the implementation class or the method of the implementation class, not on the interface, because it is OK to use JDK proxy mechanism, because it uses interface based proxy; When using cglib proxy mechanism, we will encounter problems because it uses class based proxy instead of interface, because the @ transactional annotation on the interface is "not inheritable";

Under the spring proxy mechanism (whether JDK dynamic proxy or cglib proxy), "self invocation" will not apply the corresponding transaction attributes, and its semantics is the same as that in < TX: Tags >;

By default, only runtimeException exceptions are rolled back;

When using spring proxy, only the @ transactional annotation of the method with public visibility is valid by default, Even if there are @ transactional annotations on other visibility methods (protected, private and package visible), these transaction attributes will not be applied, and spring will not report errors. If you have to annotate transaction management with non-public methods, you can consider using AspectJ.

The above article on the two transaction definition methods of spring is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>