【问题标题】:Entity Manager Object in SimpleTask executor threadSimpleTask 执行器线程中的实体管理器对象
【发布时间】:2014-06-04 10:47:14
【问题描述】:

我想异步执行一些数据库操作。我正在使用弹簧 3.0。所以我使用了@Async注解。

     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 类:

   @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.

}

}

我收到以下错误

org.springframework.dao.InvalidDataAccessApiUsageException:没有事务正在进行;嵌套异常是 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)

原因:javax.persistence.TransactionRequiredException:没有正在进行的事务

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)

【问题讨论】:

    标签: java spring hibernate jpa spring-ioc


    【解决方案1】:

    尝试将@Transactional 放在您的方法上,如下所示

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

    所以基本上你的数据库操作不在任何会话中,所以你要么必须手动创建会话,要么必须使用自动事务管理

    【讨论】:

    • 主线程和辅助线程都访问的 MyDao 类。在这种情况下,我如何生成实体管理器并传递它。它在类本身中使用默认实体管理器。
    猜你喜欢
    • 2016-07-02
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多