Search code examples
javaspringhibernatejpaspring-ioc

Entity Manager Object in SimpleTask executor thread


I want to do some database operation asynchronously. I am using spring 3.0. So i used @Async annotation.

     public class TaskExecutorExample 
     {
         public  static void main(String a[])  
         {
             ApplicationContext applicationContext = new FileSystemXmlApplicationContext("spring.xml");
             AutoCarryover service = applicationContext.getBean(AutoCarryover.class);

             try{
                service.writeSomethingASync(); //starting async task
             }catch(InterruptedException r)
             {
                 r.printStackTrace();
             }
         }
    }

AutoCarryover.class

   @Service
       public class AutoCarryover {

               @Autowired
               private MyDao mydao;


                @Async
                 public void writeSomethingASync() throws InterruptedException{

                 mydao.insertData(testObj); // This is started from second thread

 }

}

MYDAO class:

   @Repository
    public class MyDao  {


/**
 * Field entityManager.
 */
@PersistenceContext
private EntityManager entityManager;


public void insertData(TestObj test) {

        entityManager.merge(test); // Here i am inserting from second thread. Its throwing error.

}

}

I am getting following error

org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress

at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:306)

at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:104)

at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)

at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)

at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)

at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

at com.sun.proxy.$Proxy653.mergeCadPartVehAccyDescInstall(Unknown Source)

at com.tms.cad.aror.service.eng.carryOver.admin.impl.AutoCarryOverServiceImpl.insertCadPartVehAccyDescInstallTarget(AutoCarryOverServiceImpl.java:249)

at com.tms.cad.aror.service.eng.carryOver.admin.impl.AutoCarryOverServiceImpl.autoCarryOver(AutoCarryOverServiceImpl.java:175)

at com.tms.cad.aror.service.eng.carryOver.admin.impl.AutoCarryOverServiceImpl.run(AutoCarryOverServiceImpl.java:132)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)

at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)

at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:80)

at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)

at java.util.concurrent.FutureTask.run(FutureTask.java:138)

at java.lang.Thread.run(Thread.java:662)

Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress

at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:957)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)

at com.sun.proxy.$Proxy642.flush(Unknown Source)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)

at com.sun.proxy.$Proxy642.flush(Unknown Source)

at com.tms.cad.aror.service.dao.CadPartVehAccyDescInstallDao.mergeCadPartVehAccyDescInstall(CadPartVehAccyDescInstallDao.java:73)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)

at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)

at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

at com.sun.proxy.$Proxy653.mergeCadPartVehAccyDescInstall(Unknown Source)

Solution

  • Try to put @Transactional over your method like below

    @Async
    @Transactional
    public void writeSomethingASync() throws InterruptedException{
          mydao.insertData(testObj); // This is started from second thread
    }
    

    So basically what is happening your database operation is not in any active transaction so either u have to do manual txn management or have to use automatic transaction management using @Transactional