Repository: apache/shardingsphere
Issue: Proxy raised an out of bound exception

Please answer these questions before submitting your issue. Thanks!
开源不易，我们希望将精力放在完成新功能和解决有价值的问题上，为了让大家的配合更具有效率，请填写以下列出的全部问题

### Which version of Sharding-Jdbc do you using?（您使用的Sharding-Jdbc版本为？）
dev

### Expected behavior （您预期的结果是）
Read data correctly from the ByteBuf of sharding-proxy.

### Actual behavior （实际运行的结果是）
[WARN ] 17:20:14.713 [nioEventLoopGroup-5-3] i.n.u.c.AbstractEventExecutor - A task raised an exception. Task: io.shardingjdbc.proxy.frontend.mysql.MySQLFrontendHandler$1@10d19d69
java.lang.IndexOutOfBoundsException: readerIndex(1040) + length(1) exceeds writerIndex(17): PooledUnsafeDirectByteBuf(ridx: 0, widx: 17, cap: 2048)
	at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1401) ~[netty-buffer-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.buffer.AbstractByteBuf.readByte(AbstractByteBuf.java:707) ~[netty-buffer-4.1.16.Final.jar:4.1.16.Final]
	at io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload.readInt1(MySQLPacketPayload.java:51) ~[classes/:na]
	at io.shardingjdbc.proxy.frontend.mysql.MySQLFrontendHandler$1.run(MySQLFrontendHandler.java:69) ~[classes/:na]
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-common-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403) [netty-common-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) [netty-common-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:138) [netty-common-4.1.16.Final.jar:4.1.16.Final]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_162]

### Steps to reproduce the behavior （可重现问题的操作步骤）
This Exception bust randomly, but not difficult to be reproduced by continuously send message to sharding-proxy for not a very long time.

### Please provide the reproduce example codes (such as github link)，otherwise we will label the issue as Invalid and close it.（为了节省复现问题的时间，请务必提供可重现的代码，否则我们会将issue直接标记为invalid并关闭）
https://github.com/shardingjdbc/sharding-proxy-example

Code should based on https://github.com/shardingjdbc/sharding-jdbc-example
（代码请基于 https://github.com/shardingjdbc/sharding-jdbc-example）


Changed files:
sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/StatementExecuteBackendHandler.java
sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java
sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/codec/MySQLPacketCodec.java

diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java
index f146fbeb129..1bbbc95a67b 100644
--- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java
+++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java
@@ -58,6 +58,8 @@ import java.util.List;
  */
 public final class SQLExecuteBackendHandler implements BackendHandler {
     
+    private static final Integer FETCH_ONE_ROW_A_TIME = Integer.MIN_VALUE;
+    
     private final String sql;
     
     private final StatementRoutingEngine routingEngine;
@@ -116,7 +118,7 @@ public final class SQLExecuteBackendHandler implements BackendHandler {
             Connection connection = dataSource.getConnection();
             connections.add(connection);
             Statement statement = connection.createStatement();
-            statement.setFetchSize(Integer.MIN_VALUE);
+            statement.setFetchSize(FETCH_ONE_ROW_A_TIME);
             resultSets.add(statement.executeQuery(sql));
             return getQueryDatabaseProtocolPackets();
         } catch (final SQLException ex) {
@@ -226,7 +228,7 @@ public final class SQLExecuteBackendHandler implements BackendHandler {
             return mergeDML(headPackets);
         }
         if (SQLType.DQL == sqlStatement.getType()) {
-            return mergeDQLorDAL(sqlStatement, packets);
+            return mergeDQL(sqlStatement, packets);
         }
         return packets.get(0);
     }
@@ -242,7 +244,7 @@ public final class SQLExecuteBackendHandler implements BackendHandler {
         return new CommandResponsePackets(new OKPacket(1, affectedRows, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
     }
     
-    private CommandResponsePackets mergeDQLorDAL(final SQLStatement sqlStatement, final List<CommandResponsePackets> packets) {
+    private CommandResponsePackets mergeDQL(final SQLStatement sqlStatement, final List<CommandResponsePackets> packets) {
         List<QueryResult> queryResults = new ArrayList<>(packets.size());
         for (int i = 0; i < packets.size(); i++) {
             // TODO replace to a common PacketQueryResult
diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/StatementExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/StatementExecuteBackendHandler.java
index a98defd416d..80fb98fe5be 100644
--- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/StatementExecuteBackendHandler.java
+++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/StatementExecuteBackendHandler.java
@@ -61,6 +61,8 @@ import java.util.List;
  */
 public final class StatementExecuteBackendHandler implements BackendHandler {
     
+    private static final Integer FETCH_ONE_ROW_A_TIME = Integer.MIN_VALUE;
+    
     private final List<PreparedStatementParameter> preparedStatementParameters;
     
     private final PreparedStatementRoutingEngine routingEngine;
@@ -137,7 +139,7 @@ public final class StatementExecuteBackendHandler implements BackendHandler {
             Connection connection = dataSource.getConnection();
             connections.add(connection);
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
-            preparedStatement.setFetchSize(Integer.MIN_VALUE);
+            preparedStatement.setFetchSize(FETCH_ONE_ROW_A_TIME);
             setJDBCPreparedStatementParameters(preparedStatement);
             resultSets.add(preparedStatement.executeQuery());
             return getQueryDatabaseProtocolPackets();
@@ -264,7 +266,7 @@ public final class StatementExecuteBackendHandler implements BackendHandler {
             return mergeDML(headPackets);
         }
         if (SQLType.DQL == sqlStatement.getType()) {
-            return mergeDQLorDAL(sqlStatement, packets);
+            return mergeDQL(sqlStatement, packets);
         }
         return packets.get(0);
     }
@@ -280,7 +282,7 @@ public final class StatementExecuteBackendHandler implements BackendHandler {
         return new CommandResponsePackets(new OKPacket(1, affectedRows, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
     }
     
-    private CommandResponsePackets mergeDQLorDAL(final SQLStatement sqlStatement, final List<CommandResponsePackets> packets) {
+    private CommandResponsePackets mergeDQL(final SQLStatement sqlStatement, final List<CommandResponsePackets> packets) {
         List<QueryResult> queryResults = new ArrayList<>(packets.size());
         for (int i = 0; i < packets.size(); i++) {
             // TODO replace to a common PacketQueryResult
diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/codec/MySQLPacketCodec.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/codec/MySQLPacketCodec.java
index 7ef4c7f95b2..27b73ecf622 100644
--- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/codec/MySQLPacketCodec.java
+++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/codec/MySQLPacketCodec.java
@@ -45,11 +45,7 @@ public final class MySQLPacketCodec extends PacketCodec<MySQLPacket> {
             in.resetReaderIndex();
             return;
         }
-        if (readableBytes > realPacketLength) {
-            out.add(in.readRetainedSlice(payloadLength + MySQLPacket.SEQUENCE_LENGTH));
-            return;
-        }
-        out.add(in);
+        out.add(in.readRetainedSlice(payloadLength + MySQLPacket.SEQUENCE_LENGTH));
     }
     
     @Override