Skip to content

Commit

Permalink
[CONJ-1200] BULK big parameters might be sent without bulk header, re…
Browse files Browse the repository at this point in the history
…sulting in "Unknown command"
  • Loading branch information
rusher committed Oct 7, 2024
1 parent 6316ba8 commit ded788f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -673,12 +673,8 @@ private void growBuffer(int len) throws IOException {
return;
}

// need to keep all data, buf can grow more than maxPacketLength
// grow buf if needed
if (bufLength == maxPacketLength) return;
if (len + pos > newCapacity) {
newCapacity = Math.min(maxPacketLength, len + pos);
}
growBuffer(len);
return;
}
}

Expand Down Expand Up @@ -808,8 +804,7 @@ public void flushBufferStopAtMark() throws IOException {
pos = mark;
writeSocket(true);
out.flush();
initPacket();

cmdLength = 0;
System.arraycopy(buf, mark, buf, pos, end - mark);
pos += end - mark;
mark = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public int encode(Writer writer, Context context, Prepare newPrepareResult)
}
}

if (!writer.isMarked() && writer.hasFlushed()) {
if (!writer.bufIsDataAfterMark() && !writer.isMarked() && writer.hasFlushed()) {
// parameter were too big to fit in a MySQL packet
// need to finish the packet separately
writer.flush();
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/mariadb/jdbc/integration/BatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ public static void beforeAll2() throws SQLException {
"CREATE TABLE BatchTest (t1 int not null primary key auto_increment, t2 LONGTEXT)");
createSequenceTables();
stmt.execute("CREATE TABLE timestampCal(id int, val TIMESTAMP)");
stmt.execute(
"CREATE TABLE batchParamTest (sn_key INTEGER, sn_name VARCHAR(255), sn_description"
+ " VARCHAR(255), sn_object LONGBLOB, CONSTRAINT pk_test_i PRIMARY KEY (sn_key) ) ");
}

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

@Test
Expand Down Expand Up @@ -657,6 +661,38 @@ private void batchWithError(Connection con) throws SQLException {
}
}

@Test
public void bigParameterFlushTest() throws SQLException {
int maxAllowedPacket = getMaxAllowedPacket();
Assumptions.assumeTrue(maxAllowedPacket > 22 * 1024 * 1024);
String insertStatement =
"INSERT INTO batchParamTest (sn_key, sn_name, sn_description, sn_object) VALUES (?, ?, ?,"
+ " ?)";
try (PreparedStatement prep = sharedConn.prepareStatement(insertStatement)) {
prep.setInt(1, 1000);
prep.setString(2, "name1");
prep.setString(3, "desc1");
prep.setBytes(4, new byte[334004]);
prep.addBatch();

prep.setInt(1, 1001);
prep.setString(2, "name2");
prep.setString(3, "desc2");
prep.setBytes(4, new byte[21963743]);
prep.addBatch();

prep.executeBatch();
prep.clearBatch();
sharedConn.commit();
}
Statement stmt = sharedConn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM batchParamTest");
assertTrue(rs.next());
assertEquals(334004, rs.getBytes(4).length);
assertTrue(rs.next());
assertEquals(21963743, rs.getBytes(4).length);
}

@Test
public void ensureCalendarSync() throws SQLException {
Assumptions.assumeTrue(isMariaDBServer() && !isXpand());
Expand Down

0 comments on commit ded788f

Please sign in to comment.