Home >Java >javaTutorial >Detailed explanation of examples of Spring's use of annotations for transaction management configuration
This article mainly introduces Spring's use of annotations for transaction management configuration. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look
Usage steps:
Step 1. Introduce the cc2c39e2370aac9d3896793edbbe786f namespace in the spring configuration file
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
Step 2. Beans with @Transactional annotations are automatically configured to support declarative transactions
<!-- 事务管理器配置, Hibernate单数据源事务 --> <bean id="defaultTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 使用annotation定义事务 --> <tx:annotation-driven transaction-manager="defaultTransactionManager" proxy-target-class="true" />
Step 3.At the declaration of the interface or class, write A @Transactional.
If you only write on the interface, the implementation class of the interface will be inherited. The specific methods of the implementation class of the interface can override the settings at the class declaration
@Transactional //Class-level annotations, applicable to all public methods in the class
Transaction propagation behavior and isolation level
Everyone is using spring When using annotation-based transaction management, you may be a little overwhelmed by the transaction propagation behavior and isolation level. The following is a detailed introduction for easy reference.
Thing annotation method: @Transactional
When marked in front of a class, it indicates that all methods in the marked class perform transaction processing, example:
@Transactional public class TestServiceBean implements TestService {}
When some methods in the class do not require things:
@Transactional public class TestServiceBean implements TestService { private TestDao dao; public void setDao(TestDao dao) { this.dao = dao; } @Transactional(propagation = Propagation.NOT_SUPPORTED) public List<Object> getAll() { return null; } }
Thing propagation behavior introduction:
Thing timeout setting:
Parameter name | Function description |
This attribute is used to set whether the current transaction is a read-only transaction. Set to true to indicate read-only, and false to indicate read-write. The default value is false. For example: @Transactional(readOnly=true) | |
This attribute is used to set the required rollback Exception class array, when the exception in the specified exception array is thrown in the method, the transaction will be rolled back. For example: |
Specify a single exception class: @Transactional(rollbackFor=RuntimeException.class) Specify multiple exception classes: @Transactional(rollbackFor={RuntimeException.class, Exception.class}) |
Parameter name |
Function description |
rollbackForClassName |
This property is used to set the need for rollback An array of rollover exception class names. When an exception in the specified exception name array is thrown in the method, the transaction will be rolled back. For example: Specify a single exception class name: @Transactional(rollbackForClassName="RuntimeException") Specify multiple exception class names: @Transactional (rollbackForClassName={"RuntimeException","Exception"}) |
#noRollbackFor |
This attribute is used to set an array of exception classes that do not require rollback. When an exception in the specified exception array is thrown in the method, the transaction will not be rolled back. For example: Specify a single exception class: @Transactional(noRollbackFor=RuntimeException.class) Specify multiple exception classes: @Transactional(noRollbackFor ={RuntimeException.class, Exception.class}) |
##noRollbackForClassName
|
This attribute is used to set an array of exception class names that do not require rollback. When an exception in the specified exception name array is thrown in the method, the transaction will not be rolled back. For example: Specify a single exception class name: @Transactional(noRollbackForClassName="RuntimeException") Specify multiple exception class names: @Transactional(noRollbackForClassName={"RuntimeException","Exception"})
|
For example: @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
|
|
The above is the detailed content of Detailed explanation of examples of Spring's use of annotations for transaction management configuration. For more information, please follow other related articles on the PHP Chinese website!