ResultSet 何时关闭? [英] When is ResultSet closed?

查看:68
本文介绍了ResultSet 何时关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :

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

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

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

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

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

关闭 ResultSet 有什么意义?

What's the point of closing ResultSet ?

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

Edit : That's how statement is being created inside the constructor of a class called 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 是在我想执行语句后创建的.

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

推荐答案

直接来自 有关 ResultSet.close() 的文档:

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

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

...

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

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 的 Statement 已关闭,则会出现该异常.

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

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

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 getValueAt method, 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.

第三个答案:以上已回答.

Third answer: answered above.

最后一个答案:显式释放资源,无需等到 Statement is 关闭.

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

这篇关于ResultSet 何时关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆