Spring Batch 事务异常:在 JobRepository 中检测到现有事务 [英] Spring Batch Transaction Exception:Existing transaction detected in JobRepository

查看:61
本文介绍了Spring Batch 事务异常:在 JobRepository 中检测到现有事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试此操作并获得异常:java.lang.IllegalStateException:在 JobRepository 中检测到现有事务.请修复此问题并重试(例如,从客户端删除 @Transactional 注释).

I am trying this and get exception: java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again (e.g. remove @Transactional annotations from client).

有人遇到过这个问题吗?

Is there anyone who have encountered this problem?

   @Transactional(propagation = Propagation.REQUIRED)
public void method1() // this method must be Transactional 
{

    ...    /*code to call JMS services*/

  method2();
}

@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void method2()
{
  batchService.runJobWithId(123L);
}

推荐答案

我是这样解决这个问题的:

I solved this issue like this:

  • 专门创建一个新 bean 来启动批处理作业.
  • 在该 bean 上添加 @Transaction(propagation = Propagation.NOT_SUPPORTED).
  • 为 spring 批处理配置任务执行器.类似于:

spring-batch.xml:

spring-batch.xml:

<beans:bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <beans:property name="jobRepository" ref="jobRepository" />
    <beans:property name="taskExecutor" ref="taskExecutor" />
</beans:bean>

应用程序上下文.xml

Application-context.xml

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="5" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="keepAliveSeconds" value="120" />
</bean>

诀窍是任务执行器.如果您检查日志,spring batch 会抱怨,因为没有定义任务执行器,它会恢复到同步任务执行,它在同一个线程上.使用上述配置的作业将在不同的线程上启动.

What does the trick is the task executor. If you check your logs spring batch will complain that, since no task executor is defined, it reverts to synchronous task execution, which is on the same thread. Using the configuration above jobs will be launched on a different thread.

重要提示尽管我在没有配置 taskExecutor 的情况下管理了一段时间,但当我添加了一个 JobListener 来更新我自己的表的工作开始和结束时间时,我遇到了暮光之城.当 StartJob() 调用返回时,jobListener 执行的任何数据库更新都将被恢复.因此,我建议您在单独的线程中执行作业.更简单.更干净.

Important note Altough I managed for a while without configuring a taskExecutor, I ran into the twilight zone when I added a JobListener to update my own tables with start and end times for the job. Any DB Updates the jobListener performed would be reverted when the StartJob() call returned. So, I recommend that you execute your jobs in a separated thread. Simpler. Cleaner.

这篇关于Spring Batch 事务异常:在 JobRepository 中检测到现有事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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