TransactionRequiredException:使用JPAItemWriter时没有事务正在进行 [英] TransactionRequiredException: no transaction is in progress while using JPAItemWriter

查看:132
本文介绍了TransactionRequiredException:使用JPAItemWriter时没有事务正在进行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序是Spring Boot应用程序,我遇到了奇怪的问题.

I am facing weird issue with an application which is a spring boot application.

此处的详细信息:

该应用程序具有一个春季批处理作业,该作业使用JpaItemWriter来执行对数据库的写操作.

The app has a spring batch job which uses JpaItemWriter for performing the writes to the database.

此外,该应用程序被配置为使用休眠状态

Also, the application is configured to use hibernate

ItemWriter配置如下:

ItemWriter configuration as follows:

 @Bean(name = "itemWriter")
  @StepScope
  public ItemWriter<Record> itemWriter() {
      JpaItemWriter<Record> itemWriter = new JpaItemWriter<>();
      itemWriter.setEntityManagerFactory(emf);
      return itemWriter;
  }

过去,批处理工作非常好,但是最近我们将Jenkins升级为Jenkins(2.73.3).之后,使用新的Jenkins构建和部署代码 运行相同的批处理作业时开始引发以下错误:

The batch job used to work great, but recently we upgraded our Jenkins to Jenkins(2.73.3). After that, the code built and deployed using this new Jenkins started throwing the following error when the same batch job is run:

2017-12-03 16:09:44.720 DEBUG - testservice -  - jobLauncher-3 - org.springframework.batch.item.database.JpaItemWriter:97 - Writing to JPA with 4 items.
2017-12-03 16:09:44.733 DEBUG - testservice -  - jobLauncher-3 - org.springframework.batch.item.database.JpaItemWriter:109 - 4 entities merged.
2017-12-03 16:09:44.733 DEBUG - testservice -  - jobLauncher-3 - org.springframework.batch.item.database.JpaItemWriter:110 - 0 entities found in persistence context.
2017-12-03 16:09:44.743 WARN  - testservice -  - jobLauncher-3 - com.shared.domain.error.BatchExceptionHandler:63 - [test-service]999999999(Dn9lM4c)ExhaustedRetryException: Unable to locate ErrorDetail for Id ExhaustedRetryException. Details:
org.springframework.retry.ExhaustedRetryException: Retry exhausted after last attempt in recovery path, but exception is not skippable.; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
        at org.springframework.batch.core.step.item.FaultTolerantChunkProcessor$5.recover(FaultTolerantChunkProcessor.java:403) ~[spring-batch-core-3.0.7.RELEASE.jar!/:3.0.7.RELEASE]
        at org.springframework.retry.support.RetryTemplate.handleRetryExhausted(RetryTemplate.java:473) ~[spring-retry-1.1.5.RELEASE.jar!/:?]
        at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:333) ~[spring-retry-1.1.5.RELEASE.jar!/:?]
        at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:201) ~[spring-retry-1.1.5.RELEASE.jar!/:?]

使用Old Jenkins(Jenkins版本1.646)构建和部署相同的代码库时,工作正常.

The same code base when built and deployed using Old Jenkins (Jenkins ver. 1.646), the job works fine.

在两种情况下,都将应用程序部署在具有相同AMI的EC2实例(AWS)上.因此,它们所运行的服务器没有区别.

In both cases, the application is getting deployed on an EC2 instance ( AWS ) with same AMI. So there is no difference in the server which they are running on.

我还检查了用于在两个Jenkins版本上构建代码的Java版本和maven版本,它们是相同的.

Also, I checked the Java version and maven version used for building the code on both versions of Jenkins and they are same.

Maven版本和Java版本:

Apache Maven 3.0.4 (r1232337; 2012-01-17 08:44:56+0000)
Maven home: /opt/maven/apache-maven-3.0.4
Java version: 1.8.0_72, vendor: Oracle Corporation
Java home: /usr/java/jdk1.8.0_72/jre
Default locale: en_US, platform encoding: UTF-8

其他版本:

SpringBoot: 1.4.7.RELEASE
HibernateVersion: 5.2.3.Final

唯一的区别是旧的Jenkins在AWS RHEL上运行,而新的Jenkins在CentOS7上运行.

The only difference is the old Jenkins is running on AWS RHEL and the new one is running on CentOS7.

我什至都检查了在这两种情况下创建的罐子,似乎没有什么不同.

I even checked the jars created in both the cases and nothing seems to be different.

当前不确定如何进一步解决此问题

Currently not sure how to further troubleshoot this issue

已编辑:

我对两个Jenkins生成的代码进行了远程调试,以下是所做的观察.

I did remote debugging of the code generated by both the Jenkins, the following is the observations made.

JpaItemWriter在方法中调用"EntityManagerFactoryUtils.getTransactionalEntityManager" "emHolder"在一种情况下为null,在另一种情况下为有效值.

JpaItemWriter calls "EntityManagerFactoryUtils.getTransactionalEntityManager" and in the method "emHolder" is null in one case and has valid value in the other case.

    public static EntityManager doGetTransactionalEntityManager(
                EntityManagerFactory emf, Map<?, ?> properties, boolean synchronizedWithTransaction) throws PersistenceException {

            Assert.notNull(emf, "No EntityManagerFactory specified");

            EntityManagerHolder emHolder =
                    (EntityManagerHolder) TransactionSynchronizationManager.getResource(emf);

 // This line returns a valid emHolder
 // But in case of the code which is not working, it is returning a 
 // null value   
 EntityManagerHolder emHolder =
                    (EntityManagerHolder) TransactionSynchronizationManager.getResource(emf);

不确定为什么在一种情况下NamedThreadLocal>(事务资源")没有实体MangerHolder,而在另一种情况下没有.

Not sure why the NamedThreadLocal>("Transactional resources") does not have the entityMangerHolder in one case but not in other case.

更新

在进行更多调试时,发现在代码不起作用的情况下我发现了什么 春季批处理作业使用"DataSourceTransactionManager",但在其他情况下,它使用JpaTransactionManager.

Upon more debugging what I found in the case of code which is not working spring batch job uses "DataSourceTransactionManager" but in other case it uses the JpaTransactionManager.

推荐答案

我现在能够解决我的问题,这是我的发现:

I was able to solve my problem for now, here are my findings:

在这两种情况下,似乎豆的创建顺序有所不同.

Seems like there is a difference in the order in which the beans get created in both the cases.

我在配置类中定义了一个JpaTransaction管理器bean.此bean从未使用Jenkins 2.0生成的代码创建.

I had a JpaTransaction manager bean defined in of my configuration classes.This bean is never created with the code that was generated using the Jenkins 2.0.

Spring正在"org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration"类中生成代理事务管理器bean,并且此事务管理器无法与JpaItemWriter一起很好地工作.

Spring is generating a proxy transaction manager bean in the "org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration" class and this transaction manager is not working well with the JpaItemWriter.

我看过一张JIRA票证,其中讨论了SimpleBatchConfiguration的问题.

I had seen a JIRA ticket which discusses the problem with SimpleBatchConfiguration.

https://jira.spring.io/browse/BATCH-2294

在我的BatchConfiguration类中添加了相同的JpaTransaction bean之后, 问题消失了.

After I added the same JpaTransaction bean in my BatchConfiguration class, the problem disappeared.

这篇关于TransactionRequiredException:使用JPAItemWriter时没有事务正在进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆