[CONJ-953] PreparedStatement.getGeneratedKeys() returns rows when no keys are generated in insert Created: 2022-04-06  Updated: 2022-06-22  Resolved: 2022-06-22

Status: Closed
Project: MariaDB Connector/J
Component/s: JDBC compatibility
Affects Version/s: 3.0.3
Fix Version/s: 3.0.6

Type: Bug Priority: Major
Reporter: John Doe Assignee: Diego Dupin
Resolution: Fixed Votes: 0
Labels: None


 Description   

In version 2.6.1 there is no problem. But with mariadb-java-client 3.0.3 there is.

Here is a negative test to describe the problem. Mind you that you need a working connection for the test to work and this example code does not close anything to keep things simple.

rs.next() should return false, because getGeneratedKeys() API documentation says
"If this Statement object didnot generate any keys, an empty ResultSetobject is returned."

Instead, with the new version it returns true, and value 0 is returned when no keys are generated.

    @Test
    public void testGetKeys() throws SQLException {
        ds = getDataSource();
        conn = ds.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute("CREATE TABLE IF NOT EXISTS `KEYTEST` (`id` INT(11) NOT NULL);");
        PreparedStatement ps = conn.prepareStatement("INSERT INTO `KEYTEST`(`id`) VALUES(5);", Statement.RETURN_GENERATED_KEYS);
        ps.execute();
        ResultSet rs = ps.getGeneratedKeys();
        assertTrue(rs.next());
        assertEquals(0, rs.getInt(1));
    }


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