Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize MySQLComStmtExecuteExecutor and MySQLComFieldListPacketExecutor, prevent NPE error when executing close #33766

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ public final class MySQLComStmtExecuteExecutor implements QueryCommandExecutor {

private final ConnectionSession connectionSession;

private ProxyBackendHandler proxyBackendHandler;
private final ProxyBackendHandler proxyBackendHandler;

@Getter
private ResponseType responseType;

@Override
public Collection<DatabasePacket> execute() throws SQLException {
public MySQLComStmtExecuteExecutor(final MySQLComStmtExecutePacket packet, final ConnectionSession connectionSession) throws SQLException {
this.packet = packet;
this.connectionSession = connectionSession;
MySQLServerPreparedStatement preparedStatement = updateAndGetPreparedStatement();
List<Object> params = packet.readParameters(preparedStatement.getParameterTypes(), preparedStatement.getLongData().keySet(), preparedStatement.getParameterColumnDefinitionFlags());
preparedStatement.getLongData().forEach(params::set);
Expand All @@ -81,8 +82,6 @@ public Collection<DatabasePacket> execute() throws SQLException {
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData(), true);
connectionSession.setQueryContext(queryContext);
proxyBackendHandler = ProxyBackendHandlerFactory.newInstance(TypedSPILoader.getService(DatabaseType.class, "MySQL"), queryContext, connectionSession, true);
ResponseHeader responseHeader = proxyBackendHandler.execute();
return responseHeader instanceof QueryResponseHeader ? processQuery((QueryResponseHeader) responseHeader) : processUpdate((UpdateResponseHeader) responseHeader);
}

private MySQLServerPreparedStatement updateAndGetPreparedStatement() {
Expand All @@ -94,6 +93,12 @@ private MySQLServerPreparedStatement updateAndGetPreparedStatement() {
return result;
}

@Override
public Collection<DatabasePacket> execute() throws SQLException {
ResponseHeader responseHeader = proxyBackendHandler.execute();
return responseHeader instanceof QueryResponseHeader ? processQuery((QueryResponseHeader) responseHeader) : processUpdate((UpdateResponseHeader) responseHeader);
}

private Collection<DatabasePacket> processQuery(final QueryResponseHeader queryResponseHeader) {
responseType = ResponseType.QUERY;
int characterSet = connectionSession.getAttributeMap().attr(MySQLConstants.CHARACTER_SET_ATTRIBUTE_KEY).get().getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ public final class MySQLComFieldListPacketExecutor implements CommandExecutor {

private final ConnectionSession connectionSession;

private DatabaseConnector databaseConnector;
private final DatabaseConnector databaseConnector;

@Override
public Collection<DatabasePacket> execute() throws SQLException {
public MySQLComFieldListPacketExecutor(final MySQLComFieldListPacket packet, final ConnectionSession connectionSession) {
this.packet = packet;
this.connectionSession = connectionSession;
String databaseName = connectionSession.getCurrentDatabaseName();
String sql = String.format(SQL, packet.getTable(), databaseName);
MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
Expand All @@ -73,8 +74,12 @@ public Collection<DatabasePacket> execute() throws SQLException {
ProxyDatabaseConnectionManager databaseConnectionManager = connectionSession.getDatabaseConnectionManager();
QueryContext queryContext = new QueryContext(sqlStatementContext, sql, Collections.emptyList(), hintValueContext, connectionSession.getConnectionContext(), metaDataContexts.getMetaData());
databaseConnector = DatabaseConnectorFactory.getInstance().newInstance(queryContext, databaseConnectionManager, false);
}

@Override
public Collection<DatabasePacket> execute() throws SQLException {
databaseConnector.execute();
return createColumnDefinition41Packets(databaseName);
return createColumnDefinition41Packets(connectionSession.getCurrentDatabaseName());
}

private Collection<DatabasePacket> createColumnDefinition41Packets(final String databaseName) throws SQLException {
Expand Down
Loading