![](/img/trans.png)
[英]After getting the connection from Glassfish Connection Pool and use it, should I close it right after?
[英]On GlassFish got max pool size exception because of cannot close connection created from DataSource
從DataSource打開連接時,雖然手動打開和關閉功能,但在GlassFish上出現了最大池大小異常。 我到處都是谷歌,但沒有答案! 有人可以幫我嗎?
我的代碼段如下:(我設置初始連接池:8,最大池大小:32,空閑超時:300s,最大等待時間:60s)在調用work()函數時得到了異常最大池大小
public class BP{
@Resource(name="BP", type = javax.sql.DataSource.class)
private DataSource ds;
public void work(){
for(int i = 0; i < 50; i++){
doSomething();
}
}
public void doSomething(){
java.sql.Connection conn = null;
CallableStatement cs = null;
ResultSet rs = null;
try{
conn = ds.getConnection();
cs = conn.prepareCall("{ call get_bank(?,?) }");
cs.setString(1, "test");
cs.registerOutParameter(2, OracleTypes.CURSOR);
cs.execute();
rs = (ResultSet) cs.getObject(2);/
// do something
//...
} catch (Exception e) {
e.printStackTrace();
}finally{
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (cs != null) {
try {
cs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
輸出:
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: In-use connections equal max-pool-size and expired max-wait-time. Cannot allocate more connections.
您最終聲明的最后一塊可能是您的問題。
您正在connection
而不是conn
上執行.close()
。 表示您引用了錯誤的對象。
if (conn != null) {
try {
connection.close(); <---- should be conn.close()
} catch (Exception e) {
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.