Commit aed3bda6 authored by terrymanu's avatar terrymanu
Browse files

SQL exception should throw and catch at MySQLFrontendHandler to gen ErrPacket

parent fe01d25c
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ public interface BackendHandler {
     * Get result value.
     *
     * @return database packet of result value
     * @throws SQLException SQL exception
     */
    DatabasePacket getResultValue();
    DatabasePacket getResultValue() throws SQLException;
}
+6 −10
Original line number Diff line number Diff line
@@ -138,17 +138,13 @@ public final class JDBCBackendHandler implements BackendHandler {
    }
    
    @Override
    public DatabasePacket getResultValue() {
    public DatabasePacket getResultValue() throws SQLException {
        QueryResponsePackets queryResponsePackets = ((ExecuteQueryResponse) executeResponse).getQueryResponsePackets();
        try {
        int columnCount = queryResponsePackets.getColumnCount();
        List<Object> data = new ArrayList<>(columnCount);
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            data.add(mergedResult.getValue(columnIndex, Object.class));
        }
        return executeEngine.getJdbcExecutorWrapper().createResultSetPacket(++currentSequenceId, data, columnCount, queryResponsePackets.getColumnTypes(), DatabaseType.MySQL);
        } catch (final SQLException ex) {
            return new ErrPacket(++currentSequenceId, ex);
        }
    }
}
+7 −3
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakeRespons
import io.shardingsphere.proxy.util.MySQLResultCache;
import lombok.RequiredArgsConstructor;

import java.sql.SQLException;
import java.util.Collection;

/**
@@ -78,6 +79,7 @@ public final class MySQLFrontendHandler extends FrontendHandler {
            
            @Override
            public void run() {
                int currentSequenceId = 0;
                try (MySQLPacketPayload payload = new MySQLPacketPayload(message);
                     BackendConnection backendConnection = new BackendConnection()) {
                    int sequenceId = payload.readInt1();
@@ -90,17 +92,19 @@ public final class MySQLFrontendHandler extends FrontendHandler {
                            return;
                        }
                    }
                    sequenceId = packets.size();
                    currentSequenceId = packets.size();
                    while (commandPacket.next()) {
                        // TODO try to use wait notify
                        while (!context.channel().isWritable()) {
                            continue;
                        }
                        DatabasePacket resultValue = commandPacket.getResultValue();
                        sequenceId = resultValue.getSequenceId();
                        currentSequenceId = resultValue.getSequenceId();
                        context.writeAndFlush(resultValue);
                    }
                    context.writeAndFlush(new EofPacket(++sequenceId));
                    context.writeAndFlush(new EofPacket(++currentSequenceId));
                } catch (final SQLException ex) {
                    context.writeAndFlush(new ErrPacket(++currentSequenceId, ex));
                }
            }
        });
+6 −2
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import io.shardingsphere.proxy.transport.common.packet.DatabasePacket;
import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacket;
import io.shardingsphere.proxy.transport.mysql.packet.command.reponse.CommandResponsePackets;

import java.sql.SQLException;

/**
 * Command packet.
 *
@@ -40,13 +42,15 @@ public interface CommandPacket extends MySQLPacket {
     * Goto next result value.
     *
     * @return has more result value or not
     * @throws SQLException SQL exception
     */
    boolean next();
    boolean next() throws SQLException;
    
    /**
     * Get result value.
     *
     * @return database packet of result value
     * @throws SQLException SQL exception
     */
    DatabasePacket getResultValue();
    DatabasePacket getResultValue() throws SQLException;
}
+3 −7
Original line number Diff line number Diff line
@@ -162,16 +162,12 @@ public final class ComStmtExecutePacket implements CommandPacket {
    }
    
    @Override
    public boolean next() {
        try {
    public boolean next() throws SQLException {
        return jdbcBackendHandler.next();
        } catch (final SQLException ex) {
            return false;
        }
    }
    
    @Override
    public DatabasePacket getResultValue() {
    public DatabasePacket getResultValue() throws SQLException {
        return jdbcBackendHandler.getResultValue();
    }
}
Loading