我正在接受" java.sql.SQLException:ResultSet关闭后不允许操作"例外。我用谷歌搜索它,它说当我们有几个属于同一个Statement的ResultSet时它会导致它。但在我的代码中,我为每个ResultSet使用不同的Statement,但我仍然看到这个问题。这是我的代码。 Frist Code 。
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
pstmt = getPreparedStatement(sql);
rs = (ResultSet) pstmt.executeQuery();
while (rs.next()) {
PreparedStatement pstmt1 = null;
ResultSet rs1 = null;
if (condition) {
} else {
if (condition) {
int index = 0;
String newSql = "query";
pstmt1 = getPreparedStatement(newSql);
rs1 = (ResultSet) pstmt1.executeQuery();
if (rs1.next()) {
}
}
}
closeAll(pstmt1, rs1);
}
} finally {
closeAll(pstmt, rs);
closeConnection();
}
这是我的基本连接类代码
Connection conn = null;
boolean isfromPool = false;
Vector connectionavailable = new Vector();
/**
*
* @return
* @throws java.sql.SQLException
*/
private Connection getConnection() throws SQLException {
try {
Context initContext = new InitialContext();
//DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/TestDB");
Context envContext = (Context) initContext.lookup("java:comp/env");
if (envContext == null) {
ApplicationLogger.log("envContext is null. unable to get connection from pool");
throw new Exception("envContext is null. unable to get connection from pool");
}
DataSource ds = (DataSource) envContext.lookup("jdbc/CI");
if (ds == null) {
ApplicationLogger.log("DataSource is null. unable to get connection from pool");
throw new Exception("DataSource is null. unable to get connection from pool");
}
conn = ds.getConnection();
if (conn == null) {
ApplicationLogger.log("connection object is null that taken "
+ "from connection pool");
} else {
if (conn.isClosed()) {
ApplicationLogger.log(Level.INFO, "Connection object is closed that taken from pool");
throw new Exception("Connection object is closed that taken from pool");
}
isfromPool = true;
return conn;
}
} catch (Exception e) {
ApplicationLogger.log(Level.SEVERE, "unable to get the connection", e);
throw e;
} finally {
return conn;
}
}
/**
*
* @return
* @throws java.sql.SQLException
*/
private Statement getStatement() throws SQLException {
return getConnection().createStatement();
}
public PreparedStatement getPreparedStatement(String sql) throws SQLException {
ApplicationLogger.log(Level.ALL, "SQl is : " + sql);
// return (PreparedStatement) getConnection().prepareStatement(sql);
return (PreparedStatement) getConnection().prepareStatement(sql);
}
public void closeAll(PreparedStatement pstmt, ResultSet rs) throws SQLException {
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (conn != null) {
closeConnection();
}
}
你能告诉我为什么它在这段代码中工作。 第二个代码
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
pstmt = getPreparedStatement(sql);
rs = (ResultSet) pstmt.executeQuery();
while (rs.next()) {
PreparedStatement pstmt1 = null;
ResultSet rs1 = null;
if (onlineTestid != 0) {
int index = 0;
String newSql = "query";
pstmt1 = getPreparedStatement(newSql);
rs1 = (ResultSet) pstmt1.executeQuery();
if (rs1.next()) {
}
}
closeAll(pstmt1, rs1);
}
} finally {
closeAll(pstmt, rs);
closeConnection();
}
这是closeConnection()方法
public void closeConnection() throws SQLException {
if (conn != null) {
conn.close();
conn = null;
}
}
提前致谢。
答案 0 :(得分:0)
编辑我最初认为这些陈述是在共享连接的时候,事实上,它们似乎并不存在。我已更新,以反映我的新理解。
您正在使用closeAll()
方法关闭连接。
while (rs.next()) {
PreparedStatement pstmt1 = null;
ResultSet rs1 = null;
if (condition) {
} else {
if (condition) {
int index = 0;
String newSql = "query";
pstmt1 = getPreparedStatement(newSql);
rs1 = (ResultSet) pstmt1.executeQuery();
if (rs1.next()) {
}
}
}
closeAll(pstmt1, rs1);
}
在while
循环的倒数第二行,您致电closeAll(pstmt1, rs1)
。在closeAll()
方法中,语句closeConnection()
关闭(我假设我还没有看到代码)任何打开的连接,包括{{1}使用的连接} ResultSet
引用。
答案 1 :(得分:0)
您正在关闭连接,之后您正在尝试结果集
while (rs.next()) {
PreparedStatement pstmt1 = null;
ResultSet rs1 = null;
if (condition) {
} else {
if (condition) {
int index = 0;
String newSql = "query";
pstmt1 = getPreparedStatement(newSql);
rs1 = (ResultSet) pstmt1.executeQuery();
if (rs1.next()) {
}
}
}
closeAll(pstmt1, rs1);// connnection is closed
}
让我们假设您有3条记录。
所以rs.next()
将返回true并且控制进入循环while (rs.next()) {
,现在最后你关闭连接,现在再次控制转到while (rs.next()) {
但是因为连接已经关闭所以你将在这一行中得到错误。试着调试你会知道错误