package com.bluesky;
public interface FooServiceIface {
public void insertA();
public void insertB();
}
package com.bluesky;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class FooServiceImpl extends JdbcDaoSupport implements FooServiceIface {
public void insertA() {
this.getJdbcTemplate().execute("insert student(name) values('stuA')");
insertB();
int i=10/0;
}
public void insertB() {
this.getJdbcTemplate().execute("insert student(name) values('stuB')");
}
}
public class Client {
public static void main(String[] args) {
ApplicationContext appContxt = new ClassPathXmlApplicationContext("applicationContext.xml");
FooServiceIface fService= (FooServiceIface)appContxt.getBean("fooService");
fService.insertA();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="fooService" class="com.bluesky.FooServiceImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insertA" propagation="REQUIRED" />
<tx:method name="insertB" propagation="REQUIRES_NEW" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="interceptorPointCuts" expression="execution(* com.bluesky.*Service*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config>
</beans>
很抱歉代码看起来比较复杂
当我运行客户端时,出现了以下调试日志:
21:44:19,546 DEBUG TransactionSynchronizationManager:183 - 将值 [org.springframework.jdbc.datasource.ConnectionHolder@ba86ef] 绑定到键 [org.springframework.jdbc.datasource.DriverManagerDataSource@1b9658e] 在线程 [main] 21:44:19,546 DEBUG TransactionSynchronizationManager:258 - 初始化事务同步 21:44:19,547 DEBUG TransactionInterceptor:362 - 为 [com.bluesky.FooServiceImpl.insertA] 获取事务 21:44:19,547 DEBUG JdbcTemplate:416 - 执行 SQL 语句 [insert student(name) values('stuA')] 21:44:19,592 DEBUG JdbcTemplate:416 - 执行 SQL 语句 [insert student(name) values('stuB')] 21:44:19,594 DEBUG TransactionInterceptor:406 - 在出现异常 java.lang.ArithmeticException: / by zero 后完成 [com.bluesky.FooServiceImpl.insertA] 的事务 21:44:19,594 DEBUG RuleBasedTransactionAttribute:130 - 应用规则来确定是否在 java.lang.ArithmeticException: / by zero 上回滚事务 21:44:19,594 DEBUG RuleBasedTransactionAttribute:147 - 获胜的回滚规则是:null 21:44:19,595 DEBUG RuleBasedTransactionAttribute:152 - 找不到相关的回滚规则:应用默认规则 21:44:19,595 DEBUG DataSourceTransactionManager:938 - 触发 beforeCompletion 同步 21:44:19,595 DEBUG DataSourceTransactionManager:843 - 启动事务回滚 21:44:19,596 DEBUG DataSourceTransactionManager:279 - 在连接 [com.mysql.jdbc.JDBC4Connection@167f4bf] 上回滚 JDBC 事务 21:44:19,598 DEBUG DataSourceTransactionManager:967 - 触发 afterCompletion 同步 21:44:19,598 DEBUG TransactionSynchronizationManager:316 - 清除事务同步
我发现当调用 insertA()
方法时,该方法启动了一个事务,但在代码到达 insertB()
时,没有事务可以启动。
是我没有配置或者有什么问题吗?
我的意图是当 insertA()
调用 insertB()
时,会启动一个新的 REQUIRES_NEW
事务。