【正常事务操作】
Connection connection = null;
try {
connection = dataSource.getConnection();
System.out.println("连接:"+connection);
// 开启事务(关闭自动提交 -- 防止将原子业务操作拆开提交了)
connection.setAutoCommit(false);
// 核心操作
userService.insert(record);
// 提交事务
connection.commit();
} catch (SQLException e) {
// 回滚事务
connection.rollback();
e.printStackTrace();
} finally {
// 释放连接
connection.close();
}
【基于注解的声明式事务管理】
1. @Transactional(注解的属性指定:传播行为、隔离级别、回滚、只读、异常等值)
2. <tx:annotation-driven transaction-manager="transactionManager"/>
3. <bean id="transactionManager" class="..."> + dataSource
【基于xml配置+AOP切入点表达式的声明式事务管理】
1.配置要扫描的包,将@Service标注的类注入到IOC容器中
<context:component-scan base-package="com.crowdfund.service" />
2.配置事务管理器:transaction manager
# 本地事务使用 PlatformTransactionManager
# 分布式事务的话建议使用 JtaTransactionManager作为全局事务管理器
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
3.配置aop切面
<aop:config>
<!-- 切入点表达式: expression="execution(* *..*Service.*(..))" -->
<aop:pointcut id="txPoint" expression="execution(* *..*Service.*(..))" />
<!-- 将切入点表达式与事务通知关联起来,配置事务增强 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" />
</aop:config>
4.配置事务通知
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 配置事务属性 -->
<tx:attributes>
<!-- 查询类方法:配置只读属性,这样可以让数据库对查询行为进行一定的优化 -->
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="select*" read-only="true" />
<!-- 增删改方法:配置事务传播行为、回滚异常-->
<tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRES_NEW" />
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRES_NEW" />
<tx:method name="create*" isolation="DEFAULT" propagation="REQUIRES_NEW" />
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<!-- 下面的配置则表示所有其他方法使用框架的默认事务配置 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
【事务传播行为propagation说明】
# REQUIRED:默认值,表示当前方法必须工作在事务中,如果当前线程上没有已经开启的事务,则自己开始事务
而如果当前线程有事务,则使用这个事务。但是该事务是别人的,所以有被回滚的风险。
# REQUIRES_NEW:建议使用的值,表示不管当前线程上有没有事务,都要自己开一个事务,在自己的事务里运行,
好处是:不会受到其他事务回滚的影响。同时当前事务的异常回滚不会影响到外部事务的正常执行。