strongduanmu commented on code in PR #20020:
URL: https://github.com/apache/shardingsphere/pull/20020#discussion_r941929259
##########
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/session/traffic/TrafficSessionContext.java:
##########
@@ -15,43 +15,35 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.traffic.context;
+package org.apache.shardingsphere.infra.session.traffic;
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
+import lombok.Getter;
+import lombok.Setter;
import java.util.Optional;
/**
- * Hold traffic context for current thread.
+ * Traffic session context.
*/
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class TrafficContextHolder {
+@Getter
+public final class TrafficSessionContext {
- private static final ThreadLocal<TrafficContext> TRAFFIC_CONTEXT = new
ThreadLocal<>();
-
- /**
- * Set traffic context.
- *
- * @param trafficContext traffic context
- */
- public static void set(final TrafficContext trafficContext) {
- TRAFFIC_CONTEXT.set(trafficContext);
- }
+ @Setter
+ private TrafficContextAware trafficContext;
/**
* Get traffic context.
- *
+ *
* @return traffic context
*/
- public static Optional<TrafficContext> get() {
- return Optional.ofNullable(TRAFFIC_CONTEXT.get());
+ public Optional<TrafficContextAware> getTrafficContext() {
+ return Optional.ofNullable(trafficContext);
}
/**
- * Remove traffic context.
+ * Clear.
*/
- public static void remove() {
- TRAFFIC_CONTEXT.remove();
+ public void clear() {
Review Comment:
Implement AutoClosable interface?
##########
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/session/SQLSession.java:
##########
@@ -15,28 +15,16 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.traffic.context;
+package org.apache.shardingsphere.infra.session;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import lombok.Getter;
+import org.apache.shardingsphere.infra.session.traffic.TrafficSessionContext;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-@RunWith(MockitoJUnitRunner.class)
-public final class TrafficContextHolderTest {
-
- @Mock
- private TrafficContext trafficContext;
+/**
+ * SQL session.
+ */
+@Getter
+public final class SQLSession {
- @Test
- public void assertTrafficContextHolder() {
- assertFalse(TrafficContextHolder.get().isPresent());
- TrafficContextHolder.set(trafficContext);
- assertTrue(TrafficContextHolder.get().isPresent());
- TrafficContextHolder.remove();
- assertFalse(TrafficContextHolder.get().isPresent());
- }
+ private final TrafficSessionContext trafficSessionContext = new
TrafficSessionContext();
Review Comment:
Do you think TrafficSQLSession is better?
##########
shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java:
##########
@@ -250,9 +249,9 @@ private JDBCExecutionUnit createTrafficExecutionUnit(final
TrafficContext traffi
}
private TrafficContext getTrafficContext(final LogicSQL logicSQL) {
- TrafficContext result = TrafficContextHolder.get().orElseGet(() ->
createTrafficContext(logicSQL));
+ TrafficContext result = (TrafficContext)
connection.getSqlSession().getTrafficSessionContext().getTrafficContext().orElseGet(()
-> createTrafficContext(logicSQL));
Review Comment:
Can we remove trafficSessionContext?
##########
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/session/traffic/TrafficSessionContext.java:
##########
@@ -15,43 +15,35 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.traffic.context;
+package org.apache.shardingsphere.infra.session.traffic;
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
+import lombok.Getter;
+import lombok.Setter;
import java.util.Optional;
/**
- * Hold traffic context for current thread.
+ * Traffic session context.
*/
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class TrafficContextHolder {
+@Getter
+public final class TrafficSessionContext {
- private static final ThreadLocal<TrafficContext> TRAFFIC_CONTEXT = new
ThreadLocal<>();
-
- /**
- * Set traffic context.
- *
- * @param trafficContext traffic context
- */
- public static void set(final TrafficContext trafficContext) {
- TRAFFIC_CONTEXT.set(trafficContext);
- }
+ @Setter
+ private TrafficContextAware trafficContext;
Review Comment:
What's the purpose of TrafficContextAware?
--
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]