hibernate SimpleTask执行器线程中的实体管理器对象

eqfvzcg8  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(103)

我想做一些异步的数据库操作。我使用的是spring 3.0。所以我使用了@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();
             }
         }
    }

自动结转.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.

}

}

我收到以下错误
数据访问权限无效应用程序使用异常:没有正在进行的交易;嵌套异常为javax.persistence.TransactionRequiredException:没有正在进行的事务

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)
a0zr77ik

a0zr77ik1#

尝试像下面这样将@Transactional放在你的方法上

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

因此,基本上发生的情况是,您的数据库操作不在任何活动事务中,因此您必须进行手动txn管理,或者必须使用@Transactional进行自动事务管理

相关问题