[CONJ-90] ResultSet not closed on new execution of Statement Created: 2014-04-08  Updated: 2014-09-10  Resolved: 2014-09-10

Status: Closed
Project: MariaDB Connector/J
Component/s: None
Affects Version/s: 1.1.7
Fix Version/s: 1.1.8

Type: Bug Priority: Major
Reporter: Erik Mattheis Assignee: Massimo Siani (Inactive)
Resolution: Fixed Votes: 0
Labels: None


 Description   

I noticed with Hibernate I sometimes get java.sql.SQLException: There is an open result set on the current connection, which must be closed prior to executing a query when retrieving a result, depending on the complexity of the entity I am loading. Apparently Hibernate is relying on the specified behavior of the Statement to implicitly close any open ResultSet when performing a new execution.

From JDBC Spec - 15.2.5 Closing a ResultSet Object

...
A ResultSet object is implicitly closed when

  • The associated Statement object is re-executed

I worked around this in a local branch with the following code:

org.mariadb.jdbc.MySQLStatement

protected boolean execute(Query query) throws SQLException {
    synchronized (protocol) {
        if (protocol.activeResult != null) {
            protocol.activeResult.close();
        }
        // snip
    }
}



 Comments   
Comment by Massimo Siani (Inactive) [ 2014-07-07 ]

Test case in rev 514

Comment by Lennart Schedin [ 2014-09-10 ]

I looked at the test case in http://bazaar.launchpad.net/~maria-captains/mariadb-java-client/trunk/revision/514. Although it tests the closing I cannot get the test case to fail. I feel that it may be better to have a test case that fails before a bug fix and passes after because it is a higher probability that the test case actually tests the bug fix. I made an attempt to write a test case that failed, but I was unsuccessful.

I not that used with Bazaar so I cannot find the fix in the source code. Could I get a link?

Comment by Lennart Schedin [ 2014-09-10 ]

I have taken the liberty to write a better test case that shows the error. Feel free to add to the source code.

ResultSetCloseTest.java

package org.mariadb.jdbc;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
import org.junit.Test;
 
public class ResultSetCloseTest extends BaseTest {
 
    @Test
    public void testAutomaticCloseResultSet_CONJ90() throws SQLException {
        Statement statement = connection.createStatement();
        statement.execute("drop table if exists resultset_close_test");
        statement.execute("create table resultset_close_test (id int not null primary key auto_increment, data CHAR)");
        statement.execute("insert into resultset_close_test values(null, 'A')");
        statement.close();
 
        //I don't know if it a lack of feature, but the row fetch size for ResulSet must be set to
        //Integer.MIN_VALUE to be able to force streaming of ResultSets
        statement = connection.createStatement();
        statement.setFetchSize(Integer.MIN_VALUE);  
        ResultSet resultSet1 = statement.executeQuery("SELECT * FROM resultset_close_test");
        resultSet1.next();
        resultSet1.previous();
 
        /* 
         * This will trigger the error message
         *   There is an open result set on the current connection, which must be closed prior to executing a query
         */
        ResultSet resultSet2 = statement.executeQuery("SELECT * FROM resultset_close_test");
 
        statement.close();
    }
}
 

Comment by Massimo Siani (Inactive) [ 2014-09-10 ]

Thanks. Setting the row fetch size was necessary to reproduce the bug.
Fixed.

Generated at Thu Feb 08 03:13:06 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.