This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 022be4c  fix traffic transaction when connection.setAutoCommit(false) 
not in current thread (#15761)
022be4c is described below

commit 022be4cc6aa8b9e6644be9e4115a95a34430b2f5
Author: tuichenchuxin <[email protected]>
AuthorDate: Thu Mar 3 10:20:38 2022 +0800

    fix traffic transaction when connection.setAutoCommit(false) not in current 
thread (#15761)
    
    * fix traffic transaction
    
    * Update 
shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
    
    replace orElse to orElesGet
    
    Co-authored-by: 吴伟杰 <[email protected]>
    
    * Update 
shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
    
    replace orElse to orElseGet
    
    Co-authored-by: 吴伟杰 <[email protected]>
    
    Co-authored-by: 吴伟杰 <[email protected]>
---
 .../core/statement/ShardingSpherePreparedStatement.java  |  5 ++---
 .../jdbc/core/statement/ShardingSphereStatement.java     |  5 ++---
 .../shardingsphere/traffic/engine/TrafficEngine.java     |  5 +++--
 .../apache/shardingsphere/traffic/rule/TrafficRule.java  | 14 +++++++-------
 .../traffic/algorithm/engine/TrafficEngineTest.java      | 16 ++++++++--------
 .../shardingsphere/traffic/rule/TrafficRuleTest.java     |  9 +++------
 6 files changed, 25 insertions(+), 29 deletions(-)

diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
index c7e501a..0d68894 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
@@ -81,7 +81,6 @@ import 
org.apache.shardingsphere.traffic.context.TrafficContext;
 import org.apache.shardingsphere.traffic.context.TrafficContextHolder;
 import org.apache.shardingsphere.traffic.engine.TrafficEngine;
 import org.apache.shardingsphere.traffic.rule.TrafficRule;
-import org.apache.shardingsphere.transaction.TransactionHolder;
 
 import java.sql.Connection;
 import java.sql.ParameterMetaData;
@@ -225,7 +224,7 @@ public final class ShardingSpherePreparedStatement extends 
AbstractPreparedState
     
     private TrafficContext getTrafficContext(final LogicSQL logicSQL) {
         TrafficContext result = TrafficContextHolder.get().orElseGet(() -> 
createTrafficContext(logicSQL));
-        if (TransactionHolder.isTransaction()) {
+        if (connection.isHoldTransaction()) {
             TrafficContextHolder.set(result);
         }
         return result;
@@ -233,7 +232,7 @@ public final class ShardingSpherePreparedStatement extends 
AbstractPreparedState
     
     private TrafficContext createTrafficContext(final LogicSQL logicSQL) {
         Optional<TrafficRule> trafficRule = 
metaDataContexts.getGlobalRuleMetaData().findSingleRule(TrafficRule.class);
-        return trafficRule.map(optional -> new TrafficEngine(optional, 
metaDataContexts).dispatch(logicSQL)).orElse(new TrafficContext());
+        return trafficRule.map(optional -> new TrafficEngine(optional, 
metaDataContexts).dispatch(logicSQL, 
connection.isHoldTransaction())).orElseGet(TrafficContext::new);
     }
     
     private void resetParameters() throws SQLException {
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
index fa3ac45..b2e6714 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
@@ -76,7 +76,6 @@ import 
org.apache.shardingsphere.traffic.context.TrafficContext;
 import org.apache.shardingsphere.traffic.context.TrafficContextHolder;
 import org.apache.shardingsphere.traffic.engine.TrafficEngine;
 import org.apache.shardingsphere.traffic.rule.TrafficRule;
-import org.apache.shardingsphere.transaction.TransactionHolder;
 
 import java.sql.Connection;
 import java.sql.ResultSet;
@@ -166,7 +165,7 @@ public final class ShardingSphereStatement extends 
AbstractStatementAdapter {
     
     private TrafficContext getTrafficContext(final LogicSQL logicSQL) {
         TrafficContext result = TrafficContextHolder.get().orElseGet(() -> 
createTrafficContext(logicSQL));
-        if (TransactionHolder.isTransaction()) {
+        if (connection.isHoldTransaction()) {
             TrafficContextHolder.set(result);
         }
         return result;
@@ -174,7 +173,7 @@ public final class ShardingSphereStatement extends 
AbstractStatementAdapter {
     
     private TrafficContext createTrafficContext(final LogicSQL logicSQL) {
         Optional<TrafficRule> trafficRule = 
metaDataContexts.getGlobalRuleMetaData().findSingleRule(TrafficRule.class);
-        return trafficRule.map(optional -> new TrafficEngine(optional, 
metaDataContexts).dispatch(logicSQL)).orElse(new TrafficContext());
+        return trafficRule.map(optional -> new TrafficEngine(optional, 
metaDataContexts).dispatch(logicSQL, 
connection.isHoldTransaction())).orElseGet(TrafficContext::new);
     }
     
     private List<ResultSet> getShardingSphereResultSets() {
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/engine/TrafficEngine.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/engine/TrafficEngine.java
index c8add14..746322a 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/engine/TrafficEngine.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/engine/TrafficEngine.java
@@ -46,10 +46,11 @@ public final class TrafficEngine {
      * Dispatch.
      *
      * @param logicSQL logic SQL
+     * @param inTransaction is in transaction
      * @return traffic context
      */
-    public TrafficContext dispatch(final LogicSQL logicSQL) {
-        Optional<TrafficStrategyRule> strategyRule = 
trafficRule.findMatchedStrategyRule(logicSQL);
+    public TrafficContext dispatch(final LogicSQL logicSQL, final boolean 
inTransaction) {
+        Optional<TrafficStrategyRule> strategyRule = 
trafficRule.findMatchedStrategyRule(logicSQL, inTransaction);
         TrafficContext result = new TrafficContext();
         if (!strategyRule.isPresent() || 
isInvalidStrategyRule(strategyRule.get())) {
             return result;
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
index 0dcfb49..bf703f7 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
@@ -37,7 +37,6 @@ import 
org.apache.shardingsphere.traffic.api.traffic.transaction.TransactionTraf
 import 
org.apache.shardingsphere.traffic.api.traffic.transaction.TransactionTrafficValue;
 import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
 import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
-import org.apache.shardingsphere.transaction.TransactionHolder;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -119,11 +118,12 @@ public final class TrafficRule implements GlobalRule {
      * Find matched strategy rule.
      * 
      * @param logicSQL logic SQL
+     * @param inTransaction is in transaction
      * @return matched strategy rule
      */
-    public Optional<TrafficStrategyRule> findMatchedStrategyRule(final 
LogicSQL logicSQL) {
+    public Optional<TrafficStrategyRule> findMatchedStrategyRule(final 
LogicSQL logicSQL, final boolean inTransaction) {
         for (TrafficStrategyRule each : strategyRules) {
-            if (match(each.getTrafficAlgorithm(), logicSQL)) {
+            if (match(each.getTrafficAlgorithm(), logicSQL, inTransaction)) {
                 return Optional.of(each);
             }
         }
@@ -137,9 +137,9 @@ public final class TrafficRule implements GlobalRule {
     }
     
     @SuppressWarnings("unchecked")
-    private boolean match(final TrafficAlgorithm trafficAlgorithm, final 
LogicSQL logicSQL) {
+    private boolean match(final TrafficAlgorithm trafficAlgorithm, final 
LogicSQL logicSQL, final boolean inTransaction) {
         if (trafficAlgorithm instanceof TransactionTrafficAlgorithm) {
-            return matchTransactionTraffic((TransactionTrafficAlgorithm) 
trafficAlgorithm);
+            return matchTransactionTraffic((TransactionTrafficAlgorithm) 
trafficAlgorithm, inTransaction);
         }
         SQLStatement sqlStatement = 
logicSQL.getSqlStatementContext().getSqlStatement();
         if (trafficAlgorithm instanceof HintTrafficAlgorithm) {
@@ -165,8 +165,8 @@ public final class TrafficRule implements GlobalRule {
         return trafficAlgorithm.match(segmentTrafficValue);
     }
     
-    private boolean matchTransactionTraffic(final TransactionTrafficAlgorithm 
trafficAlgorithm) {
-        TransactionTrafficValue transactionTrafficValue = new 
TransactionTrafficValue(TransactionHolder.isTransaction());
+    private boolean matchTransactionTraffic(final TransactionTrafficAlgorithm 
trafficAlgorithm, final boolean inTransaction) {
+        TransactionTrafficValue transactionTrafficValue = new 
TransactionTrafficValue(inTransaction);
         return trafficAlgorithm.match(transactionTrafficValue);
     }
     
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/engine/TrafficEngineTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/engine/TrafficEngineTest.java
index 7c51a46..a8fdb47 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/engine/TrafficEngineTest.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/engine/TrafficEngineTest.java
@@ -73,8 +73,8 @@ public final class TrafficEngineTest {
     @Test
     public void assertDispatchWhenNotExistTrafficStrategyRule() {
         TrafficEngine trafficEngine = new TrafficEngine(trafficRule, 
metaDataContexts);
-        
when(trafficRule.findMatchedStrategyRule(logicSQL)).thenReturn(Optional.empty());
-        TrafficContext actual = trafficEngine.dispatch(logicSQL);
+        when(trafficRule.findMatchedStrategyRule(logicSQL, 
false)).thenReturn(Optional.empty());
+        TrafficContext actual = trafficEngine.dispatch(logicSQL, false);
         assertNull(actual.getInstanceId());
     }
     
@@ -83,24 +83,24 @@ public final class TrafficEngineTest {
         TrafficEngine trafficEngine = new TrafficEngine(trafficRule, 
metaDataContexts);
         TrafficStrategyRule strategyRule = mock(TrafficStrategyRule.class);
         when(strategyRule.getLabels()).thenReturn(Collections.emptyList());
-        
when(trafficRule.findMatchedStrategyRule(logicSQL)).thenReturn(Optional.of(strategyRule));
-        TrafficContext actual = trafficEngine.dispatch(logicSQL);
+        when(trafficRule.findMatchedStrategyRule(logicSQL, 
false)).thenReturn(Optional.of(strategyRule));
+        TrafficContext actual = trafficEngine.dispatch(logicSQL, false);
         assertNull(actual.getInstanceId());
     }
     
     @Test
     public void 
assertDispatchWhenExistTrafficStrategyRuleNotExistComputeNodeInstances() {
         TrafficEngine trafficEngine = new TrafficEngine(trafficRule, 
metaDataContexts);
-        
when(trafficRule.findMatchedStrategyRule(logicSQL)).thenReturn(Optional.of(strategyRule));
+        when(trafficRule.findMatchedStrategyRule(logicSQL, 
false)).thenReturn(Optional.of(strategyRule));
         when(strategyRule.getLabels()).thenReturn(Arrays.asList("OLTP", 
"OLAP"));
-        TrafficContext actual = trafficEngine.dispatch(logicSQL);
+        TrafficContext actual = trafficEngine.dispatch(logicSQL, false);
         assertNull(actual.getInstanceId());
     }
     
     @Test
     public void 
assertDispatchWhenExistTrafficStrategyRuleExistComputeNodeInstances() {
         TrafficEngine trafficEngine = new TrafficEngine(trafficRule, 
metaDataContexts);
-        
when(trafficRule.findMatchedStrategyRule(logicSQL)).thenReturn(Optional.of(strategyRule));
+        when(trafficRule.findMatchedStrategyRule(logicSQL, 
false)).thenReturn(Optional.of(strategyRule));
         when(strategyRule.getLabels()).thenReturn(Arrays.asList("OLTP", 
"OLAP"));
         TrafficLoadBalanceAlgorithm loadBalancer = 
mock(TrafficLoadBalanceAlgorithm.class);
         when(loadBalancer.getInstanceId("traffic", 
Arrays.asList("127.0.0.1@3307", 
"127.0.0.1@3308"))).thenReturn("127.0.0.1@3307");
@@ -108,7 +108,7 @@ public final class TrafficEngineTest {
         when(strategyRule.getName()).thenReturn("traffic");
         
when(metaDataContexts.getMetaDataPersistService()).thenReturn(Optional.of(metaDataPersistService));
         
when(metaDataPersistService.getComputeNodePersistService().loadComputeNodeInstances(InstanceType.PROXY,
 Arrays.asList("OLTP", "OLAP"))).thenReturn(mockComputeNodeInstances());
-        TrafficContext actual = trafficEngine.dispatch(logicSQL);
+        TrafficContext actual = trafficEngine.dispatch(logicSQL, false);
         assertThat(actual.getInstanceId(), is("127.0.0.1@3307"));
     }
     
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
index 6b81807..e9f8f33 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
@@ -31,7 +31,6 @@ import 
org.apache.shardingsphere.traffic.algorithm.traffic.hint.SQLHintTrafficAl
 import 
org.apache.shardingsphere.traffic.algorithm.traffic.transaction.ProxyTrafficAlgorithm;
 import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
 import 
org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration;
-import org.apache.shardingsphere.transaction.TransactionHolder;
 import org.junit.Test;
 
 import java.util.Arrays;
@@ -61,7 +60,7 @@ public final class TrafficRuleTest {
     @Test
     public void assertFindMatchedStrategyRuleWhenSQLHintMatch() {
         TrafficRule trafficRule = new TrafficRule(createTrafficRuleConfig());
-        Optional<TrafficStrategyRule> actual = 
trafficRule.findMatchedStrategyRule(createLogicSQL(true));
+        Optional<TrafficStrategyRule> actual = 
trafficRule.findMatchedStrategyRule(createLogicSQL(true), false);
         assertTrue(actual.isPresent());
         assertThat(actual.get().getName(), is("sql_hint_traffic"));
         assertThat(actual.get().getLabels(), is(Arrays.asList("OLTP", 
"OLAP")));
@@ -72,21 +71,19 @@ public final class TrafficRuleTest {
     @Test
     public void assertFindMatchedStrategyRuleWhenSQLHintNotMatch() {
         TrafficRule trafficRule = new TrafficRule(createTrafficRuleConfig());
-        Optional<TrafficStrategyRule> actual = 
trafficRule.findMatchedStrategyRule(createLogicSQL(false));
+        Optional<TrafficStrategyRule> actual = 
trafficRule.findMatchedStrategyRule(createLogicSQL(false), false);
         assertFalse(actual.isPresent());
     }
     
     @Test
     public void assertFindMatchedStrategyRuleWhenInTransaction() {
-        TransactionHolder.setInTransaction();
         TrafficRule trafficRule = new TrafficRule(createTrafficRuleConfig());
-        Optional<TrafficStrategyRule> actual = 
trafficRule.findMatchedStrategyRule(createLogicSQL(false));
+        Optional<TrafficStrategyRule> actual = 
trafficRule.findMatchedStrategyRule(createLogicSQL(false), true);
         assertTrue(actual.isPresent());
         assertThat(actual.get().getName(), is("transaction_traffic"));
         assertThat(actual.get().getLabels(), 
is(Collections.singletonList("OLAP")));
         assertThat(actual.get().getTrafficAlgorithm(), 
instanceOf(ProxyTrafficAlgorithm.class));
         assertThat(actual.get().getLoadBalancer(), 
instanceOf(RandomTrafficLoadBalanceAlgorithm.class));
-        TransactionHolder.clear();
     }
     
     @Test

Reply via email to