读书人

tomcat有关问题.每隔一段时间就需要重

发布时间: 2013-01-07 10:02:24 作者: rapoo

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:


*
* 数据源连接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:

/*
* 网站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("   ");


}
}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;
}
}



远程空间的日志报错:
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不能连接到数据源
搞了很长时间,头都大了.
谢谢!
[解决办法]
conn = null能正常关闭连接吗?没这么用过

读书人网 >应用服务器

热点推荐