以我的经验,提醒您使用连接池时要注意两点:1.任何情况下要记得关闭连接,最好的办法是放在finally{}内,以保证万无一失 -- 即使中间有意外或return也会执行。如下所示:
Connection connection=null;
try{
try{
connection = getConnectionByJndi(...);
}catch(Exception e){
return ;
} }catch(Exception e){}
finally{
//***在这里关闭连接***///
}
2.如果不是特别需要,请把连接池的参数"Statement Cache Size"设为0. weblogic8默认值是10. 如果不为0,有些未打补丁的weblogic会造成内存泄露。
{
try {
if (this.objStmt != null) {
this.objStmt.close();
this.objStmt = null;
}
if (this.objCon != null) {
this.objCon.close();
this.objCon = null;
}
} catch (SQLException e) {
} finally {
try{
if (this.objStmt != null) {
this.objStmt.close();
this.objStmt = null;
}
if (this.objCon != null) {
this.objCon.close();
this.objCon = null;
}
} catch (SQLException e) {}
} }
1.首先指出你控制上的错误,如下:
public void close() {
try {
if (this.objStmt != null) {
this.objStmt.close(); //***1.假设这里出错,会跑到***2***
this.objStmt = null;
}
if (this.objCon != null) {
this.objCon.close();
this.objCon = null;
}
}
catch (SQLException e) {
}
finally {
try {
//***2***////////////////
if (this.objStmt != null) {
this.objStmt.close(); //***3.假设这里又出错,会跑到***4***
this.objStmt = null;
}
if (this.objCon != null) {
this.objCon.close();
this.objCon = null;
}
}
catch (SQLException e) {
//***4***. 跑到这里时,connection.close()可能从未执行。(另SQLException最好改成Exception)
}
} }
2.你把connection当成某类的成员,所以有this.objCon。 这是没有必要的,因为你用了weblogic提供连接池你不必自己维护连接,所以需要时随时定义一个Connection objCon, 用完就关闭,可以参考我给你的例子。还有objStmt也一样,需要时createStatement(),不必成为某类的成员。
try
{
if (conn != null)conn.close();
}
catch (Exception ex)
{
//log.error("释放连接时出错!",ex);
}