下面是一个servlet 里的 doPOST 方法的代码我tomcat 连接池 maxActive =8  结果执行8次后 就无法得到 Connection了(为null)而且getConnection变得很慢(等于maxWait设置的时间)如果我 maxActive 设置为4  那么就四次以后不行  我每次都很小心的关闭 为什么关闭的Connection无法再利用??
==================================================
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String keyword = request.getParameter("keyword");
boolean hasData = false;

Context ctx=null;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;

try{
ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/sample");
if(ds!=null){
conn=ds.getConnection();//问题所在!
}
if(conn != null){
HttpSession session = request.getSession();
session.setAttribute("sou4sou_keyword",keyword); stmt = conn.createStatement() ;
rs = stmt.executeQuery("select top 1 id from searchdata where keyword like '%"+keyword+"%'");
System.out.println("select top 1 id from searchdata where keyword like '%"+keyword+"%'");
if(rs.next()){
hasData = true;
}
}
rs.close();
    rs = null;
    stmt.close();
    stmt = null;
    conn.close(); // Return to connection pool
    conn = null; 
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if (rs != null) {
try { rs.close(); } catch (SQLException e) { ; }
rs = null;
}
if (stmt != null) {
try { stmt.close(); } catch (SQLException e) { ; }
stmt = null;
}
if (conn != null) {
try { conn.close(); } catch (SQLException e) { ; }
conn = null;
} }catch(Exception e){

}

}
ServletContext sc = getServletContext();
RequestDispatcher rd = null; 
if(hasData){
rd = sc.getRequestDispatcher("/list.jsp");
rd.forward(request,response);
}else{
rd = sc.getRequestDispatcher("/souproxy.jsp");
rd.forward(request,response);
}
}     
==================================================

解决方案 »

  1.   

    每次close之后 不要在设置为null了
      

  2.   

    每次close之后 不要在设置为null了
    ++++++++++++++++++++++++++++++++++
    有关系么?操作的对象又不是的引用都交掉了吧
      

  3.   

    就拿conn来说,如果光是close的话,那close之后conn是什么??  是null吗?
      

  4.   

    tomcat 连接池的一个超级bug????????????????首先你要认识到,数据库连接池这么好的一个东西,在推出来之前一定是做过很多测试的。这么关键的地方出现bug的几率几乎为零
      

  5.   

    请教一下,确实不知道conn.close()后conn是什么,看过人家的代码,在关闭链接的时候都是:
    if(null!=conn)
       conn.close();关于conn.close()所做的工作,不知道,感觉跟将conn=null后,getConnection()并不是立即为空,请楼上的解释下,说说清楚,谢谢了。
      

  6.   

    哈哈,这么大的BUG都被你找到了,TOMCAT还不完啦!
      

  7.   

    你用的tomcat是哪个版本?放个最新版试试吧.
      

  8.   

    没碰到过这样的问题。关注。
    楼主在最后ctx.close();试试看会不会在出现这个问题。
    还有的话应该是数据库驱动的问题了。楼主的jdbc是什么呢?
      

  9.   

    你把 if(conn != null)改为if(!conn.isClose())试试吧,你的代码没什么问题的,我想可能是你的工程中,有别的程序打一了连接没关导致。如果TOMCAT的连接池有这样明显的BUG,他早就修改了。
      

  10.   

    这个不是BUG,我遇到过这个问题。建立连接时不要什么条件都不加就创建一个连接,关闭一定要小心,包括过程中关闭RS,STMT等对象时,看是否影响到连接的关闭等。可以解决!
      

  11.   

    我们用java来解决实际问题。java技术交流,讨论java的技术细节和最新技术。欢迎中高级程序员以及渴望学习java技术的初学者加入讨论。QQ群:3001581