当前位置: 代码迷 >> 应用服务器 >> tomcat有关问题.每隔一段时间就需要重启一下远程空间的TOCMAT
  详细解决方案

tomcat有关问题.每隔一段时间就需要重启一下远程空间的TOCMAT

热度:9805   发布时间:2013-02-26 00:00:00.0
tomcat问题.每隔一段时间就需要重启一下远程空间的TOCMAT?
日志报错:set.java.sql.SQLException: Illegal operation on empty result set.org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object不能连接到数据源
怀疑有连接没有关闭.请帮忙看看代码.谢谢
DBConnSource.java:
Java code
* * 数据源连接BEAN  */package mybean;import java.sql.*;import javax.sql.*;import javax.naming.*;public class DBConnSource {    private Connection conn;    private Statement stmt;    private PreparedStatement pstmt;    public DBConnSource(String dsName){        try{            Context initCtx = new InitialContext();            Context ctx =(Context)initCtx.lookup("java:comp/env");            DataSource ds =(DataSource)ctx.lookup(dsName);            conn = ds.getConnection();        }        catch(Exception e)        {            System.out.print(e.toString());        }    }    public synchronized Statement getStmt()throws Exception    {        stmt=conn.createStatement();        return stmt;    }    public synchronized PreparedStatement getPstmt(String sql)throws Exception    {        pstmt=conn.prepareStatement(sql);        return pstmt;    }    public void DBclose(){        try{             //关闭 Connection conn;            if(conn!=null){            conn.close();            }        }catch(Exception e){            System.out.print(e.toString());        }finally{            conn=null;        }                try{               //关闭Statement stmt;            if(stmt!=null){            stmt.close();            }        }catch(Exception e){            System.out.print(e.toString());        }finally{            stmt=null;        }                try{               //关闭 PreparedStatement pstmt;            if(pstmt!=null){            pstmt.close();            }        }catch(Exception e){            System.out.print(e.toString());        }finally{            pstmt=null;        }    }}




stylelist.java:
Java code
/* * 网站JAVABEAN * 所有帖子分类列表. * 使用数据源连接. * 前后台共用JAVABEAN. */package mybean;import java.sql.*;import mybean.DBConnSource;public class StyleList {    private String tableName;    private Statement stmt;    private ResultSet rs;    private DBConnSource dbc;         public StyleList(){}        public void setTableName(String n){        this.tableName=n;    }        public StringBuffer getBuffer(){        StringBuffer buffer=new StringBuffer();        dbc=new DBConnSource("jdbc/myweb");           try{                        stmt=dbc.getStmt();             }catch(Exception e){                        System.out.print("不能连接到数据源");            }                      try{             String strSql="SELECT * FROM "+tableName;              rs = stmt.executeQuery(strSql);                          while(rs.next()){                                    buffer.append("<a href='style.jsp?style="+rs.getString("style")+"' target=_blank>"+rs.getString("style")+"</a>");                buffer.append("&nbsp;&nbsp;&nbsp;");                }                       }catch(SQLException e){            System.out.print(e.toString());        }finally{            try{               //关闭 ResultSet rs.                   if(rs!= null){                   rs.close();}                }catch(SQLException ex){                   System.out.print(ex.toString());                }finally{                   rs=null;                }                                             try{               // 关闭 Statement stmt.                     if(stmt!= null){                    stmt.close();}                 }catch(SQLException ex){                    System.out.print(ex.toString());                 }finally{                    stmt=null;                 }                     dbc.DBclose();  //关闭 DBConnsource dbc;                }         return buffer;    }}
  相关解决方案