一个服务方法调用Spring事务的内部多个方法
抱歉,代码太复杂了 运行客户端blow调试日志时显示:一个服务方法调用Spring事务的内部多个方法,spring,transactions,aop,Spring,Transactions,Aop,抱歉,代码太复杂了 运行客户端blow调试日志时显示: <?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"
<?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:19546调试事务同步管理器:183-绑定值[org.springframework.jdbc.datasource]。ConnectionHolder@ba86ef]对于键[org.springframework.jdbc.datasource]。DriverManagerDataSource@1b9658e]线程[主]
21:44:19546调试事务同步管理器:258-初始化事务同步
21:44:19547调试事务接收器:362-获取[com.bluesky.FooServiceImpl.insertA]的事务
21:44:19547调试JdbcTemplate:416-执行SQL语句[插入学生(名称)值('stuA')]
21:44:19592调试JdbcTemplate:416-执行SQL语句[插入学生(名称)值('stuB')]
21:44:19594调试事务Interceptor:406-在异常:java.lang.arithmetricException:/by零之后完成[com.bluesky.FooServiceImpl.insertA]的事务
21:44:19594调试RuleBasedTransactionAttribute:130-应用规则以确定事务是否应在java.lang.ArrithmeticException:/by 0上回滚
21:44:19594调试RuleBasedTransactionAttribute:147-成功回滚规则为:null
21:44:19595调试基于规则的TransactionAttribute:152-未找到相关回滚规则:应用默认规则
21:44:19595调试数据源TransactionManager:938-完成同步前触发
21:44:19595调试数据源TransactionManager:843-启动事务回滚
21:44:19596调试DataSourceTransactionManager:279-在连接[com.mysql.JDBC]上回滚JDBC事务。JDBC4Connection@167f4bf]
21:44:19598调试数据源TransactionManager:967-完成同步后触发
21:44:19598调试事务同步管理器:316-清除事务同步
我发现当调用insertA()
方法时,该方法启动一个事务,当代码到达insertB()
时,没有可启动的事务
是否存在我没有配置的内容或我弄错的问题
我的意图是当
insertA()
调用insertB(
)时,有一个需要新的事务开始。我理解您的问题。有一些技术上复杂的方法让它工作,但我们通常不认为它们是值得的。我建议使用另一种简单而强大的方法,实际上可以改进代码
不要把它们放在同一个春豆上,而是放在两个不同的豆子上,B被注入A
我知道我没有回答你的问题,但想想它的优点:
- 非常简单,现在只需花费很少的时间
- 为了需要一个新的事务,B可能是一个不同的关注点。不同的关注点是不同的类,这似乎正是我们所寻找的,好的设计
- 您没有任何新的和复杂的东西要向同事解释(或为未来的开发人员编写文档),它的简单性使它立即易懂
KLE关于重构代码的建议是合算的,但至于为什么它不起作用,springaop默认使用它来提供AOP。这意味着,当您将服务注入到某个对象中时,真正注入的是实现服务接口的代理实例。在该代理上调用方法时,它会在委托给实际服务实例之前运行事务代码。但是,一旦控制流在您的服务中,通过this.foo()
调用另一个方法(即使this
是隐式的)只调用同一实例上的方法:您的服务。它不会返回到代理,代理是唯一了解事务的东西。要使用AspectJ构建或加载时字节码编织,您可以这样做,它将按预期工作,因为调用代码的事务将直接编织到您的服务代码中,而不是生活在单独的对象中。KLE的答案是可靠的建议。为了完成这幅图,有一个变通方法,但它打破了AOP所代表的一切:
21:44:19,546 DEBUG TransactionSynchronizationManager:183 - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@ba86ef] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@1b9658e] to thread [main]
21:44:19,546 DEBUG TransactionSynchronizationManager:258 - Initializing transaction synchronization
21:44:19,547 DEBUG TransactionInterceptor:362 - Getting transaction for [com.bluesky.FooServiceImpl.insertA]
21:44:19,547 DEBUG JdbcTemplate:416 - Executing SQL statement [insert student(name) values('stuA')]
21:44:19,592 DEBUG JdbcTemplate:416 - Executing SQL statement [insert student(name) values('stuB')]
21:44:19,594 DEBUG TransactionInterceptor:406 - Completing transaction for [com.bluesky.FooServiceImpl.insertA] after exception: java.lang.ArithmeticException: / by zero
21:44:19,594 DEBUG RuleBasedTransactionAttribute:130 - Applying rules to determine whether transaction should rollback on java.lang.ArithmeticException: / by zero
21:44:19,594 DEBUG RuleBasedTransactionAttribute:147 - Winning rollback rule is: null
21:44:19,595 DEBUG RuleBasedTransactionAttribute:152 - No relevant rollback rule found: applying default rules
21:44:19,595 DEBUG DataSourceTransactionManager:938 - Triggering beforeCompletion synchronization
21:44:19,595 DEBUG DataSourceTransactionManager:843 - Initiating transaction rollback
21:44:19,596 DEBUG DataSourceTransactionManager:279 - Rolling back JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@167f4bf]
21:44:19,598 DEBUG DataSourceTransactionManager:967 - Triggering afterCompletion synchronization
21:44:19,598 DEBUG TransactionSynchronizationManager:316 - Clearing transaction synchronization
参考资料:
10亿感谢您的回复,我想知道当insertA()使用spring aop调用insertB()时是否没有效果(方法insertB()不应在spring事务控制中)?@user。。。我不确定我是否理解你的评论。在不同的bean上,Spring将确保事务按预期工作。如果您设置需要新的交易策略,您将得到您想要的。+1感谢您提供的技术详细信息。找到与需求接近的产品总是很好的。:-)+谢谢你的帮助!:-)我希望我的团队永远不会读到这篇文章,有些人可能会开始使用它:-))
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 - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@ba86ef] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@1b9658e] to thread [main]
21:44:19,546 DEBUG TransactionSynchronizationManager:258 - Initializing transaction synchronization
21:44:19,547 DEBUG TransactionInterceptor:362 - Getting transaction for [com.bluesky.FooServiceImpl.insertA]
21:44:19,547 DEBUG JdbcTemplate:416 - Executing SQL statement [insert student(name) values('stuA')]
21:44:19,592 DEBUG JdbcTemplate:416 - Executing SQL statement [insert student(name) values('stuB')]
21:44:19,594 DEBUG TransactionInterceptor:406 - Completing transaction for [com.bluesky.FooServiceImpl.insertA] after exception: java.lang.ArithmeticException: / by zero
21:44:19,594 DEBUG RuleBasedTransactionAttribute:130 - Applying rules to determine whether transaction should rollback on java.lang.ArithmeticException: / by zero
21:44:19,594 DEBUG RuleBasedTransactionAttribute:147 - Winning rollback rule is: null
21:44:19,595 DEBUG RuleBasedTransactionAttribute:152 - No relevant rollback rule found: applying default rules
21:44:19,595 DEBUG DataSourceTransactionManager:938 - Triggering beforeCompletion synchronization
21:44:19,595 DEBUG DataSourceTransactionManager:843 - Initiating transaction rollback
21:44:19,596 DEBUG DataSourceTransactionManager:279 - Rolling back JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@167f4bf]
21:44:19,598 DEBUG DataSourceTransactionManager:967 - Triggering afterCompletion synchronization
21:44:19,598 DEBUG TransactionSynchronizationManager:316 - Clearing transaction synchronization
public void insertA() {
this.getJdbcTemplate().execute("insert student(name) values('stuA')");
// this works, but... gah!
((FooServiceIface) AopContext.currentProxy()).insertB();
int i=10/0;
}