当前位置: 首页 > 知识库问答 >
问题:

Spring异步作业中出现异常-InvalidDataAccessApiUsageException:没有正在进行的事务

元嘉木
2023-03-14

场景是,我有一个带有spring批处理作业的spring启动应用程序。我正在尝试使用@EnableAsyncThreadPoolTaskExecutor使批处理作业异步,然后将此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

谢谢你的帮助!

共有3个答案

慕学海
2023-03-14

我已经在其他上下文中遇到了这种问题,并且只有一种有效的方法可以找到来源:将断点放在org.hibernate.internal.SessionImpl.checkTransactionNeed和org.springframework.transaction.interceptor.TransactionAbulSupport.invokeAnd inTransaction中试着理解为什么后面创建的事务(应该是)第一个不能识别。

史商震
2023-03-14

JobExecution#beforeJob不在Spring Batch管理的事务范围内执行。您可能需要将@Transactional添加到WriteToFileJobExecutionListener#beforeJob方法中。

寇桐
2023-03-14

@Transactional(propagation=propagation.REQUIRES_NEW)注释负责数据库访问逻辑的作业方法。

 类似资料:
  • 有人能帮我解决这个问题吗?

  • 我正在用异步JobLauncher在Spring Batch中配置一个(长时间运行的)作业,我有两个RESTendpoint: null 谢谢朱利奥

  • 我有一个JPA web应用程序,其中包含一些针对JPA存储库的集成测试。目前还没有针对Neo4J存储库的集成测试。 现在,我已经向这个现有的JPA web应用程序添加了一些Neo4J功能。 我现在使用Neo4J存储库和JPA存储库。我的实体和存储库的名称不同,并且位于不同的包中。 我的所有测试都扩展了以下类: 当应用程序配置没有任何配置时,测试可以正常运行 但在添加Neo4J配置时出现异常错误:

  • 我已经阅读了Spring@Transactional-Isolation,propagation中的指令 但是我不明白我解释的这个特性是如何产生的:我有一个异步方法,像这样 此方法(setFlag)在调用异步方法之前通过1L调用一次 并且在异步方法中的所有操作结束时传递零 但是,如果异步方法因某些问题而失败,则catch分支中的调用将失败,因为 且标志保持为1。 所以,我希望我是清楚的,但是...

  • 我正在使用RESTEasy与Spring MVC的集成,如“39.2.Spring MVC集成”一节http://docs.jboss.org/RESTEasy/docs/2.0.0.ga/userguide/html/resteasy_spring_integration.html所述 我想试验一下Resteasy对“异步作业服务”的实现,如下所述:http://docs.jboss.org/r

  • 我正在Spring3和Hibernate3中进行产品构建,我已经更新了这个Hibernate5和Spring5。现在我可以部署应用程序了,但是当我尝试与数据库连接时,会得到“javax.persistence.TransactionRequiredException:no transaction is in progress”