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

时间:2015-07-30 10:06:20

标签: spring oracle datasource spring-test-dbunit

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

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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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;
}

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

@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提交/回滚事务后关闭连接的Spring数据库连接行为的问题吗?如果是,我该如何解决?我的最后一次尝试是在类/方法基础上创建事务但没有成功。

提前谢谢。

3 个答案:

答案 0 :(得分:2)

我认为它与你的DirtiesContextTestExecutionListener.class有关,而 private void rich1_SelectionChanged(object sender, EventArgs e) { rich2.SelectionLength = rich1.SelectionLength; rich2.SelectionStart = rich1.SelectionStart; } private void button2_Click(object sender, EventArgs e) { rich2.SelectedRtf = @"{\rtf1\ansi{colour=12}" + rich2.SelectedRtf; rich1.ForeColor = Color.Blue; } 与某种形式相当 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/annotation/DirtiesContext.html

  

测试注释,指示与测试关联的ApplicationContext是脏的并且应该关闭:   ...

     

在当前测试类中的每个测试方法之后,在声明时   类模式设置为AFTER_EACH_TEST_METHOD

的类级别

所以春天可能会在第一次测试方法后关闭你的豆子。也许在@AfterMethod方法中更容易清理数据库?它将在每次测试后运行。

答案 1 :(得分:0)

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

我们创建了自己的类

  • OurOwnDbUnitTestExecutionListener.java
  • OurOwnDbUnitRunner.java

基于Spring-test-DbUnit&#39;

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

类。 请注意,由于使用了包作用域的类,我们类的包名必须相同。由于监听器以不可配置的方式使用转轮,我们使用自己的DbUnit转轮:

private static OurOwnDbUnitRunner runner = new OurOwnDbUnitRunner();

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

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

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

答案 2 :(得分:0)

如果您使用的是spring-test-dbunit,另一种解决方案可能是提供dbUnitDatabaseConnection来覆盖close-method:

@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
    }
}