2012-11-28 68 views
0

我正在運行一些單元測試,但我看到一些奇怪的行爲,關於連接在使用後沒有被釋放到池中(這不可避免地導致單元測試在測試次數達到時掛起池大小)Groovy SQL連接沒有被關閉

爲了證明,我創建了一個非常簡單的單元測試:

@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(所以這是每次相同的數據源)。

我也明確地調用close在拆卸方法,我的SQL對象上,即使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,那麼將在第二次測試後無限期地掛起。

任何人都可以建議爲什麼連接不被返回?

謝謝。

回答

0

好吧,這是一個愚蠢的問題..

我沒有注意到的TestClass正在伸出了另一類有@Before重複設置方法,所以顯然是做有趣的事情與池。刪除已解決問題。