@@ -14242,4 +14242,99 @@ public void testBug77658() throws Exception {
1424214242 interruptorExecService.shutdown();
1424314243 }
1424414244
14245+ /**
14246+ * Tests fix for Bug#118201 (Bug#37971552), A potential bug in Mysql Connector/J.
14247+ *
14248+ * @throws Exception
14249+ */
14250+ @Test
14251+ void testBug118201() throws Exception {
14252+ createTable("testBug118201", "(id INT PRIMARY KEY)");
14253+
14254+ boolean mltQry = false;
14255+ boolean rwBS = false;
14256+ boolean contBE = false;
14257+ boolean useSPS = false;
14258+
14259+ Properties props = new Properties();
14260+ do {
14261+ props.setProperty(PropertyKey.allowMultiQueries.getKeyName(), Boolean.toString(mltQry));
14262+ props.setProperty(PropertyKey.rewriteBatchedStatements.getKeyName(), Boolean.toString(rwBS));
14263+ props.setProperty(PropertyKey.continueBatchOnError.getKeyName(), Boolean.toString(contBE));
14264+
14265+ this.stmt.execute("TRUNCATE TABLE testBug118201");
14266+ this.stmt.executeUpdate("INSERT INTO testBug118201 VALUES (3)");
14267+
14268+ // Test Statement.
14269+ String testCase = String.format("Case [mulitQueries: %s, rwBS: %s, contBE: %s]", mltQry ? "Y" : "N", rwBS ? "Y" : "N", contBE ? "Y" : "N");
14270+ try (Connection testConn = getConnectionWithProps(props)) {
14271+ Statement testStmt = testConn.createStatement();
14272+ testStmt.addBatch("INSERT INTO testBug118201 VALUES (1)");
14273+ testStmt.addBatch("INSERT INTO testBug118201 VALUES (2)");
14274+ testStmt.addBatch("INSERT INTO testBug118201 VALUES (3)");
14275+ testStmt.addBatch("INSERT INTO testBug118201 VALUES (4)");
14276+ testStmt.addBatch("INSERT INTO testBug118201 VALUES (5)");
14277+ assertThrows(BatchUpdateException.class, "(?i)Duplicate entry '3' for key 'testBug118201.PRIMARY'", testStmt::executeBatch);
14278+ }
14279+
14280+ this.rs = this.stmt.executeQuery("SELECT * FROM testBug118201");
14281+ assertTrue(this.rs.next(), testCase);
14282+ assertEquals(1, this.rs.getInt(1), testCase);
14283+ assertTrue(this.rs.next(), testCase);
14284+ assertEquals(2, this.rs.getInt(1), testCase);
14285+ assertTrue(this.rs.next(), testCase);
14286+ assertEquals(3, this.rs.getInt(1), testCase);
14287+ if (!rwBS && contBE) {
14288+ assertTrue(this.rs.next(), testCase);
14289+ assertEquals(4, this.rs.getInt(1), testCase);
14290+ assertTrue(this.rs.next(), testCase);
14291+ assertEquals(5, this.rs.getInt(1), testCase);
14292+ }
14293+ assertFalse(this.rs.next(), testCase);
14294+
14295+ do {
14296+ this.stmt.execute("TRUNCATE TABLE testBug118201");
14297+ this.stmt.executeUpdate("INSERT INTO testBug118201 VALUES (3)");
14298+
14299+ // Test client and server PreparedStatement.
14300+ testCase = String.format("Case [mulitQueries: %s, rwBS: %s, contBE: %s, useSPS: %s]", mltQry ? "Y" : "N", rwBS ? "Y" : "N", contBE ? "Y" : "N",
14301+ useSPS ? "Y" : "N");
14302+ props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), Boolean.toString(useSPS));
14303+ try (Connection testConn = getConnectionWithProps(props)) {
14304+ PreparedStatement testPstmt = testConn.prepareStatement("INSERT INTO testBug118201 VALUES (?)");
14305+ testPstmt.setInt(1, 1);
14306+ testPstmt.addBatch();
14307+ testPstmt.setInt(1, 2);
14308+ testPstmt.addBatch();
14309+ testPstmt.setInt(1, 3);
14310+ testPstmt.addBatch();
14311+ testPstmt.setInt(1, 4);
14312+ testPstmt.addBatch();
14313+ testPstmt.setInt(1, 5);
14314+ testPstmt.addBatch();
14315+ assertThrows(BatchUpdateException.class, "(?i)Duplicate entry '3' for key 'testBug118201.PRIMARY'", testPstmt::executeBatch);
14316+ }
14317+
14318+ this.rs = this.stmt.executeQuery("SELECT * FROM testBug118201");
14319+ assertTrue(this.rs.next(), testCase);
14320+ if (rwBS) {
14321+ assertEquals(3, this.rs.getInt(1), testCase);
14322+ } else {
14323+ assertEquals(1, this.rs.getInt(1), testCase);
14324+ assertTrue(this.rs.next(), testCase);
14325+ assertEquals(2, this.rs.getInt(1), testCase);
14326+ assertTrue(this.rs.next(), testCase);
14327+ assertEquals(3, this.rs.getInt(1), testCase);
14328+ if (contBE) {
14329+ assertTrue(this.rs.next(), testCase);
14330+ assertEquals(4, this.rs.getInt(1), testCase);
14331+ assertTrue(this.rs.next(), testCase);
14332+ assertEquals(5, this.rs.getInt(1), testCase);
14333+ }
14334+ }
14335+ assertFalse(this.rs.next(), testCase);
14336+ } while (useSPS = !useSPS);
14337+ } while ((mltQry = !mltQry) || (rwBS = !rwBS) || (contBE = !contBE));
14338+ }
14339+
1424514340}
0 commit comments