【问题标题】:Groovy SQL Connections not being closedGroovy SQL 连接未关闭
【发布时间】:2012-11-28 17:39:47
【问题描述】:

我正在运行一些单元测试,但我看到一些奇怪的行为,即使用后连接未释放到池中(当测试数量达到池大小时,这不可避免地导致单元测试挂起)

为了演示,我创建了一个非常简单的单元测试:

@Before void setUp(){
    sql = new Sql(getDataSource())
    println getDataSource().getNumActive()
}

@After void tearDown(){
    sql.close()
}

@Test void test1(){
    println sql.rows("select 1")
}
@Test void test2(){
    println sql.rows("select 1")
}
@Test void test3(){
    println sql.rows("select 1")
}
@Test void test4(){
    println sql.rows("select 1")
}

在我的设置方法中,我的 getDataSource() 方法只返回一个静态的、已初始化的 BasicDataSource(因此每次都是同一个数据源)。

我还在拆解方法中对我的 Sql 对象显式调用 close,尽管 Groovy 在其 docs 中说在使用 DataSource 构造 Sql 对象时不必这样做

public Sql(javax.sql.DataSource dataSource)
Constructs an SQL instance using the given DataSource. Each operation will use a Connection from the DataSource pool and close it when the operation is completed putting it back into the pool.

但是,当我运行测试时,活动连接的数量继续增加,如果我将最大池大小设置为 2,那么它将在第二次测试后无限期挂起。

谁能告知为什么连接没有被返回?

谢谢。

【问题讨论】:

    标签: sql groovy connection datasource


    【解决方案1】:

    好吧,这是一个愚蠢的问题..

    我没有注意到 testclass 正在扩展另一个类,其中包含重复的 @Before 设置方法,因此显然是在对池做一些有趣的事情。删除已解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2014-12-18
      相关资源
      最近更新 更多