Skip to content

Commit 99dea0e

Browse files
author
diego Dupin
committed
[misc] bulk command correction.
max_allowed_packet is not requested at connection creation. if max_allowed_packet is known and less than 16M (not default) Driver will prevent to add bunch of parameter until reaching max_allowed_packet. if unknown, bulk will split command by bunch of 16Mb, avoiding unknown limitation
1 parent 913f15d commit 99dea0e

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/main/java/org/mariadb/jdbc/client/socket/impl/PacketWriter.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class PacketWriter implements Writer {
3535
protected byte[] buf;
3636
protected int pos = 4;
3737
private int maxPacketLength = MAX_PACKET_LENGTH;
38-
private int maxAllowedPacket;
38+
private Integer maxAllowedPacket;
3939
private long cmdLength;
4040
private boolean permitTrace = true;
4141
private String serverThreadLog = "";
@@ -62,7 +62,7 @@ public PacketWriter(
6262
this.cmdLength = 0;
6363
this.sequence = sequence;
6464
this.compressSequence = compressSequence;
65-
this.maxAllowedPacket = maxAllowedPacket == null ? Integer.MAX_VALUE : maxAllowedPacket;
65+
this.maxAllowedPacket = maxAllowedPacket;
6666
}
6767

6868
public int pos() {
@@ -688,20 +688,24 @@ public void flush() throws IOException {
688688
* @throws MaxAllowedPacketException if query has not to be sent.
689689
*/
690690
private void checkMaxAllowedLength(int length) throws MaxAllowedPacketException {
691-
if (cmdLength + length >= maxAllowedPacket) {
692-
// launch exception only if no packet has been sent.
693-
throw new MaxAllowedPacketException(
694-
"query size ("
695-
+ (cmdLength + length)
696-
+ ") is >= to max_allowed_packet ("
697-
+ maxAllowedPacket
698-
+ ")",
699-
cmdLength != 0);
691+
if (maxAllowedPacket != null) {
692+
if (cmdLength + length >= maxAllowedPacket) {
693+
// launch exception only if no packet has been sent.
694+
throw new MaxAllowedPacketException(
695+
"query size ("
696+
+ (cmdLength + length)
697+
+ ") is >= to max_allowed_packet ("
698+
+ maxAllowedPacket
699+
+ ")",
700+
cmdLength != 0);
701+
}
700702
}
701703
}
702704

703705
public boolean throwMaxAllowedLength(int length) {
704-
return cmdLength + length >= maxAllowedPacket;
706+
if (maxAllowedPacket != null)
707+
return cmdLength + length >= maxAllowedPacket;
708+
return false;
705709
}
706710

707711
public void permitTrace(boolean permitTrace) {

src/main/java/org/mariadb/jdbc/message/client/BulkExecutePacket.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ public int encode(Writer writer, Context context, Prepare newPrepareResult)
124124
break;
125125
}
126126

127+
if (writer.isMarked() && writer.throwMaxAllowedLength(writer.pos())) {
128+
// for max_allowed_packet < 16Mb
129+
// packet length was ok at last mark, but won't with new data
130+
writer.flushBufferStopAtMark();
131+
writer.mark();
132+
lastCmdData = writer.resetMark();
133+
break;
134+
}
135+
127136
writer.mark();
128137

129138
if (writer.bufIsDataAfterMark()) {

src/test/java/org/mariadb/jdbc/integration/BatchTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,27 +272,33 @@ public void largeBatch(Connection con) throws SQLException {
272272
public void bulkPacketSplitMaxAllowedPacket() throws SQLException {
273273
int maxAllowedPacket = getMaxAllowedPacket();
274274
bulkPacketSplit(2, maxAllowedPacket - 40, maxAllowedPacket);
275+
if (maxAllowedPacket >= 16 * 1024 * 1024)
276+
bulkPacketSplit(2, maxAllowedPacket - 40, null);
275277
}
276278

277279
@Test
278280
public void bulkPacketSplitMultiplePacket() throws SQLException {
279281
int maxAllowedPacket = getMaxAllowedPacket();
280282
bulkPacketSplit(4, getMaxAllowedPacket() / 3, maxAllowedPacket);
283+
if (maxAllowedPacket >= 16 * 1024 * 1024)
284+
bulkPacketSplit(4, getMaxAllowedPacket() / 3, null);
281285
}
282286

283287
@Test
284288
public void bulkPacketSplitHugeNbPacket() throws SQLException {
285289
int maxAllowedPacket = getMaxAllowedPacket();
286290
bulkPacketSplit(getMaxAllowedPacket() / 8000, 20, maxAllowedPacket);
291+
if (maxAllowedPacket >= 16 * 1024 * 1024)
292+
bulkPacketSplit(getMaxAllowedPacket() / 8000, 20, null);
287293
}
288294

289-
public void bulkPacketSplit(int nb, int len, int maxAllowedPacket) throws SQLException {
295+
public void bulkPacketSplit(int nb, int len, Integer maxAllowedPacket) throws SQLException {
290296
byte[] arr = new byte[Math.min(16 * 1024 * 1024, len)];
291297
for (int pos = 0; pos < arr.length; pos++) {
292298
arr[pos] = (byte) ((pos % 60) + 65);
293299
}
294300

295-
try (Connection con = createCon("&useServerPrepStmts&useBulkStmts&maxAllowedPacket=" + maxAllowedPacket)) {
301+
try (Connection con = createCon("&useServerPrepStmts&useBulkStmts" + (maxAllowedPacket != null ? "&maxAllowedPacket=" + maxAllowedPacket: ""))) {
296302
Statement stmt = con.createStatement();
297303
stmt.execute("TRUNCATE BatchTest");
298304
stmt.execute("START TRANSACTION"); // if MAXSCALE ensure using WRITER

0 commit comments

Comments
 (0)