【发布时间】: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