场景是,我有一个弹簧批处理作业的弹簧启动应用程序。我尝试使用@EnableAsync
和ThreadPoolTaskExecutor
使批处理作业异步,然后将此taskExecutor分配给JobLauncher。在此更改之后,作业以异步方式运行,但我遇到了持久化或更新数据库的问题:
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:413)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:489)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at com.abc.fin.rec.batch.dao.impl.CustomerDaoImpl$$EnhancerBySpringCGLIB$$b55f85f1.updateRequestSummary(<generated>)
at com.abc.fin.rec.batch.service.impl.BatchServiceImpl.updateRequestSummary(BatchServiceImpl.java:122)
at com.abc.fin.rec.batch.service.impl.BatchServiceImpl$$FastClassBySpringCGLIB$$b4c3b9f7.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at com.abc.fin.rec.batch.service.impl.BatchServiceImpl$$EnhancerBySpringCGLIB$$b9160b39.updateRequestSummary(<generated>)
at com.abc.fin.rec.batch.cust.match.req.WriteToFileJobExecutionListener.beforeJob(WriteToFileJobExecutionListener.java:97)
at org.springframework.batch.core.listener.CompositeJobExecutionListener.beforeJob(CompositeJobExecutionListener.java:73)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:303)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.cloud.sleuth.instrument.async.SpanContinuingTraceRunnable.run(SpanContinuingTraceRunnable.java:52)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3430)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1397)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1393)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)
at com.sun.proxy.$Proxy133.flush(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
at com.sun.proxy.$Proxy127.flush(Unknown Source)
at com.abc.fin.rec.batch.dao.impl.CustomerDaoImpl.updateRequestSummary(CustomerDaoImpl.java:138)
at com.abc.fin.rec.batch.dao.impl.CustomerDaoImpl$$FastClassBySpringCGLIB$$333ee7df.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 26 common frames omitted
感谢您的帮助!
答案 0 :(得分:0)
用@Transactional(propagation = Propagation.REQUIRES_NEW)
注释处理数据库访问逻辑的作业方法。
答案 1 :(得分:0)
JobExecution#beforeJob
未在Spring Batch管理的事务范围内执行。您可能希望将@Transactional
添加到WriteToFileJobExecutionListener#beforeJob
方法。
答案 2 :(得分:0)
我已经在其他环境中遇到过这种问题,只有一种有效的方法可以找到来源: 在org.hibernate.internal.SessionImpl.checkTransactionNeeded中放置断点 在org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction中 然后尝试理解为什么后来创建的事务(假设确实如此)不会被第一个人识别。