我想异步进行一些数据库操作。我使用的是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)
答案 0 :(得分:2)
尝试将@Transactional
放在您的方法上,如下所示
@Async
@Transactional
public void writeSomethingASync() throws InterruptedException{
mydao.insertData(testObj); // This is started from second thread
}
所以基本上发生了什么,你的数据库操作不在任何会话中,所以你必须手动创建会话或必须使用自动事务管理