【问题标题】:Problem with dbUnit: java.sql.SQLException: Closed StatementdbUnit 的问题:java.sql.SQLException: Closed Statement
【发布时间】:2009-10-21 14:45:23
【问题描述】:

我对 dbUnit 有一个奇怪的问题。 我在我的项目中使用 dbUnit 2.4.4、java 1.6、Spring(作为 db 连接池)、Oracle 9 进行了大约 50 个单元测试。 对于其中一些(当我运行整套测试时)我得到这样的异常:

Closed Statement
[junit] junit.framework.AssertionFailedError: Closed Statement
[junit]     at com.myproj.DataAccess.Internal.BaseDAOTest.importToDb(Unknown Source)
[junit]     at com.myproj.DataAccess.Internal.MyDAOTest.testGetBuyClientOrders(Unknown Source)
[junit]     at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
[junit]     at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
[junit]     at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)

importToDb 方法通过 dbUnit 的 DatabaseOperation.REFRESH.execute 方法将测试数据从 XML 文件加载到数据库,并用于所有测试。 如果我单独运行这些有问题的测试,它们就没有问题。 你有什么想法?谢谢!

【问题讨论】:

标签: java oracle dbunit


【解决方案1】:

我猜你的一些测试在清理时会关闭数据库连接。下一个测试尝试再次使用此连接进行导入并失败。

【讨论】:

  • 我没有在任何测试中明确关闭连接。它们都使用 Spring DAO 库中的 jdbcTemplate,所以我不需要手动操作连接。在导入函数中连接是关闭的,但是这个函数对于所有的测试都是一样的。
  • 您使用哪个数据源?是SingleConnectionDataSource吗?
  • 我的 Spring 配置中的数据源描述:
【解决方案2】:

当这种情况发生在我身上时,我们已经明确配置了连接缓存,以使用两个属性来狙击持久连接:

AbandonedConnectionTimeout
InactivityTimeout

See Oracle's docs here regarding timeout properties

原来查询+处理时间只是跳跃了两个属性结合的风口浪尖(AbandonedConnectionTimeout + InactivityTimeout

要解决此问题,您可以提高超时限制,也可以通过将超时设置为 0(默认值)来消除超时

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多