use of com.mysql.cj.exceptions.CJPacketTooBigException in project aws-mysql-jdbc by awslabs.
the class SimplePacketReaderTest method exceedMaxAllowedPacketHeaderRead.
// test checking of maxAllowedPacket
@Test
public void exceedMaxAllowedPacketHeaderRead() throws IOException {
RuntimeProperty<Integer> maxAllowedPacket = new JdbcPropertySetImpl().getProperty(PropertyKey.maxAllowedPacket);
maxAllowedPacket.setValue(1024);
// read header with packet size = maxAllowedPacket => SUCCESS
MockSocketConnection connection = new FixedBufferSocketConnection(new byte[] { 0, 4, 0, 42 });
MessageReader<NativePacketHeader, NativePacketPayload> reader = new SimplePacketReader(connection, maxAllowedPacket);
NativePacketHeader hdr = reader.readHeader();
assertEquals(1024, hdr.getMessageSize());
// read header with packet size = maxAllowedPacket + 1 => ERROR
connection = new FixedBufferSocketConnection(new byte[] { 1, 4, 0, 42 });
reader = new SimplePacketReader(connection, maxAllowedPacket);
try {
reader.readHeader();
fail("Should throw exception as packet size exceeds maxAllowedPacket");
} catch (CJPacketTooBigException ex) {
assertTrue(connection.forceClosed, "Connection should be force closed when maxAllowedPacket is exceeded");
}
}
use of com.mysql.cj.exceptions.CJPacketTooBigException in project aws-mysql-jdbc by awslabs.
the class SyncMessageSender method send.
public void send(XMessage message) {
synchronized (this.waitingAsyncOperationMonitor) {
MessageLite msg = message.getMessage();
try {
int type = MessageConstants.getTypeForMessageClass(msg.getClass());
int size = 1 + msg.getSerializedSize();
if (this.maxAllowedPacket > 0 && size > this.maxAllowedPacket) {
throw new CJPacketTooBigException(Messages.getString("PacketTooBigException.1", new Object[] { size, this.maxAllowedPacket }));
}
// for debugging
// System.err.println("Initiating write of message (size=" + size + ", tag=" + ClientMessages.Type.valueOf(type) + ")");
byte[] sizeHeader = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(size).array();
this.outputStream.write(sizeHeader);
this.outputStream.write(type);
msg.writeTo(this.outputStream);
this.outputStream.flush();
this.previousPacketSentTime = this.lastPacketSentTime;
this.lastPacketSentTime = System.currentTimeMillis();
} catch (IOException ex) {
throw new CJCommunicationsException("Unable to write message", ex);
}
}
}
Aggregations