Spring / DbUnit 在一次测试后关闭连接

Spring / DbUnit closes connection after one test

因此,我们需要为 Java 配置的 Spring 应用程序 (3.2.3) 和 Oracle 数据库 编写集成测试。有一个单独的模式,由 Spring-test-DbUnit 填充,第一个测试运行没有任何问题。以下所有其他测试,无论它们是否在同一个 class 中,都会因连接关闭而失败:

java.sql.SQLRecoverableException: closed connection
    at oracle.jdbc.driver.PhysicalConnection.createStatement(PhysicalConnection.java:3423)
    at oracle.jdbc.driver.PhysicalConnection.createStatement(PhysicalConnection.java:3398)
    at org.dbunit.database.statement.AbstractBatchStatement.<init>(AbstractBatchStatement.java:50)
    at org.dbunit.database.statement.SimpleStatement.<init>(SimpleStatement.java:49)
    at org.dbunit.database.statement.PreparedStatementFactory.createBatchStatement(PreparedStatementFactory.java:57)
    at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:85)
    at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
    at com.github.springtestdbunit.DbUnitRunner.setupOrTeardown(DbUnitRunner.java:194)
    at com.github.springtestdbunit.DbUnitRunner.beforeTestMethod(DbUnitRunner.java:66)
    at com.github.springtestdbunit.DbUnitTestExecutionListener.beforeTestMethod(DbUnitTestExecutionListener.java:186)
    at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:348)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

这是我们的数据源:

@Override
@Bean
public DataSource dataSource()
{
    String conString = "jdbc:oracle:thin:@localhost:1521:XE";
    String username = "john";
    String password = "doe";

    Driver driver = new OracleDriver();
    SimpleDriverDataSource dataSource = new SimpleDriverDataSource(driver, conString, username, password);

    return dataSource;
}

这是一个示例集成测试class:

@ContextConfiguration(classes = { IntegrationTestConfiguration.class })
@ActiveProfiles(Constants.PROFILE_INTEGRATION)
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class, ForeignKeyDisabler.class })
@DbUnitConfiguration(databaseConnection = "oracleConnection")
@DatabaseSetup("/database/snapshot/snapshot1.xml")
public class IntegrationTest extends AbstractTransactionalJUnit4SpringContextTests
{
    @Autowired
    private NewsService newsService;


    @Test
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void testSpringConfiguration()
    {
        this.assertNewsSize(1);

        News news1 = new News();
        news1.setTitle("Test News 1");
        News savedNews1 = this.newsService.save(news1);
        Assert.assertTrue(savedNews1.getId() > 0);

        News news2 = new News();
        news2.setTitle("Test News 2");
        News savedNews2 = this.newsService.save(news2);
        Assert.assertTrue(savedNews2.getId() > 0);

        News news3 = new News();
        news3.setTitle("Test News 3");
        News savedNews3 = this.newsService.save(news3);
        Assert.assertTrue(savedNews3.getId() > 0);

        this.assertNewsSize(4);
    }


    @Test
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void testSpringConfigurationAgain()
    {
        this.assertNewsSize(1);

        News news1 = new News();
        news1.setTitle("Test News 1");
        News savedNews1 = this.newsService.save(news1);
        Assert.assertTrue(savedNews1.getId() > 0);

        News news2 = new News();
        news2.setTitle("Test News 2");
        News savedNews2 = this.newsService.save(news2);
        Assert.assertTrue(savedNews2.getId() > 0);

        News news3 = new News();
        news3.setTitle("Test News 3");
        News savedNews3 = this.newsService.save(news3);
        Assert.assertTrue(savedNews3.getId() > 0);

        this.assertNewsSize(4);
    }


    private void assertNewsSize(int newsSize)
    {
        List<News> allNews = this.newsService.getNews();
        Assert.assertEquals(newsSize, allNews.size());
    }
}

这可能是 Spring 在 committing/rolling 返回事务后关闭连接的数据库连接行为的问题?如果是,我该如何解决?我最后一次尝试是在 class/method 基础上创建交易,但没有成功。

提前致谢。

我相信它与您的 DirtiesContextTestExecutionListener.class 有关,这相当于 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/annotation/DirtiesContext.html.

Test annotation which indicates that the ApplicationContext associated with a test is dirty and should be closed: ...

after each test method in the current test class, when declared at the class level with class mode set to AFTER_EACH_TEST_METHOD

所以可能 spring 在第一个测试方法后关闭你的 bean。也许在 @AfterMethod 方法中清理数据库更容易?每次测试后都会运行。

感谢 freakman,我们找到了一个似乎对我们有用的解决方案:

我们创建了自己的 classes

  • OurOwnDbUnitTestExecutionListener.java
  • OurOwnDbUnitRunner.java

基于Spring-test-DbUnit 的

  • com.github.springtestdbunit.DbUnitTestExecutionListener.java
  • com.github.springtestdbunit.DbUnitRunner.java

class是的。 请注意,由于使用包范围 classes,我们的 classes 的包名称必须相等。 因为侦听器在非-可配置的方式,我们使用我们自己的 DbUnit runner:

private static OurOwnDbUnitRunner runner = new OurOwnDbUnitRunner();

运行程序已修改,因此在完成测试方法后不会关闭连接。第 116 行已删除:

// testContext.getConnections().closeAll();

最后,我们在测试中使用修改后的 DbUnitTestExecutionListener class: @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class, OurOwnDbUnitTestExecutionListener.class, // <<< this does the trick ForeignKeyDisabler.class })

如果您正在使用 spring-test-dbunit,另一个解决方案可能是提供一个覆盖关闭方法的 dbUnitDatabaseconnection:

@Bean
public IDatabaseConnection dbUnitDatabaseConnection(DataSource dataSource) throws SQLException, DatabaseUnitException {
    return new CustomDatabaseConnection(dataSource.getConnection());
}

private static class CustomDatabaseConnection extends DatabaseConnection {
    public CustomDatabaseConnection(Connection connection) throws DatabaseUnitException {
        super(connection);
    }


    @Override
    public void close() {
        // don't close the connection
    }
}