连接池泄漏问题,高分求,在线等

gonxi1001 2005-09-15 02:51:24
错误提示:

<2005-9-15 下午14时36分02秒 CST> <Warning> <JDBC> <BEA-001074> <A JDBC pool connection leak was detected. A connection leak occurs when a connection obtained from the pool was not closed explicitly by calling close() and then was disposed by the arbage collector and returned to the connection pool.
The following stack trace at create shows where the leaked connection was created. [Null exception
passed, creating stack trace for offending caller]
at weblogic.utils.StackTraceUtils.throwable2StackTrace(StackTraceUtils.java:28)
at weblogic.jdbc.wrapper.PoolConnection.finalize(PoolConnection.java:77)
at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:83)
at java.lang.ref.Finalizer.access$100(Finalizer.java:14)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160)
>
...全文
320 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ywb1973 2005-09-16
  • 打赏
  • 举报
回复
把close放在该类的finalize()函数中试试,理论上该函数会在垃圾回收器回收连接池前执行。
didu811 2005-09-15
  • 打赏
  • 举报
回复
第一步:
在try外面
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
第二步:
在finally中关闭连接
finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
throw new Exception("数据库关闭时出错 !!! "+e.getMessage());
}
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
throw new Exception("数据库关闭时出错 !!! "+e.getMessage()); }
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
throw new Exception("数据库关闭时出错 !!! "+e.getMessage()); }
}
dachun 2005-09-15
  • 打赏
  • 举报
回复
当然得在取完数据后才到finally
gonxi1001 2005-09-15
  • 打赏
  • 举报
回复
如果rs关闭了就取不到数据了
gonxi1001 2005-09-15
  • 打赏
  • 举报
回复
To: flyxxxxx(灭神)
我类似于getComplexPage的函数有好几个,不可能在同一个函数开始的时候打开连接又在该函数的结尾关闭连接呀。
gonxi1001 2005-09-15
  • 打赏
  • 举报
回复
没什么变化,还是提示连接池泄漏
flyxxxxx 2005-09-15
  • 打赏
  • 举报
回复
Connection conn=null;
try{
conn=...;
}catch(Exception e){}//这里的catch如果不需要可以没有
finally{
if(conn!=null){
try{
conn.close();
}catch(Exception){}
}
}
dachun 2005-09-15
  • 打赏
  • 举报
回复
rs.close();
stm1.close();
conn.close();

应该写在finally中
dachun 2005-09-15
  • 打赏
  • 举报
回复
在try外面
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
gonxi1001 2005-09-15
  • 打赏
  • 举报
回复
需要放在catch中才行,而且放在finally中如何初始化?
gonxi1001 2005-09-15
  • 打赏
  • 举报
回复
to dachun(达达):
怎么放在finally中?
dachun 2005-09-15
  • 打赏
  • 举报
回复
rs.close();
stm1.close();

应该写在finally中
gonxi1001 2005-09-15
  • 打赏
  • 举报
回复
问题:
1、如果不用static的方法该如何写?
2、endup()中已经有关闭连接的操作,但跟踪发现,在关闭连接前系统已经报错了。
gonxi1001 2005-09-15
  • 打赏
  • 举报
回复
调用的jsp:
<%
DispNew.init();
try{
%>
...
<%
</font><font color="#333333"><font color="#333333"><font color="#333333"><font color="#333333">
<%
if(sPage.equals("5")){
String vt=DispNew.getComplexPage(5,10,pages,30,"index.jsp","images/p1/dot1.gif");
out.print(vt.trim());}
else{
String vt=DispNew.getComplexPage(5,10,"1and5",30,"index.jsp","images/p1/dot1.gif");
out.print(vt.trim());
}
%>
%>
...
<%
DispNew.endup();
}
catch(Exception e)
{
e.printStackTrace();
out.println("Error occur, please contact with administrator!");
}
%>
gonxi1001 2005-09-15
  • 打赏
  • 举报
回复
源码:

public static void init(){
try{
ctx = new InitialContext();
ds = (javax.sql.DataSource)ctx.lookup("WebDB");
cn = ds.getConnection();
}
catch(Exception e){
e.printStackTrace();
}
}

public static void endup(){
try{
if(rt!=null) rt.close();
if(stm!=null) stm.close();
if(cn!=null) cn.close();
}
catch(Exception e){
e.printStackTrace();
}
}

private static void pageinationQuery(int PageSize,int PageNo,int id){
String sql=new String();

try{
stm = cn.createStatement();
if(id!=0){
sql="select top "+PageSize*PageNo+" * from entry where title_id="+id+" order by entry_id desc";
}
else{
sql="select top "+PageSize*PageNo+" * from entry order by entry_id desc";
}
rt = stm.executeQuery(sql);
for(int i=PageSize;i<(PageSize*PageNo);i++){
rt.next();
}
}
catch(Exception e){
System.out.print("\n\n\n"+sql+"\n\n\n");
e.printStackTrace();
}
}

private static void getCount(int id,int pageSize){
ResultSet rs;
try{
Statement stm1 = cn.createStatement();
String sql;
if(id!=0){
sql="select count(*) from entry where title_id="+id;
}
else{
sql="select count(*) from entry";
}
rs = stm1.executeQuery(sql);
rs.next();
cnt=(rs.getInt(1)%pageSize==0)?(rs.getInt(1)/pageSize):(rs.getInt(1)/pageSize+1);
rs.close();
stm1.close();
}
catch(Exception e){
e.printStackTrace();
}
}

public static String getComplexPage(int tab,int pageSize,String strPageNo,int iDispWidth,String jspFile,String imgFile){
String strDisp,strHint,strTmp,strDate;
int pageNo=Integer.parseInt(strPageNo.substring(0,strPageNo.indexOf("and")));
String sPageNo=strPageNo.substring(strPageNo.indexOf("and")+3,strPageNo.length());
pageinationQuery(pageSize,pageNo,tab);
strDisp="<table border='0' cellspacing='0' cellpadding='0'>";
getCount(tab,pageSize);
try{
while(rt.next()){
strHint=rt.getString("content").trim();
if(strHint.length()>(iDispWidth+3)){
strTmp=strHint.substring(0,iDispWidth);
strTmp=strTmp+"...";
}
else{
strTmp=strHint;
}
strDate=rt.getString("iss_date").substring(5,10);
strDisp=strDisp+"<tr class='Color2'><td width='30'> <div align='center'><img src="+imgFile+" width='8' height='8'></div></td>";
strDisp=strDisp+"<td width='487' height='20' bordercolor='#FFFFFF'><font size='2' color='#666666'><A href=\""+rt.getString("doc_path").trim()+"\" target=\"_blank\">";
strDisp=strDisp+strTmp+"("+strDate+")";
strDisp=strDisp+"</A></font>";
if(strDate.equals(strToday)){
strDisp=strDisp+"<img src ='images/p2/new.gif'></td></tr>";
}
else{
strDisp=strDisp+"</td></tr>";
}
}
int totalPages=cnt;
if(pageNo==1 && pageNo <totalPages){
strDisp=strDisp+"<tr><td></td><td><font size='2' color='#666666'>第"+Integer.toString(pageNo)+"页,共"+Integer.toString(totalPages)+"页 首页 上一页 </font><A href=\""+jspFile+"?page="+Integer.toString(pageNo+1)+"and"+sPageNo+"\">下一页</A>";
strDisp=strDisp+" <A href=\""+jspFile+"?page="+Integer.toString(totalPages)+"and"+sPageNo+"\">尾页</A></td></tr>";
}
else if(pageNo==totalPages && pageNo!=1){
strDisp=strDisp+"<tr><td></td><td><font size='2' color='#666666'>第"+Integer.toString(pageNo)+"页,共"+Integer.toString(totalPages)+"页 </font><A href=\""+jspFile+"?page=1"+"and"+sPageNo+"\">首页</A>";
strDisp=strDisp+" <A href=\""+jspFile+"?page="+Integer.toString(pageNo-1)+"and"+sPageNo+"\">上一页</A><font size='2' color='#666666'> 下一页 尾页</font></td></tr>";
}
else if(pageNo>1 && pageNo<totalPages){
strDisp=strDisp+"<tr><td></td><td><font size='2' color='#666666'>第"+Integer.toString(pageNo)+"页,共"+Integer.toString(totalPages)+"页 </font><A href=\""+jspFile+"?page=1"+"and"+sPageNo+"\">首页</A>";
strDisp=strDisp+" <A href=\""+jspFile+"?page="+Integer.toString(pageNo-1)+"and"+sPageNo+"\">上一页</A>";
strDisp=strDisp+" <A href=\""+jspFile+"?page="+Integer.toString(pageNo+1)+"and"+sPageNo+"\">下一页</A>";
strDisp=strDisp+" <A href=\""+jspFile+"?page="+Integer.toString(totalPages)+"and"+sPageNo+"\">尾页</A></td></tr>";
}
else{
strDisp=strDisp+"<tr><td></td><td><font size='2' color='#666666'>第"+Integer.toString(pageNo)+"页,共"+Integer.toString(totalPages)+"页 首页 上一页 下一页 尾页</font></td></tr>";
}
}
catch(Exception e){
e.printStackTrace();
}
strDisp=strDisp+"</table>";
return strDisp;
}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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