Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PSQLException: this ResultSet is closed

i've occours this strange error for the first time in my life, and i don't know what does it means. I've a class that retrieve information from a table on a postgresql database, do some operations and return an arraylist with parsed element:

ResultSet rs = ProduttoreDCS.getProduttori();
        System.out.println("Recuperato result set produttori");
        ArrayList<String[]> res = new ArrayList<String[]>();


        while (rs.next()) {
            String[] current = new String[6];

            current[0] = Integer.toString(rs.getInt("partita_iva"));
            current[1] = rs.getString("nome");
            current[2] = rs.getString("cognome");
            current[3] = rs.getString("via_sede");
            current[4] = rs.getString("citta_sede");
            current[5] = rs.getString("provincia_sede");

            res.add(current);
            current = null;
        }

        return res;

the error is on "while" line.

public static ResultSet getProduttori() throws ClassNotFoundException, SQLException {
    /*
     * retrieve all record from produttori table
     */
    Connection conn = null;
    ResultSet res = null;
    Statement stmt = null;
    String query = "SELECT * FROM produttore";

    conn = ConnectionManager.getConnection();
    System.out.println("Connection obtained by ProduttoreDCS class");
    stmt = conn.createStatement();
    res = stmt.executeQuery(query);

    stmt.close();
    conn.close();


    return res;   
}
like image 794
giozh Avatar asked Jun 28 '13 15:06

giozh


People also ask

What is ResultSet closed?

A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results. The number, types and properties of a ResultSet object's columns are provided by the ResultSetMetaData object returned by the ResultSet.

What happens if ResultSet is not closed?

In the mean time many result sets might opened and left unclosed. If it happens on a single database by multiple applications, the data updation is not perfect and may violate ACID properties rule for data presuming.

Do we need to close the ResultSet?

You should explicitly close Statements , ResultSets , and Connections when you no longer need them, unless you declare them in a try -with-resources statement (available in JDK 7 and after).

Can we use ResultSet after closing connection?

Once the connection is closed you can no longer use any of the resources (statements, prepared statements, result sets), all of them are automatically closed. So you need to do all of your processing while the resources are open.


1 Answers

I've encountered the same problem by running the wrong combination of:

Postgres version; Hibernate dialect; postgres jdbc driver;

Updating all to the latest version solved the problem for me.

P.S. I know this is not what OP was asking, but this is the first result in Google when you search for the problem.

like image 191
gyosifov Avatar answered Oct 25 '22 12:10

gyosifov