Spring 中事务的 API
mybatis:sqlSession.commit();
hibernate:session.commit();
PlatformTransactionManager
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | public interface PlatformTransactionManager {
 
 
 
 TransactionStatus getTransaction(@Nullable TransactionDefinition definition)
 throws TransactionException;
 
 
 
 
 void commit(TransactionStatus status) throws TransactionException;
 
 
 
 
 void rollback(TransactionStatus status) throws TransactionException;
 
 }
 
 | 
此接口是 Spring 的事务管理器核心接口。Spring 本身并不支持事务实现,只是负责提供标准,应用底层支持什么样的事务,需要提供具体实现类。此处也是策略模式的具体应用。在Spring框架中,也为我们内置了一些具体策略,例如:DataSourceTransactionManager, HibernateTransactionManager(在 spring-orm-5.1.12.RELEASE.jar 中)等。
需要的依赖
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 
 | <!--Spring aop--><dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-aop</artifactId>
 <version>5.1.12.RELEASE</version>
 </dependency>
 <!--aspectj-->
 <dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjweaver</artifactId>
 <version>1.9.5</version>
 </dependency>
 
 <!--spring 声明式事物-->
 <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 方式
xml 文件头
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | <?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 https://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 https://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/aop
 https://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/tx
 https://www.springframework.org/schema/tx/spring-tx.xsd">
 
 </beans>
 
 | 
配置
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | 
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <constructor-arg name="dataSource" ref="dataSource"/>
 </bean>
 
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
 
 <tx:attributes>
 
 <tx:method name="*" read-only="false" isolation="DEFAULT" propagation="REQUIRED" timeout="-1"/>
 
 <tx:method name="query*" read-only="true" propagation="SUPPORTS"/>
 </tx:attributes>
 </tx:advice>
 
 <aop:config>
 
 <aop:pointcut id="pt1" expression="execution(* com.lagou.edu.service.impl.*.*(..))"/>
 
 <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
 </aop:config>
 
 | 
配置事务的属性
- isolation:用于指定事务的隔离级别。默认值是DEFAULT,表示使用数据库的默认隔离级别。
- propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。
- read-only:用于指定事务是否只读。只有查询方法才能设置为true。默认值是false,表示读写。
- timeout:用于指定事务的超时时间,默认值是-1,表示用不超时。如果指定了数值,以秒为单位。
- rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值。表示任何异常都回滚。
- no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值。表示任何异常都回滚。
XML + 注解方式
配置
| 12
 3
 4
 5
 6
 7
 
 | <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <constructor-arg name="dataSource" ref="dataSource"/>
 </bean>
 
 
 <tx:annotation-driven transaction-manager="transactionManager"/>
 
 | 
在接口、类或者方法上添加 @Transactional 注解
| 1
 | @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
 | 
注解方式
在 Spring 的配置类上添加 @EnableTransactionManagement 注解以替换 xml 配置文件中的 <tx:annotation-driven transaction- manager=”transactionManager”/> 配置。
| 12
 3
 
 | @EnableTransactionManagement public class SpringConfig {
 }
 
 |