One service method calls an internal multiple method for a Spring transaction

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&amp;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> 

Sorry for the complicated code.

When I run the debug logs, you will see:

  21: 44: 19,546 DEBUG TransactionSynchronizationManager: 183 - Bound value [ org.springframework.jdbc.datasource.ConnectionHolder@ba86ef ] for key [ org.springframework.jdbc.datasource.DriverManagerDataSource@1b96 58e] 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

I found that when I call the insertA() method, this method starts a transaction, when insertB() arrives, there is no transaction to start.

Is there something that I didn’t configure or worry, I was wrong

My intention is to insertA() call insertB( ) to start the REQUIRES_NEW transaction.

+7
source share
3 answers

KLE's advice on refactoring your code directly for money, but as to why it doesn't work, Spring AOP uses JDK dynamic proxies by default to provide AOP. This means that when you embed your service into something, what is really being introduced is an instance of Proxy that implements your service interface. When the method runs on this proxy, it runs the transaction code before passing it to your actual service instance. However, if the control flow is inside your service, calling another method via this.foo() (even if this implicit) simply calls the method in the same instance: your service. It does not return to the proxy, it is the only thing that is known about transactions. If you switched to weaving bytecode with upload or download using AspectJ, then you can do it and it will work as expected, since the transaction-invasive code will be woven directly into your service code instead of living in separate facility.

+5
source

I understand your problem. There are some technically sophisticated ways to make it work, but we usually don't think it's worth it. I suggest a different approach, simple and powerful, that actually improves your code.

Instead of having them on the same Spring bean, have them on two different Beans, B inserted into A.

I know that I do not answer your question, but I think about the benefits:

  • Just dead, it will cost you very little time .
  • To require a new transaction, B is probably another problem. I am interested in another class that looks exactly the way we are looking, good design .
  • You have nothing new and difficult to explain to your colleagues (or a document for future developers), simplicity makes it immediately understandable .
+4
source

KLE's answer is solid advice. There is a workaround to complete the image, but it breaks all the AOPs:

 public void insertA() { this.getJdbcTemplate().execute("insert student(name) values('stuA')"); // this works, but... gah! ((FooServiceIface) AopContext.currentProxy()).insertB(); int i=10/0; } 

Reference:

+2
source

All Articles