怀疑有连接没有关闭.请帮忙看看代码.谢谢
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不能连接到数据源
搞了很长时间,头都大了.
谢谢!
2 个解决方案
#1
conn = null能正常关闭连接吗?没这么用过
#2
谢谢.你说的很对.那个conn=null是多余的.
#1
conn = null能正常关闭连接吗?没这么用过
#2
谢谢.你说的很对.那个conn=null是多余的.