java—使用spring批处理而不使用事务

7xllpg7q  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(672)

我有一个spring批处理作业,它在第一步/tasklet中调用另一个作业。当然,这不适用于默认情况下作业如何处理事务。
在我的应用程序中,我并不需要那么多事务,而且这些步骤也没有数据库连接,所以我宁愿完全删除事务。为此,对于这两项工作的每一步,我都做了以下工作:

public Step stepX() {
    return this.stepBuilderFactory
        .get("stepX")
        .tasklet(new StepXTasklet())
        .transactionAttribute(transactionAttribute())
        .build();
  }

  private TransactionAttribute transactionAttribute() {
    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    attribute.setPropagationBehavior(Propagation.SUPPORTS.value());
    return attribute;
  }

应用程序仍将引发此异常:

org.springframework.dao.OptimisticLockingFailureException: Attempt to update step execution id=1 with wrong version (2), where current version is 3
    at org.springframework.batch.core.repository.dao.MapStepExecutionDao.updateStepExecution(MapStepExecutionDao.java:106)
    at org.springframework.batch.core.repository.support.SimpleJobRepository.update(SimpleJobRepository.java:196)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:353)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy150.update(Unknown Source)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)

这就是springbatch如何呈现其内部 TransactionSuspensionNotSupportedException 因为某种原因。
所以有些东西仍然在使用事务,我只是不知道是什么,以及如何删除它们。也许是 JobExecutionListener ? 也许是大块?
我倾向于块,因为在第二块之后(按读取的行数计算),在第三块之前,我得到了输出:

Commit failed while step execution data was already updated. Reverting to old version.

如果有区别,父作业是“流”作业,子作业是“读写器”。
spring批处理作业框架的哪些部分使用事务,如何确保这些部分停止使用它们?

bgibtngc

bgibtngc1#

问题在于默认作业存储库。它的事务处理似乎有问题。若要解决此问题,请将其替换为具有内存中数据库的jdbc作业存储库。只需将此类添加到spring上下文:

@Configuration
@EnableBatchProcessing
public class InMemoryBatchContextConfigurer extends DefaultBatchConfigurer {

  @Override
  protected JobRepository createJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDatabaseType(DatabaseType.H2.getProductName());
    factory.setDataSource(dataSource());
    factory.setTransactionManager(getTransactionManager());
    return factory.getObject();
  }

  public DataSource dataSource() {
    EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder();
    return embeddedDatabaseBuilder
        .addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
        .addScript("classpath:org/springframework/batch/core/schema-h2.sql")
        .setType(EmbeddedDatabaseType.H2).build();
  }
}

相关问题