Transaction Configuration
Transaction refers to a group of operations that either all succeed or all fail, maintaining data consistency. Spring provides declarative transaction support. The underlying layer is based on AOP principle, implementing method call enhancement.
Pure XML Mode
Jar Package
Ensure the following dependencies exist:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
XML Sample
Core configuration as follows:
<!-- Configure data source -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!-- Database connection parameters -->
</bean>
<!-- Configure transaction manager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Enable transaction annotation-driven -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Or use transaction interceptor + AOP approach -->
<aop:config>
<aop:pointcut id="serviceOperation" expression="execution(* com.xxx.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
XML Configuration
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" read-only="false" propagation="REQUIRED" isolation="DEFAULT" timeout="-1" />
<tx:method name="query*" read-only="true" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* wzk.service.impl.WzkTransferServiceImpl.*(..))" />
</aop:config>
Based on XML + Annotation
<!--Configure transaction manager-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--Enable Spring annotation transaction support-->
<tx:annotation-driven transaction-manager="transactionManager"/>
Add @Transactional annotation on interface, class, or method:
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
Based on Pure Annotation
Spring annotation-driven development transaction control configuration, just change XML configuration part to annotation implementation.
Annotation replaces <tx:annotation-driven transaction-manager="xxx"> in XML configuration file. Then add in Spring configuration:
// Enable transaction support
@EnableTransactionManagement
@Transactional annotation attributes:
- propagation: Transaction propagation behavior, such as REQUIRED, REQUIRES_NEW
- isolation: Transaction isolation level, such as READ_COMMITTED
- timeout: Transaction timeout time (seconds)
- readOnly: Whether read-only transaction, improves query performance
- rollbackFor: Specify which exception types trigger rollback (such as Exception.class)
- noRollbackFor: Specify which exception types do not trigger rollback