当从DataSource打开连接时,我在GlassFish上获得了最大池大小异常,尽管手动耦合了打开和关闭功能。我四处搜索,但没有答案! 有谁可以帮助我?
我的代码片段如下:(我的设置初始连接池:8,max-pool-size: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.
答案 0 :(得分:0)
您的finally语句的最后一个块可能是您的问题。
您正在.close()
上connection
而不是conn
。这意味着你引用了错误的对象。
if (conn != null) {
try {
connection.close(); <---- should be conn.close()
} catch (Exception e) {
e.printStackTrace();
}
}