[CONJ-1132] Inconsistency in getUpdateCount() Return Value for CREATE Statements Created: 2023-12-05  Updated: 2023-12-13  Resolved: 2023-12-13

Status: Closed
Project: MariaDB Connector/J
Component/s: batch
Affects Version/s: 3.3.1
Fix Version/s: 3.3.2

Type: Bug Priority: Major
Reporter: Wenqian Deng Assignee: Diego Dupin
Resolution: Fixed Votes: 0
Labels: None


 Description   

When CREATE statements are executed through executeBatch(), the subsequent call to getUpdateCount() returns -1, which is inconsistent with the behavior observed when executing CREATE statements using the execute() method, where getUpdateCount() returns 0. This behavior is also inconsistent with the MySQL Connector/J, which returns 0 in both scenarios.

@Test
public void test() throws SQLException {
    Connection con = null;
    Statement stmt = null;
    con = DriverManager.getConnection("jdbc:mariadb://localhost:3366/test5?user=user&password=password");
    stmt = con.createStatement();
    stmt.addBatch("CREATE TABLE table211_0(id VARCHAR(5) PRIMARY KEY,value BOOL);");
    stmt.addBatch("CREATE TABLE table211_0(id TINYINT PRIMARY KEY,value SMALLINT);");
    try {
        stmt.executeBatch();
    } catch (Exception e) {
        System.out.println(e);
    }
    System.out.println(stmt.getUpdateCount()); // -1
}



 Comments   
Comment by Diego Dupin [ 2023-12-05 ]

Just to confirm, the example you indicate return the expected behavior (javadoc)

the current result as an update count; -1 if the current result is a ResultSet object or there are no more results

The problem reside in executeX when an error occur, updateCount not being reset:

@Test
  public void getUpdateCountValue() throws SQLException {
    try (Statement st = sharedConn.createStatement()) {
      st.execute("DROP TABLE IF EXISTS getUpdateCountValue");
      try (Statement stmt = sharedConn.createStatement()) {
        assertEquals(-1, stmt.getUpdateCount());
        assertEquals(0, stmt.executeUpdate("CREATE TABLE getUpdateCountValue(id VARCHAR(5) PRIMARY KEY,value BOOL)"));
        assertEquals(0, stmt.getUpdateCount());
        try {
          stmt.executeUpdate("CREATE TABLE getUpdateCountValue(id TINYINT PRIMARY KEY,value SMALLINT");
        } catch (Exception e) {
          // eat
        }
        assertEquals(-1, stmt.getUpdateCount());
      } finally {
        st.execute("DROP TABLE IF EXISTS getUpdateCountValue");
      }
    }
  }

(the example is the expected returns, but would return wrong result with current implementation, correction will be done)
Are we on the same page or do i miss something ?

Comment by Wenqian Deng [ 2023-12-06 ]

Yes, your example is more reasonable than my description.

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