使用 @JpaDataTest 测试 Spring 批处理 [英] Test Spring batch with @JpaDataTest

查看:66
本文介绍了使用 @JpaDataTest 测试 Spring 批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring 批次 4.0,我正在尝试测试我的批次.我会将嵌入式数据库 h2 与 @JpaDataTest 一起使用,但它不起作用.当我添加此注释时出现错误

<块引用>

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

@Test 上的@Transaction(propagation=Propagation.NEW_REQUIRED) 不起作用.

我试图从@JpaDataTest 复制每个注释并删除@Transaction

@BootstrapWith(SpringBootTestContextBootstrapper.class)@OverrideAutoConfiguration(enabled = false)@AutoConfigureCache@AutoConfigureDataJpa@AutoConfigureTestDatabase@AutoConfigureTestEntityManager@ImportAutoConfiguration

但是当我这样做时,我丢失了 EntityManager...

有人已经找到解决方案了吗?

解决方案

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

当您尝试在外部事务上下文(示例中的测试)中运行 Spring Batch 代码(驱动事务)时,会发生此错误.

与其在测试中添加 @Transaction(propagation=Propagation.NEW_REQUIRED),不如尝试停用事务,让 Spring Batch 驱动事务.例如,使用:

@Transaction(propagation = Propagation.NOT_SUPPORTED)

<块引用>

我试图从@JpaDataTest 复制每个注释并删除@Transaction [...] 但是当我这样做时,我丢失了 EntityManager...

您需要确保 Spring Batch 使用您想要的事务管理器(在您的情况下我猜是 JpaTransactionManager)来驱动其事务.为此,您需要在批处理配置中定义 BatchConfigurer 类型的 bean 并覆盖 getTransactionManager.下面是一个例子:

@Bean公共 BatchConfigurer batchConfigurer() {返回新的 DefaultBatchConfigurer() {@覆盖公共 PlatformTransactionManager getTransactionManager() {返回新的 MyTransactionManager();}};}

您可以在中找到更多详细信息参考文档的 Java 配置部分.

希望这会有所帮助.

I'm using spring batch 4.0 and i'm trying to test my batch. I would use embedded database h2 with @JpaDataTest but it doesn't work. When i add this annotation i got error

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

@Transaction(propagation=Propagation.NEW_REQUIRED) on the @Test dosen't work.

I tried to copy every annotations from @JpaDataTest and remove @Transaction

@BootstrapWith(SpringBootTestContextBootstrapper.class)
@OverrideAutoConfiguration(enabled = false)
@AutoConfigureCache
@AutoConfigureDataJpa
@AutoConfigureTestDatabase
@AutoConfigureTestEntityManager
@ImportAutoConfiguration

But when i'm doing this, i'm lossing the EntityManager...

Someone already find a solution ?

解决方案

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

This error happens when you try to run Spring Batch code (which drives a transaction) in an outer transactional context (the test in your example).

Instead of adding @Transaction(propagation=Propagation.NEW_REQUIRED) on the test, you should try to deactivate transactions instead, letting Spring Batch drive the transaction. For example, using:

@Transaction(propagation = Propagation.NOT_SUPPORTED)

I tried to copy every annotations from @JpaDataTest and remove @Transaction [...] But when i'm doing this, i'm lossing the EntityManager...

You need to make sure that Spring Batch uses the transaction manager you want (I guess a JpaTransactionManager in your case) to drive its transactions. To do that, you need to define a bean of type BatchConfigurer in your batch configuration and override getTransactionManager. Here is an example:

@Bean
public BatchConfigurer batchConfigurer() {
    return new DefaultBatchConfigurer() {
            @Override
            public PlatformTransactionManager getTransactionManager() {
                    return new MyTransactionManager();
            }
    };
}

You can find more details in the Java Configuration section of the reference documentation.

Hope this helps.

这篇关于使用 @JpaDataTest 测试 Spring 批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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