Groovy SQL连接未关闭(Groovy SQL Connections not being closed)

编程入门 行业动态 更新时间:2024-10-10 03:23:19
Groovy SQL连接未关闭(Groovy SQL Connections not being closed)

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

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

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

在我的setup方法中,我的getDataSource()方法只返回一个静态的,初始化的BasicDataSource(所以每次都是相同的数据源)。

我也在teardown方法中显式调用了我的Sql对象,尽管Groovy在其文档中说你在使用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,那么它将在第二次测试后无限期地挂起。

任何人都可以建议为什么不返回连接?

谢谢。

I am running some unit-tests, but I am seeing some strange behaviour with regards connections not being released to the pool after use (which inevitably results in the unit tests hanging when the number of tests reach the pool size)

To demonstrate, I have created a really simple unit-test:

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

In my setup method, my getDataSource() method just returns a static, initialised BasicDataSource (so this is the same datasource every time).

I am also explicitly calling close on my Sql object in the teardown method, even though Groovy says in its docs that you dont have to when constructing your Sql object with a DataSource

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.

However, when I run the tests the number of active connections continues to increase, and if i set the max pool size to be 2, then it will hang after the second test indefinitely.

Can anyone advise why the connections are not being returned?

Thanks.

最满意答案

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

我没有注意到测试类正在扩展另一个具有重复的@Before安装方法的类,所以显然在池中做了一些有趣的事情。 删除它解决了这个问题。

Ok, it was a stupid problem..

I hadn't noticed that the testclass was extending another class that had duplicate @Before setup method in it, so was obviously doing funny things with the pool. Removing that has solved the problem.

更多推荐

本文发布于:2023-04-29 04:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQL   Groovy   closed   Connections

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!