gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:46:15 24 4
gpt4 key购买 nike

我想异步执行一些数据库操作。我用的是spring 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:没有事务正在进行

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)

最佳答案

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

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

所以基本上你的数据库操作不在任何 Activity 事务中发生,所以你要么必须进行手动 txn 管理,要么必须使用 @Transactional 使用自动事务管理

关于java - SimpleTask 执行器线程中的实体管理器对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24035404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com