Java SQLServerException结果集已关闭
我正在获取“SQLServerException:0”结果集已关闭。结果集已关闭。@java.lang.Thread:run:722'在以下代码中 我可以看到我没有关闭语句或结果集,那么为什么我会得到这个异常 有人能帮忙吗??提前谢谢Java SQLServerException结果集已关闭,java,sql,database,Java,Sql,Database,我正在获取“SQLServerException:0”结果集已关闭。结果集已关闭。@java.lang.Thread:run:722'在以下代码中 我可以看到我没有关闭语句或结果集,那么为什么我会得到这个异常 有人能帮忙吗??提前谢谢 private boolean isConnectionValid(Connection connection){ //SQL statement to execute the query. Statement statement = null;
private boolean isConnectionValid(Connection connection){
//SQL statement to execute the query.
Statement statement = null;
//resultSet receives the result of statement execution.
ResultSet resultSet = null;
//detect the connectivity.
try{
//create a statement.
statement = connection.createStatement();
//define the specific query after the statement is created.
String query = databaseType == DatabaseType.ORACLE? "select 1 from dual" : "select 1";
//apply the statement to execute the query.
resultSet = statement.executeQuery(query);
// if the resultSet.next() returns true, the connection is fine. Otherwise the connection is invalid.
return resultSet.next();
}catch(SQLException e){
//If any SQL Exception is caught, the connection is invalid as well.
Common.logException2(getLogger(), e, null);
return false;
}finally{
//finally close statement and resultSet to prevent cursor leak if any of them is not null.
Common.closeStatementAndResultSet(statement, resultSet, getLogger());
}
我使用isConnectionValid方法的一个示例如下:
public boolean execute(Logger logger) throws SQLException {
try {
if( !query.toUpperCase().startsWith("SELECT") ) {
queryLoggerInfo(database.getDbName() + " " + this);
}
return statement.execute();
} catch (SQLException e) {
if (database.isConnectionValid(connectionId)){
//log it
} else {
// other log methods
}
throw e;
}
}
正在调用finally块,当您返回值时,resultset已经关闭 尝试以下方式更改代码:
boolean output = resultSet.next();
return output;
请参阅
编辑:
这总是真的。我创建了一个测试来证明我的观点:
public class Testing {
public static void main(String[] args) {
Testing t = new Testing();
System.out.println("Test1: " + t.getStringListValue());
System.out.println("Test2: " + t.getStringList().size());
}
public String getStringListValue() {
List<String> stringList = new ArrayList<String>();
try {
stringList.add("a");
stringList.add("b");
stringList.add("c");
return stringList.get(2);
} catch (Exception e) {
} finally {
stringList.clear();
}
return null;
}
public List<String> getStringList() {
List<String> stringList = new ArrayList<String>();
try {
stringList.add("a");
stringList.add("b");
stringList.add("c");
return stringList;
} catch (Exception e) {
} finally {
stringList.clear();
}
return null;
}
}
公共类测试{
公共静态void main(字符串[]args){
测试t=新测试();
System.out.println(“Test1:+t.getStringListValue());
System.out.println(“Test2:+t.getStringList().size());
}
公共字符串getStringListValue(){
List stringList=新建ArrayList();
试一试{
stringList.添加(“a”);
字符串列表。添加(“b”);
添加(“c”);
返回stringList.get(2);
}捕获(例外e){
}最后{
stringList.clear();
}
返回null;
}
公共列表getStringList(){
List stringList=新建ArrayList();
试一试{
stringList.添加(“a”);
字符串列表。添加(“b”);
添加(“c”);
返回字符串列表;
}捕获(例外e){
}最后{
stringList.clear();
}
返回null;
}
}
在getStringList()方法中,我在finally块中调用clear(),当我尝试获取大小时,得到了预期的0
另一方面,在getStringListValue()中,我也调用clear,但是我按照您的建议返回列表中第二个元素的值,并且可以打印它的值
这里发生的情况是,在返回行中创建了一个指向字符串对象的指针,并接收到数组中第二个元素的值。因此,finally块中的clear()会清除列表,但返回字符串会保留复制的值。看起来您不会在任何地方打开连接。连接打开了吗?@gh9是的,这是我正在做的检查的一部分。所以我正在打开连接并做一些事情。当我遇到一些错误/异常时,我运行此方法来检查连接是否有效。您正在调用的代码在哪里
。isConnectionValid(连接)代码>@user1354678我更新了描述。希望它有帮助…提供完整的堆栈跟踪,以便我们知道在哪一行和哪一个组件抛出此异常。你的帖子现在令人困惑。另一方面,最好让连接池处理验证。Commons DBCP让我们使用validationQuery
参数validationQuery=“SELECT version();”
来实现这一点。我想这不一定是真的。我做了一个简单的实验,制作了一个ArrayList(AL)并在其中添加了3个值。在try块的return语句中,我尝试获取第n个索引的值,即'AL.get(2),在finally块中,我清除列表,即AL.clear()。我可以看到我得到了retrun值。在调试过程中,我看到指针在try块中指向return,然后在finally块中指向return,然后返回我所期望的值。我同意您的示例,我也期望得到相同的结果。但是,您反驳了关于resultset接近的原始答案,因为finally块被执行了。在原始问题中,resutset.next()值应该存储为示例中存储的返回值。如果我错了,请纠正我。