java ResultSet 何时关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4984013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:56:59  来源:igfitidea点击:

When is ResultSet closed?

javajdbc

提问by Ahmed

I want to know if ResultSet can be closed if I didn't close it ? I have a ResultSet is closed exception but I am sure I didn't close the ResultSet anywhere . What I do exactly is that I use the ResultSet to perform a SELECT query then I use the same ResultSet because it's called by this method :

我想知道如果我没有关闭 ResultSet 是否可以关闭它?我有一个 ResultSet is closed 异常,但我确定我没有在任何地方关闭 ResultSet 。我所做的就是使用 ResultSet 来执行 SELECT 查询,然后我使用相同的 ResultSet 因为它是由这个方法调用的:

public Object getValueAt( int row, int column )
        throws IllegalStateException {
    // ensure database connection is available
    if ( !dbConnection.isConnectedToDatabase() )
        throw new IllegalStateException( "Not Connected to Database" );

    // obtain a value at specified ResultSet row and column

    try {
        getResultSet().absolute( row + 1 );
        return getResultSet().getObject( column + 1 );
    } // end try
    catch ( SQLException sqlException ) {
        System.out.println("Exception from here dude");
        sqlException.printStackTrace();
    } // end catch

    return ""; // if problems, return empty string object
} // end method getValueAt

So , another question : Is there a way to ensure the ResultSet is opened ?

那么,另一个问题:有没有办法确保 ResultSet 被打开?

Third question : Maybe the problem because I never close ResultSets .

第三个问题:也许是因为我从不关闭 ResultSets 的问题。

What's the point of closing ResultSet ?

关闭 ResultSet 有什么意义?

Edit : That's how statement is being created inside the constructor of a class called DBConnection:

编辑:这就是在名为 DBConnection 的类的构造函数中创建语句的方式:

Class.forName(driver);
        // connect to database
        connection = DriverManager.getConnection(url, username, password);

        // create Statement to query database
        statement = connection.createStatement(
     ResultSet.TYPE_SCROLL_INSENSITIVE,
     ResultSet.CONCUR_READ_ONLY );

        //connection ok
        connectedToDatabase=true;

ResultSet is created later whenever I want to execute a statement.

ResultSet 是在以后每当我想执行语句时创建的。

回答by mdrg

Directly from the docs on ResultSet.close():

直接来自ResultSet.close() 上的文档

Releases this ResultSet object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

...

Note: A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.

立即释放此 ResultSet 对象的数据库和 JDBC 资源,而不是等待它自动关闭时发生。

...

注意:当 Statement 对象关闭、重新执行或用于从多个结果的序列中检索下一个结果时,ResultSet 对象会被生成它的 Statement 对象自动关闭。

So, if you closed the Statement that generated your ResultSet is closed, you get that exception.

因此,如果您关闭了生成 ResultSet 的 Statement 已关闭,则会出现该异常。

Another question's answer: you shouldn't read results from a ResultSet like that. Perform the select an read all the data you need from the ResultSet at once, close the connection and then later you can read the fetched data as much as you want. You really shouldn't have an external resource/class calling your getValueAtmethod, which you expect to still be connected to the database. Connection may be terminated for many other reasons, so that's not the way to go.

另一个问题的答案:您不应该像那样从 ResultSet 中读取结果。执行选择并立即从 ResultSet 中读取您需要的所有数据,关闭连接,然后您可以根据需要读取获取的数据。你真的不应该有一个外部资源/类调用你的getValueAt方法,你希望它仍然连接到数据库。连接可能因许多其他原因而终止,因此这不是可行的方法。

Third answer: answered above.

第三个答案:上面已经回答了。

Last answer: releasing resources explicitly, without waiting for it to be closed when Statement is.

最后一个答案:显式释放资源,在 Statement is 时不等待它关闭。

回答by Swaranga Sarma

In case you have closed any of the following, your ResultSet will be closed automatically:

如果您关闭了以下任何一项,您的 ResultSet 将自动关闭:

  1. Statement object.
  2. Connection object.
  1. 声明对象。
  2. 连接对象。

I am strongly suspecting the connection is being closed. It is a natural tendency to close the database connection once the query is run. While it is a good practice, but may be you are closing the connection even before you have used the ResultSet object inside your TableModel class.

我强烈怀疑连接正在关闭。一旦运行查询,关闭数据库连接是很自然的趋势。虽然这是一个很好的做法,但您可能在 TableModel 类中使用 ResultSet 对象之前就关闭了连接。

回答by Sebastian ?askawiec

I always close Connections, ResultSets and Statements in finally {} block. In such case I don't have this problem, since this block is always executed (Well, not always, but here it fits). Please refer to this post, I placed skeleton implementation that might be interesting for you.

我总是在 finally {} 块中关闭连接、结果集和语句。在这种情况下,我没有这个问题,因为这个块总是被执行(嗯,不总是,但它适合)。请参考这篇文章,我放置了您可能感兴趣的框架实现。