Announcement Announcement Module
Collapse
No announcement yet.
Transaction rollback with @Async Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction rollback with @Async

    I am using @Async method annotation to improve the performance of the application by querying independent tables from the same database (sql server). However the transaction is not rolled back on an exception which works fine if I query synchronously. Is there a way to extend transaction support for asyncronous processing.

    I feel it is not greatly useful if transaction is not supported for parallel processing. Any solution would be helpful.

  • #2
    Can you provide code.

    Comment


    • #3
      Below is my configuration for transactions

      Code:
      	<bean id="TransactionProxyCreator"
      		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
      		<property name="interceptorNames">
      			<list>
      				<value>SpringTransactionInterceptor</value>
      			</list>
      		</property>
      		<property name="beanNames">
      			<value>*Service</value>
      		</property>
      	</bean>
      
      	<bean id="TransactionAttributes"
      		class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
      		<property name="properties">
      			<value>
      			 	*=PROPAGATION_REQUIRED
      			</value>
      		</property>
      	</bean>
      
      	<bean id="SpringTransactionInterceptor"
      		class="org.springframework.transaction.interceptor.TransactionInterceptor">
      		<property name="transactionManager">
      			<ref bean="JTATxManager" />
      		</property>
      		<property name="transactionAttributeSource">
      			<ref bean="TransactionAttributes" />
      		</property>
      	</bean>
      
      	<bean id="JTATxManager"
      		class="org.springframework.transaction.jta.JtaTransactionManager">
      		<property name="autodetectTransactionManager" value="false" />
      	</bean>
      Below is the method in service class OrderService.java

      Code:
      public void updateOrders()
       {
      
      	List<Future<Integer>> queryTasks = new ArrayList<Future<Integer>>();
      	for(int i=4521;i<4526;i++)
      	{		
      		Future<Integer> queryTask = this.getTestDao().updateOrderId(i);
      		queryTasks.add(queryTask);
      	}	
      	
      }
      Below is the Dao method which is asynchronous
      Code:
      @Async
      public Future<Integer> updateOrderId(int orderNo) 
      {
      	int result = this.update("updateOrderStatusForId", orderNo);
      	System.out.println("@@@@@@@@@@@@@@@@@@@@@@@ Update successful");
      
      	return new AsyncResult<Integer>(result);
      }
      I need all the orders from 4521 to 4526 to be rolled back if any one of them fails. Currently none are rolled back.

      Comment


      • #4
        How do you expect that to work? A transaction cannot be spun into another thread. How long should the calling thread actually wait for the newly spun off one? As soon as the calling thread returns outside the transactional method we *have to* either rollback or commit. If the spun off thread has not finished yet, what do you think should happen? Block and wait? What's the point of asynchrony then?

        Comment


        • #5
          Hi I am getting below error when i started application in tomcat.
          12:54:55,978 [ERROR] [main] [ContextLoader] Context initialization failed
          org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'mattSnapshotRepository': Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.transaction.interceptor.Transa ctionInterceptor.setTransactionManagerBeanName(Lja va/lang/StringV
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:527)
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
          at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 91)
          at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
          at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:288 )
          at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:190)
          at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:580)
          at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:895)
          at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:425)
          at org.springframework.web.context.ContextLoader.crea teWebApplicationContext(ContextLoader.java:276)
          at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:197)
          at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.java: 47)
          at org.apache.catalina.core.StandardContext.listenerS tart(StandardContext.java:4205)
          at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4704)
          at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1053)
          at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:840)
          at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1053)
          at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:463)
          at org.apache.catalina.core.StandardService.start(Sta ndardService.java:525)
          at org.apache.catalina.core.StandardServer.start(Stan dardServer.java:754)
          at org.apache.catalina.startup.Catalina.start(Catalin a.java:595)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:597)
          at org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:289)
          at org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:414)
          Caused by: java.lang.NoSuchMethodError: org.springframework.transaction.interceptor.Transa ctionInterceptor.setTransactionManagerBeanName(Lja va/lang/StringV
          at org.springframework.data.repository.core.support.T ransactionalRepositoryProxyPostProcessor.<init>(Tr ansactionalRepositoryProxyPostProcessor.java:76)
          at org.springframework.data.repository.core.support.T ransactionalRepositoryFactoryBeanSupport.setBeanFa ctory(TransactionalRepositoryFactoryBeanSupport.ja va:98)
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeAwareMethods(Abst ractAutowireCapableBeanFactory.java:1439)
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1408)
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:519)
          ... 26 more
          23-Jul-2011 12:54:55 org.apache.catalina.core.StandardContext listenerStart
          SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListe ner


          MY appcontext.xml file
          ---------------

          <?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="http://www.springframework.org/schema/p"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:jpa="http://www.springframework.org/schema/data/jpa"
          xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schem...-beans-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schem...ng-context.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schem...ng-mvc-3.0.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schem...ng-aop-3.0.xsd
          http://www.springframework.org/schema/data/jpa
          http://www.springframework.org/schem...spring-jpa.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

          <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager"
          p:entityManagerFactory-ref="entityManagerFactory" />

          <bean id="assuranceRepositoryDataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
          <property name="driverClassName"
          value="${assurance.repository.datasource.drivercla ssname}" />
          <property name="url" value="${assurance.repository.datasource.url}" />
          <property name="username" value="${assurance.repository.datasource.username} " />
          <property name="password" value="${assurance.repository.datasource.password} " />
          </bean>

          <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean"
          p:dataSource-ref="assuranceRepositoryDataSource"
          persistenceUnitName="assuranceRepository-jpa">
          <property name="jpaVendorAdapter">
          <bean class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter" />
          </property>
          </bean>

          <tx:annotation-driven transaction-manager="transactionManager" />

          <jpa:repositories base-package="com.assurance.repository" entity-manager-factory-ref="entityManagerFactory">
          </jpa:repositories>

          </beans>

          please help me ....

          Comment

          Working...
          X