Skip to content

Commit

Permalink
max allowed packet < 16Mb skipping packet correction
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Mar 25, 2021
1 parent e1b02d0 commit 838cf34
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ install:
connector-test-machine/launch.bat -t "$srv" -v "$v" -d testj
;;
linux)
source connector-test-machine/launch.sh -t "$srv" -v "$v" -d testj -n 0 -l "$local"
source connector-test-machine/launch.sh -t "$srv" -v "$v" -d testj -n 0 -l "$local" -p "$packet"
;;
esac
jobs:
Expand All @@ -47,6 +47,7 @@ jobs:
- env: srv=mariadb v=10.3 local=1
- env: srv=mariadb v=10.4 local=1
- env: srv=mariadb v=10.5
- env: srv=mariadb v=10.5 packet=8
- env: srv=mariadb v=10.5 local=1 BENCH=1
- env: srv=maxscale
- env: srv=skysql
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/org/mariadb/jdbc/client/ClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.mariadb.jdbc.util.constants.HaMode;
import org.mariadb.jdbc.util.constants.ServerStatus;
import org.mariadb.jdbc.util.exceptions.ExceptionFactory;
import org.mariadb.jdbc.util.exceptions.MaxAllowedPacketException;
import org.mariadb.jdbc.util.log.Logger;
import org.mariadb.jdbc.util.log.Loggers;

Expand Down Expand Up @@ -427,6 +428,11 @@ public int sendQuery(ClientMessage message) throws SQLException {
try {
return message.encode(writer, context);
} catch (IOException ioException) {
if (ioException instanceof MaxAllowedPacketException) {
throw exceptionFactory
.withSql(message.description())
.create("Packet too big for current server max_allowed_packet value", "HZ000", ioException);
}
destroySocket();
throw exceptionFactory
.withSql(message.description())
Expand Down Expand Up @@ -462,12 +468,13 @@ public List<Completion> executePipeline(
for (int i = 0; i < messages.length; i++) {
responseMsg[i] = sendQuery(messages[i]);
}
for (; readCounter < messages.length; readCounter++) {
for (int j = 0; j < responseMsg[readCounter]; j++) {
for (; readCounter < messages.length; ) {
readCounter++;
for (int j = 0; j < responseMsg[readCounter - 1]; j++) {
results.addAll(
readResponse(
stmt,
messages[readCounter],
messages[readCounter - 1],
fetchSize,
maxRows,
resultSetConcurrency,
Expand All @@ -479,13 +486,13 @@ public List<Completion> executePipeline(
} catch (SQLException sqlException) {

// read remaining results
for (int i = ++readCounter; i < messages.length; i++) {
for (int j = 0; j < responseMsg[readCounter]; j++) {
for (int i = readCounter; i < messages.length; i++) {
for (int j = 0; j < responseMsg[i]; j++) {
try {
results.addAll(
readResponse(
stmt,
messages[readCounter],
messages[i],
fetchSize,
maxRows,
resultSetConcurrency,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.mariadb.jdbc.MariaDbBlob;
import org.mariadb.jdbc.MariaDbClob;
import org.mariadb.jdbc.Statement;
import org.mariadb.jdbc.util.exceptions.MaxAllowedPacketException;

public class PreparedStatementParametersTest extends Common {

Expand Down Expand Up @@ -450,7 +451,33 @@ public void bigSend(Connection con, String st) throws SQLException {
ResultSet rs = stmt.executeQuery("SELECT t2 from bigTest WHERE t1 = 1");
assertTrue(rs.next());
assertEquals(st, rs.getString(1));
stmt.execute("COMMIT");
con.commit();
}

@Test
public void bigSendError() throws SQLException {
int maxAllowedPacket = getMaxAllowedPacket();
Assumptions.assumeTrue(maxAllowedPacket < 10 * 1024 * 1024);
char[] arr = new char[10 * 1024 * 1024];
for (int pos = 0; pos < arr.length; pos++) {
arr[pos] = (char) ('A' + (pos % 60));
}
String st = new String(arr);
bigSendError(sharedConn, st);
bigSendError(sharedConnBinary, st);
}


public void bigSendError(Connection con, String st) throws SQLException {
Statement stmt = con.createStatement();
stmt.execute("TRUNCATE bigTest");
stmt.execute("START TRANSACTION"); // if MAXSCALE ensure using WRITER
try (PreparedStatement prep = con.prepareStatement("INSERT INTO bigTest VALUES (?, ?)")) {
prep.setInt(1, 1);
prep.setString(2, st);
assertThrowsContains(SQLException.class, () -> prep.execute(), "Packet too big for current server max_allowed_packet value");
}
con.commit();
}

@FunctionalInterface
Expand Down

0 comments on commit 838cf34

Please sign in to comment.