Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring事务的JUnit多线程测试_Java_Spring_Multithreading_Junit_Transactions - Fatal编程技术网

Java 使用Spring事务的JUnit多线程测试

Java 使用Spring事务的JUnit多线程测试,java,spring,multithreading,junit,transactions,Java,Spring,Multithreading,Junit,Transactions,我正在运行JUnit集成测试,内存中的H2数据库配置如下: @Bean public DataSource dataSource() { try { SimpleDriverDataSource simpleDriverDataSource = new SimpleDriverDataSource(); simpleDriverDataSource.setDriverClass((Class<? extends Driver>) ClassU

我正在运行JUnit集成测试,内存中的H2数据库配置如下:

@Bean
public DataSource dataSource() {

    try {
        SimpleDriverDataSource simpleDriverDataSource = new SimpleDriverDataSource();
        simpleDriverDataSource.setDriverClass((Class<? extends Driver>) ClassUtils.forName("org.h2.Driver", this.getClass().getClassLoader()));
        simpleDriverDataSource.setUrl("jdbc:h2:file:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MVCC=true;FILE_LOCK=NO;mv_store=false");
        simpleDriverDataSource.setUsername("sa");
        simpleDriverDataSource.setPassword("");

        return simpleDriverDataSource;
    } catch(ClassNotFoundException e) {
        throw new IllegalStateException(e.getMessage());
    }

}
@Bean
公共数据源数据源(){
试一试{
SimpleDriverDataSource SimpleDriverDataSource=新SimpleDriverDataSource();

simpleDriverDataSource.setDriverClass((Class假设您的测试是
@Transactional
),您看到的是预期的行为,因为您(通常)不希望测试所做的更改对其他测试/线程可见,也不希望在测试结束时回滚

解决此问题的一个简单方法是如下注释
@Test
方法

@Test
@Transactional(propagation = Propagation.NEVER)
public void testYourLogics() { /* ... */ }
我已经能够在没有
propagation=propagation.NEVER
的情况下重现巡演行为,并看到它得到了解决

请注意,运行测试后,您必须手动将数据库恢复到干净状态,否则该测试所做的更改将对其他测试可见。