[CONJ-424] JDBC connector 1.5.6+: preparedStatement + getGeneratedKeys() = faulty exception on _first_ resultset.next() Created: 2017-02-03  Updated: 2017-02-09  Resolved: 2017-02-09

Status: Closed
Project: MariaDB Connector/J
Component/s: Other
Affects Version/s: 1.5.7
Fix Version/s: 1.5.8

Type: Bug Priority: Major
Reporter: Daniel Nilsson Assignee: Diego Dupin
Resolution: Fixed Votes: 0
Labels: jdbc


 Description   

{{ connection = dataSource.getConnection();
statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
<... >
result = statement.executeUpdate();

ResultSet rs = statement.getGeneratedKeys();
}}

rs.next() now throws java.sql.SQLException: Operation not permit on a closed resultSet.

This should not happen on first call to rs.next().

Bug introduced in 1.5.7, 1.5.6 behaves as it should with regards to this.



 Comments   
Comment by Diego Dupin [ 2017-02-06 ]

could you post full stracktrace ? I fail to reproduced these issue

(try with this works well

@Test
public void testt()throws Exception {
 
    try (Connection connection = DriverManager.getConnection("jdbc:mariadb://localhost/testj?user=root")) {
        try (Statement statement = connection.createStatement()) {
            statement.execute("DROP TABLE IF EXISTS tableTest");
            statement.execute("CREATE TABLE tableTest(id int auto_increment primary key, data varchar(20)) ");
        }
        try (PreparedStatement prepStmt = sharedConnection.prepareStatement("INSERT INTO tableTest(data) values ('data')", Statement.RETURN_GENERATED_KEYS)) {
            prepStmt.executeUpdate();
 
            ResultSet rs = prepStmt.getGeneratedKeys();
            rs.next();
            Assert.assertEquals(1, rs.getInt(1));
        }
    }
}

Comment by Daniel Nilsson [ 2017-02-06 ]

Hi Diego, we're working on providing a good way to reproduce.

I also edited the description of this issue - seems like the problem arrived with 1.5.6, not 1.5.7. 1.5.5 works as expected.

The exception is thrown by the first line of MariaSelectResultSet next() method.

Comment by Daniel Nilsson [ 2017-02-06 ]

I might add - we have an internal test that does reproduce this issue, just have to slim it down to something suitable for your GeneratedKeysTest.java.

Comment by Lennart Schedin [ 2017-02-06 ]

Use this code to reproduce the problem (in the GeneratedKeysTest.java test class context):

    /**
     * CONJ-424: Calling getGeneratedKeys() two times on the same connection, with different
     * PreparedStatement on a table that does not have an auto increament.
     */
    @Test
    public void testGeneratedKeysWithoutTableAutoIncrementCalledTwice() throws SQLException {
        createTable("gen_key_test_resultset", "name VARCHAR(40) NOT NULL, xml MEDIUMTEXT");
        String sql = "INSERT INTO gen_key_test_resultset (name, xml) VALUES (?, ?)";
        PreparedStatement preparedStatement;
        ResultSet generatedKeysResultSet;
        for (int i = 0; i < 2; i++) {
            preparedStatement = sharedConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            preparedStatement.setString(1, "John");
            preparedStatement.setString(2, "<xml/>");
            preparedStatement.executeUpdate();
 
            generatedKeysResultSet = preparedStatement.getGeneratedKeys();
            if (generatedKeysResultSet.next()) {
                Assert.fail("Should not get here!");
            }
            generatedKeysResultSet.close();
            preparedStatement.close();
        }
    }

Comment by Diego Dupin [ 2017-02-08 ]

correction in done in 1.5.8-SNAPSHOT, available using :

<repositories>
    <repository>
        <id>sonatype-nexus-snapshots</id>
        <name>Sonatype Nexus Snapshots</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </repository>
</repositories>
 
<dependencies>
    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <version>1.5.8-SNAPSHOT</version>
    </dependency>
</dependencies>

could you confirm it solve this issue ?

Comment by Daniel Nilsson [ 2017-02-09 ]

Thanks Diego!

I've been busy trying to write a workaround here for another issue that we face - apparently you now by default enable STRICT_TRANS_TABLES with gives us some grief. A ticket is coming on that aswell

I have to try and finish a workaround for that before I get to testing your fix. Not sure if I can manage before the weekend, sorry.

Comment by Diego Dupin [ 2017-02-09 ]

danielnilssonse, JDBC required that when a data truncation occur an exception is thrown.
That correspond to having STRICT_TRANS_TABLES set.

So that's the default implementation, but can be disabled using jdbcCompliantTruncation=false in connection string. see more in docs

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