为什么连接数还是不断增加?

zhusuhao 2004-01-14 06:07:46
我的数据库连接池中已经有了:
public void destory() throws Exception{
try {
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
}catch(Exception e){
System.err.println("Close connection error:"+e);
}
}
在程序中调用了,为什么连接数还是在增加?
...全文
57 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
baisun 2004-01-19
  • 打赏
  • 举报
回复
我上次不是给你讲过了吗?呵呵
lxx8402 2004-01-15
  • 打赏
  • 举报
回复
那个异常不必调用,因为finally不管是否产生异常都会执行
redapple111 2004-01-15
  • 打赏
  • 举报
回复
后面那个异常没有
redapple111 2004-01-15
  • 打赏
  • 举报
回复
public void destory() throws Exception{
try {
if(rs != null)
rs.close();
}catch(Exception e){

}

if(stmt != null)
{
stmt.close();
}catch(Exception e){

}

if(conn != null)
{
conn.close();
}catch(Exception e){

}

rs=null;
stmt=null;
conn=null;

}catch(Exception e){
System.err.println("Close connection error:"+e);
}
}

lkpei 2004-01-15
  • 打赏
  • 举报
回复
异常的时候也要调用
lxx8402 2004-01-15
  • 打赏
  • 举报
回复
kadina(次帅)说的对阿。
1.写在try中,如果if(rs != null) rs.close(); 触发异常,程序会直接跳转catch段,try后面的代码根本不会执行。
2.有关destory()的调用,放在末尾,如果前面的jsp代码就出错,destory()也得不到执行。这些都不能保证连接被有效的关闭。

建议:对于这种应该把读写数据库于html页面分开,或着同一个页面中把结果集付给List,HashMap等等,这样,在读写数据时就可以比较安全实现连接的关闭。如:
try{
conn = ConnectionManager.getConnection();
...
while(rs.next()){
map = ....
}
rs.close();
}
catch(SQLException sqle){}
finally{
try {stmt.close();}
catch (Exception e) { e.printStackTrace(); }
try {conn.close();}
catch (Exception e) { e.printStackTrace(); }
}
ddadoris 2004-01-15
  • 打赏
  • 举报
回复
一定有地方未能关闭,在任何一个跟数据库打交道的方法中,一般不要返回ResultSet,这样会导致rs无法关闭,而且最好要有finally,把rs,stm,con的close都写入
kadina 2004-01-15
  • 打赏
  • 举报
回复
必须保证无论处理是否正常、是否出现Exception都要被调用才行。写到finally里面。
zhusuhao 2004-01-15
  • 打赏
  • 举报
回复
在每一页的末尾调用
zhusuhao 2004-01-15
  • 打赏
  • 举报
回复
我用了,现在是这样的:
public void destory() throws Exception{
try {
if(rs != null)
{
rs.close();
}
}catch(Exception e){
System.err.println("Close connection error:"+e);
}
try {
if (stmt != null) {
stmt.close();
}
}catch(Exception e){
System.err.println("Close connection error:"+e);
}
try {
if(conn != null)
{
conn.close();
}
}catch(Exception e){
System.err.println("Close connection error:"+e);
}
finally{
try {conn.close();}
catch (Exception e) { e.printStackTrace(); }
}

}

原先关闭rs出错的地方还是会抛出错误,但是还会继续下面的conn.close的执行。
dropship 2004-01-14
  • 打赏
  • 举报
回复
那我问你,你的destroy()方法什么时候调用?
是不是有异常的时候调用,还是每一个用户离开后都调用?
这点很重要
sgdb 2004-01-14
  • 打赏
  • 举报
回复
public void destory() throws Exception{
try {
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
rs=null;
stmt=null;
conn=null;
System.gc();
}catch(Exception e){
System.err.println("Close connection error:"+e);
}
}
zhusuhao 2004-01-14
  • 打赏
  • 举报
回复
当然了,我发现还是有一定用的,因为有些页面运行后连接数并不增加!

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧