Uploaded image for project: 'MariaDB Connector/J'
  1. MariaDB Connector/J
  2. CONJ-953

PreparedStatement.getGeneratedKeys() returns rows when no keys are generated in insert

    XMLWordPrintable

Details

    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));
          }
      

      Attachments

        Activity

          People

            diego dupin Diego Dupin
            johndoe John Doe
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.