Search in sources :

Example 1 with CJPacketTooBigException

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");
    }
}
Also used : CJPacketTooBigException(com.mysql.cj.exceptions.CJPacketTooBigException) JdbcPropertySetImpl(com.mysql.cj.jdbc.JdbcPropertySetImpl) Test(org.junit.jupiter.api.Test)

Example 2 with CJPacketTooBigException

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);
        }
    }
}
Also used : CJPacketTooBigException(com.mysql.cj.exceptions.CJPacketTooBigException) IOException(java.io.IOException) MessageLite(com.google.protobuf.MessageLite) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException)

Aggregations

CJPacketTooBigException (com.mysql.cj.exceptions.CJPacketTooBigException)2 MessageLite (com.google.protobuf.MessageLite)1 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)1 JdbcPropertySetImpl (com.mysql.cj.jdbc.JdbcPropertySetImpl)1 IOException (java.io.IOException)1 Test (org.junit.jupiter.api.Test)1