spring-batch - 将 Spring Batch 与 JtaTransactionManager 一起使用时出现 java.lang.StackOverflowError 错误

标签 spring-batch stack-overflow jta transactionmanager

我正在尝试将 JtaTransactionManager 与 Spring Batch 一起使用。

transactionManager 是依赖项目中已经定义好的 Jta 事务管理器。

这是我的 Spring 批处理配置:

@Configuration
@EnableBatchProcessing
public class IntegrationJobConfig implements BatchConfigurer {

@Inject
private JobBuilderFactory jobs;

@Inject
private StepBuilderFactory stepBuilderFactory;

@Inject
private DataSource dataSource;

@Inject
private PlatformTransactionManager transactionManager;

@Override
public PlatformTransactionManager getTransactionManager() {
    return transactionManager;
}

@Override
public JobRepository getJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(dataSource);
    factory.setTransactionManager(getTransactionManager());
    factory.afterPropertiesSet();
    return  factory.getObject();
}

@Override
public JobLauncher getJobLauncher() throws Exception {
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(getJobRepository());
    jobLauncher.afterPropertiesSet();
    return jobLauncher;
}

@Override
public JobExplorer getJobExplorer() throws Exception {
    JobExplorerFactoryBean jobExplorer = new JobExplorerFactoryBean();
    jobExplorer.setDataSource(dataSource);
    jobExplorer.afterPropertiesSet();
    return jobExplorer.getObject();
}

以及我如何从 quartz 调度程序作业启 Action 业:
public class IncomingIntegrationJob extends QuartzJobBean {

@Inject
private JobLauncher jobLauncher;

@Inject
private Job myJob;

 @Override
 protected void executeInternal(JobExecutionContext context) {

  JobParametersBuilder builder = new JobParametersBuilder();
  builder.addDate("execDate", new Date());

  JobExecution result = jobLauncher.run(myJob, builder.toJobParameters());

 }
}

但是当我启动上面的 jobLauncher 时,它抛出 StackOverFlowError,似乎错误是一个无限循环。
[m2016-06-12 14:58:00,108 INFO  [smrp-scheduler_Worker-1] [32morg.springframework.batch.core.repository.support.JobRepositoryFactoryBean No database type set, using meta data indicating: H2
[m2016-06-12 14:58:00,420 INFO  [smrp-scheduler_Worker-1] [32morg.springframework.batch.core.repository.support.JobRepositoryFactoryBean No database type set, using meta data indicating: H2
[m2016-06-12 14:58:00,423 INFO  [smrp-scheduler_Worker-1] [32morg.springframework.batch.core.launch.support.SimpleJobLauncher No TaskExecutor has been set, defaulting to synchronous executor.
[m2016-06-12 14:58:00,551 ERROR [smrp-scheduler_Worker-1] [1;31morg.quartz.core.JobRunShell Job inbound-batch-group.inbound-batch-job threw an unhandled Exception: 
java.lang.StackOverflowError
at java.lang.reflect.Method.hashCode(Method.java:331) ~[?:1.8.0_91]
at org.springframework.aop.framework.AdvisedSupport$MethodCacheKey.<init>(AdvisedSupport.java:597) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.aop.framework.AdvisedSupport.getInterceptorsAndDynamicInterceptionAdvice(AdvisedSupport.java:486) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:193) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at com.sun.proxy.$Proxy250.getTransaction(Unknown Source) ~[?:?]
at sun.reflect.GeneratedMethodAccessor182.invoke(Unknown Source) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_91]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at com.sun.proxy.$Proxy250.getTransaction(Unknown Source) ~[?:?]
at sun.reflect.GeneratedMethodAccessor182.invoke(Unknown Source) ~[?:?]

直到最后:
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_91]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at com.sun.proxy.$Proxy250.getTransaction(Unknown Source) ~[?:?]
at sun.reflect.GeneratedMethodAccessor182.invoke(Unknown Source) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_91]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
Wrapped by: org.quartz.SchedulerException: Job threw an unhandled exception.
at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [quartz-2.2.3.jar:?]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.3.jar:?]
  • 事务使用 AspectJ 编译时编织

  • @EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
  • 使用相同事务时,依赖项项目正常运行
  • 该错误仅发生在我正在处理的项目中。

  • 谁能帮我指出可能导致问题的原因?
    谢谢。

    最佳答案

    我知道已经有一段时间了,但是在将 Spring Boot 迁移到版本 2 时我遇到了同样的问题。

    如果我们需要在批处理和应用程序中的其他模 block 中使用相同的事务管理器,我们将覆盖批处理配置器和事务管理器。

    在我的情况下,通过在配置批处理配置器之前使用 @EnableBatchProcessing 过程解决了该错误。所以只需将@EnableBatchProcessing 放在为我工作的运行者上。

    希望能帮助到你。

    关于spring-batch - 将 Spring Batch 与 JtaTransactionManager 一起使用时出现 java.lang.StackOverflowError 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37771696/

    相关文章:

    .net - 如何更改.NET程序的堆栈大小?

    Spring JTA 事务与 Websphere 的 JPA 和 jndi 数据源

    java - 未找到产品名称 : [Impala] 的数据库类型

    spring-batch - Spring Batch FlatFileItemWriter - 如何使用 stepExecution.jobId 生成文件名

    java - Spring批处理不将作业实例插入数据库

    crash - 在CentOS6.3 x86-64中测试无限递归js时V8崩溃

    java - 如何避免递归函数的 StackOverflowError

    sql-server - 分布式事务的性能开销

    java - 如何打印 JDBC 的连接 url?

    java - 外部设置日志级别log4j