strongduanmu commented on code in PR #20020:
URL: https://github.com/apache/shardingsphere/pull/20020#discussion_r942092711
##########
shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java:
##########
@@ -185,19 +184,22 @@ private static SQLFederationDeciderContext decide(final
LogicSQL logicSQL, final
return deciderEngine.decide(logicSQL, database);
}
- private TrafficContext getTrafficContext(final LogicSQL logicSQL) {
- TrafficContext result = TrafficContextHolder.get().orElseGet(() ->
createTrafficContext(logicSQL));
- if (connection.isHoldTransaction()) {
- TrafficContextHolder.set(result);
+ private Optional<String> getInstanceIdAndSave(final LogicSQL logicSQL) {
+ Optional<String> trafficInstanceId =
connection.getSqlSession().getTrafficInstanceId();
+ if (!trafficInstanceId.isPresent()) {
+ trafficInstanceId = getTrafficInstanceId(logicSQL);
}
- return result;
+ if (connection.isHoldTransaction() && trafficInstanceId.isPresent()) {
+
connection.getSqlSession().setTrafficInstanceId(trafficInstanceId.get());
+ }
+ return trafficInstanceId;
Review Comment:
Please rename trafficInstanceId to result.
##########
shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java:
##########
@@ -211,9 +210,9 @@ public ResultSet executeQuery() throws SQLException {
}
clearPrevious();
LogicSQL logicSQL = createLogicSQL();
- trafficContext = getTrafficContext(logicSQL);
- if (trafficContext.isMatchTraffic()) {
- JDBCExecutionUnit executionUnit =
createTrafficExecutionUnit(trafficContext, logicSQL);
+ trafficInstanceId =
getTrafficInstanceIdAndSave(logicSQL).orElse(null);
Review Comment:
Can we return Optional here?
##########
shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java:
##########
@@ -157,9 +156,9 @@ public ResultSet executeQuery(final String sql) throws
SQLException {
ResultSet result;
try {
LogicSQL logicSQL = createLogicSQL(sql);
- trafficContext = getTrafficContext(logicSQL);
- if (trafficContext.isMatchTraffic()) {
- JDBCExecutionUnit executionUnit =
createTrafficExecutionUnit(trafficContext, logicSQL);
+ trafficInstanceId = getInstanceIdAndSave(logicSQL).orElse(null);
Review Comment:
getInstanceIdAndSet may be better.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]