Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-1071] Error during Bulk execution might result in connection wr…
…ong state
  • Loading branch information
rusher committed Apr 26, 2023
1 parent fa51fd4 commit 1ea84e3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java
Expand Up @@ -543,7 +543,7 @@ public List<Completion> executePipeline(
boolean canRedo)
throws SQLException {
List<Completion> results = new ArrayList<>();

int perMsgCounter = 0;
int readCounter = 0;
int[] responseMsg = new int[messages.length];
try {
Expand All @@ -566,7 +566,7 @@ public List<Completion> executePipeline(
}
while (readCounter < messages.length) {
readCounter++;
for (int j = 0; j < responseMsg[readCounter - 1]; j++) {
for (perMsgCounter = 0; perMsgCounter < responseMsg[readCounter - 1]; perMsgCounter++) {
results.addAll(
readResponse(
stmt,
Expand All @@ -583,6 +583,23 @@ public List<Completion> executePipeline(
} catch (SQLException sqlException) {
if (!closed) {
// read remaining results
perMsgCounter++;
for (; perMsgCounter < responseMsg[readCounter - 1]; perMsgCounter++) {
try {
results.addAll(
readResponse(
stmt,
messages[readCounter - 1],
fetchSize,
maxRows,
resultSetConcurrency,
resultSetType,
closeOnCompletion));
} catch (SQLException e) {
// eat
}
}

for (int i = readCounter; i < messages.length; i++) {
for (int j = 0; j < responseMsg[i]; j++) {
try {
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/org/mariadb/jdbc/integration/BulkStmtSplitError.java
@@ -0,0 +1,49 @@
package org.mariadb.jdbc.integration;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.junit.jupiter.api.*;
import org.mariadb.jdbc.Statement;

public class BulkStmtSplitError extends Common {

@AfterAll
public static void drop() throws SQLException {
Statement stmt = sharedConn.createStatement();
stmt.execute("DROP TABLE IF EXISTS BulkStmtSplitError");
}

@BeforeAll
public static void beforeAll2() throws SQLException {
drop();
Statement stmt = sharedConn.createStatement();
stmt.execute(
"CREATE TABLE BulkStmtSplitError (t1 int not null primary key, field varchar(300))");
stmt.execute("FLUSH TABLES");
}

@Test
public void BulkStmtSplitError() throws SQLException {
Statement stmt = sharedConn.createStatement();
stmt.execute("INSERT INTO BulkStmtSplitError VALUES (1, 'tt'), (2, 'tt2')");

try (PreparedStatement prep =
sharedConn.prepareStatement("insert into BulkStmtSplitError values (?, ?)")) {
prep.setInt(1, 1);
prep.setNull(2, Types.VARCHAR);
prep.addBatch();
prep.setInt(1, 2);
prep.setString(2, "Kenny");
prep.addBatch();
prep.executeBatch();
} catch (SQLException e) {
// eat
}

ResultSet rs = stmt.executeQuery("SELECT count(*) FROM agent");

This comment has been minimized.

Copy link
@RadekWikturna

RadekWikturna Apr 26, 2023

This is table likely does not exist in this test case - I wonder how this can work!
I provided this table in my example code.

This comment has been minimized.

Copy link
@rusher

rusher Jun 6, 2023

Author Collaborator

because of CREATE TABLE BulkStmtSplitError in @BeforeAll

This comment has been minimized.

Copy link
@RadekWikturna

RadekWikturna Jun 6, 2023

Sorry but I can't see a way this can pass.
Line 23 creates table BulkStmtSplitError but line 45 reads from table agent !

This comment has been minimized.

Copy link
@rusher

rusher Jun 6, 2023

Author Collaborator

yes, i've just a correction : commit 75a8736 the test wasn't executed because of class wasn't properly named.
Thanks for pointing that out !

Assertions.assertTrue(rs.next());
Assertions.assertEquals(2, rs.getInt(1));
}
}

0 comments on commit 1ea84e3

Please sign in to comment.