Bug #110357 setting allowMultiQueries made batch insert fail
Submitted: 14 Mar 2023 10:48 Modified: 15 Mar 2023 11:32
Reporter: Wenqian Deng Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:8.0.32 OS:Any
Assigned to: CPU Architecture:Any

[14 Mar 2023 10:48] Wenqian Deng
Description:
It seems that I find another allowMultiQueries issue.
When I set allowMultiQueries=true and ran the test case, executeBatch threw java.sql.BatchUpdateException: Duplicate entry '1670697762' for key 't0.PRIMARY' and "SELECT * FROM t0" returned 1 record 1670697762.
When I set allowMultiQueries=false and ran the test case, executeBatch also threw java.sql.BatchUpdateException: Duplicate entry '1670697762' for key 't0.PRIMARY', however, this time "SELECT * FROM t0" returned 2 records: 1670697762 and 86274641.
You can check the test case for detailed information(set allowMultiQueries=true or false to see different return values).

How to repeat:
    @Test
    public void test() throws SQLException {
        String url1 = "jdbc:mysql://localhost:3368?user=user&password=password&allowMultiQueries=true";
        Connection con1 = DriverManager.getConnection(url1);

        execute(con1, "DROP DATABASE IF EXISTS test");
        execute(con1, "CREATE DATABASE test");
        execute(con1, "USE test");
        execute(con1, "CREATE TABLE t0(c0 REAL SIGNED  PRIMARY KEY NOT NULL) engine=InnoDB");
        execute(con1, "INSERT INTO t0 VALUES (1670697762)");
        batchExecute(con1, new String[]{"INSERT INTO t0 VALUES (1670697762);", "INSERT INTO t0 VALUES (86274641)"});
        executeAndPrint(con1, "SELECT * FROM t0");
   }

    public void execute(Connection con, String sql)  {
        try {
            Statement statement = con.createStatement();
            statement.execute(sql);
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void executeAndPrint(Connection con, String sql)  {
        try {
            Statement statement = con.createStatement();
            if (statement.execute(sql)) {
                ResultSet rs = statement.getResultSet();
                ResultSetMetaData rsMetaData = rs.getMetaData();
                int count = rsMetaData.getColumnCount();
                StringBuffer sb = new StringBuffer();

                while (rs.next()) {
                    sb.setLength(0);
                    for (int i = 1; i <= count; i++) {
                        sb.append("* " + rs.getString(i) + " *");
                    }
                    System.out.println(sb);
                }
            }
            else {
                System.out.println("count: " + statement.getUpdateCount());
            }
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void batchExecute(Connection con, String[] sql)  {
        try {
            Statement statement = con.createStatement();
            for (String s : sql) {
                statement.addBatch(s);
            }
            statement.executeBatch();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
[15 Mar 2023 11:32] MySQL Verification Team
Hello mingshi wu,

Thank you for the report and test case.

regards,
Umesh