diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java index 5f93209c5ba4b..1063cd329bade 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java @@ -159,10 +159,6 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState private ResultSet currentResultSet; - private String trafficInstanceId; - - private boolean useFederation; - private final HintValueContext hintValueContext; private ResultSet currentBatchGeneratedKeysResultSet; @@ -224,7 +220,6 @@ private boolean isStatementsCacheable(final RuleMetaData databaseRuleMetaData) { @Override public ResultSet executeQuery() throws SQLException { - ResultSet result; try { if (statementsCacheable && !statements.isEmpty()) { resetParameters(); @@ -233,18 +228,19 @@ public ResultSet executeQuery() throws SQLException { clearPrevious(); QueryContext queryContext = createQueryContext(); handleAutoCommit(queryContext); - trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null); + String trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null); if (null != trafficInstanceId) { JDBCExecutionUnit executionUnit = createTrafficExecutionUnit(trafficInstanceId, queryContext); - return executor.getTrafficExecutor().execute(executionUnit, (statement, sql) -> ((PreparedStatement) statement).executeQuery()); + currentResultSet = executor.getTrafficExecutor().execute(executionUnit, (statement, sql) -> ((PreparedStatement) statement).executeQuery()); + return currentResultSet; } - useFederation = decide(queryContext, - metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData()); - if (useFederation) { - return executeFederationQuery(queryContext); + if (decide(queryContext, metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData())) { + currentResultSet = executeFederationQuery(queryContext); + return currentResultSet; } executionContext = createExecutionContext(queryContext); - result = doExecuteQuery(executionContext); + currentResultSet = doExecuteQuery(executionContext); + return currentResultSet; // CHECKSTYLE:OFF } catch (final RuntimeException ex) { // CHECKSTYLE:ON @@ -253,8 +249,6 @@ public ResultSet executeQuery() throws SQLException { } finally { clearBatch(); } - currentResultSet = result; - return result; } private ShardingSphereResultSet doExecuteQuery(final ExecutionContext executionContext) throws SQLException { @@ -347,7 +341,7 @@ public int executeUpdate() throws SQLException { clearPrevious(); QueryContext queryContext = createQueryContext(); handleAutoCommit(queryContext); - trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null); + String trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null); if (null != trafficInstanceId) { JDBCExecutionUnit executionUnit = createTrafficExecutionUnit(trafficInstanceId, queryContext); return executor.getTrafficExecutor().execute(executionUnit, (statement, sql) -> ((PreparedStatement) statement).executeUpdate()); @@ -411,15 +405,16 @@ public boolean execute() throws SQLException { clearPrevious(); QueryContext queryContext = createQueryContext(); handleAutoCommit(queryContext); - trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null); + String trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null); if (null != trafficInstanceId) { JDBCExecutionUnit executionUnit = createTrafficExecutionUnit(trafficInstanceId, queryContext); - return executor.getTrafficExecutor().execute(executionUnit, (statement, sql) -> ((PreparedStatement) statement).execute()); + boolean result = executor.getTrafficExecutor().execute(executionUnit, (statement, sql) -> ((PreparedStatement) statement).execute()); + currentResultSet = executor.getTrafficExecutor().getResultSet(); + return result; } - useFederation = decide(queryContext, - metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData()); - if (useFederation) { + if (decide(queryContext, metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData())) { ResultSet resultSet = executeFederationQuery(queryContext); + currentResultSet = resultSet; return null != resultSet; } executionContext = createExecutionContext(queryContext); @@ -496,14 +491,7 @@ public ResultSet getResultSet() throws SQLException { if (null != currentResultSet) { return currentResultSet; } - if (null != trafficInstanceId) { - return executor.getTrafficExecutor().getResultSet(); - } - if (useFederation) { - return executor.getSqlFederationEngine().getResultSet(); - } - if (executionContext.getSqlStatementContext() instanceof SelectStatementContext - || executionContext.getSqlStatementContext().getSqlStatement() instanceof DALStatement) { + if (executionContext.getSqlStatementContext() instanceof SelectStatementContext || executionContext.getSqlStatementContext().getSqlStatement() instanceof DALStatement) { List resultSets = getResultSets(); if (resultSets.isEmpty()) { return currentResultSet; @@ -629,7 +617,7 @@ private String getGeneratedKeysColumnName(final String columnName) { public void addBatch() { try { QueryContext queryContext = createQueryContext(); - trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null); + String trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null); executionContext = null == trafficInstanceId ? createExecutionContext(queryContext) : createExecutionContext(queryContext, trafficInstanceId); batchPreparedStatementExecutor.addBatchForExecutionUnits(executionContext.getExecutionUnits()); } finally {