【问题标题】:SQLServerException The result set is closedSQLServerException 结果集已关闭
【发布时间】:2015-02-17 13:11:51
【问题描述】:

我收到 'SQLServerException : 0 结果集已关闭。结果集关闭。@java.lang.Thread:run:722' 在下面的代码中。

我可以看到我没有关闭语句或结果集,那么为什么我会收到此异常。

有人可以帮忙吗??提前致谢

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

【问题讨论】:

  • 看起来您并没有在任何地方打开连接。是 connection.Open 被调用了吗?
  • @gh9 是的,这是我正在做的检查的一部分。所以我正在打开连接并做一些事情。当我遇到一些错误/异常时,我会运行此方法来检查连接是否有效。
  • 你调用的代码在哪里.isConnectionValid(connection);
  • @user1354678 我更新了描述。希望对您有所帮助...
  • 提供完整的堆栈跟踪,以便我们知道在哪一行和哪个组件上抛出了这个异常。您的帖子目前令人困惑。在旁注中,您最好让连接池处理验证。 Commons DBCP 让您使用validationQuery 参数validationQuery="SELECT version();" 来执行此操作。

标签: java sql database


【解决方案1】:

finally 块被调用before the return is evaluated,当你返回你的值时,结果集已经关闭了。

尝试以这种方式更改您的代码:

boolean output = resultSet.next();
return output;

请参考this question

编辑:

它总是正确的。我创建了一个测试来证明我的观点:

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

在getStringList()方法中,我在finally块中调用clear(),当我尝试获取大小时,如预期的那样得到0。

另一方面,在 getStringListValue() 中,我也调用了 clear,但是我按照您的建议返回 List 中第二个元素的值,并且可以打印它的值。

这里发生的是在返回行中创建了一个指向 String 对象的指针,并接收到数组中第二个元素的值。因此,finally 块中的 clear() 清除了 List,但返回的 String 保留了复制的值。

【讨论】:

  • 我猜这并不总是正确的。我做了一个简单的实验,我做了一个 ArrayList (AL) 并在其中添加了 3 个值。在 try 块的 return 语句中,我尝试在第 n 个索引处获取值,即 'AL.get(2),在 finally 块中,我清除列表,即 AL.clear()。我可以看到我得到了重新运行的值。在调试时,我看到指针在 try 块中返回,然后在 finally 块中返回,然后返回并返回我所期望的值。
  • 我同意你的例子,我也希望如此。但是,您与关于结果集的原始答案相矛盾,因为 finally 块已执行。在原始问题中,resutset.next() 值应存储为返回值存储在您的示例中。如果我错了,请纠正我。
猜你喜欢
  • 2019-06-21
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 1970-01-01
相关资源
最近更新 更多