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 9d82cc0d482 Modify to only create transaction manager of current 
transaction type(#25674) (#25676)
9d82cc0d482 is described below

commit 9d82cc0d482861e368ce6c52b26337e2b0ee2f68
Author: ZhangCheng <[email protected]>
AuthorDate: Mon May 15 19:00:08 2023 +0800

    Modify to only create transaction manager of current transaction 
type(#25674) (#25676)
---
 .../batch/BatchPreparedStatementExecutorTest.java  |  2 +-
 .../spi/ShardingSphereTransactionManager.java      |  3 +-
 .../ShardingSphereTransactionManagerEngine.java    | 39 +++++++++-------------
 .../transaction/rule/TransactionRule.java          |  8 ++---
 ...ShardingSphereTransactionManagerEngineTest.java |  2 +-
 ...herShardingSphereTransactionManagerFixture.java |  5 +++
 .../ShardingSphereTransactionManagerFixture.java   |  5 +++
 .../ShardingSphereTransactionManagerFixture.java   |  5 +++
 .../query/ShowTransactionRuleExecutorTest.java     | 14 ++++----
 ...ransaction.spi.ShardingSphereTransactionManager | 18 ++++++++++
 .../SeataATShardingSphereTransactionManager.java   |  5 +++
 .../xa/XAShardingSphereTransactionManager.java     |  5 +++
 12 files changed, 74 insertions(+), 37 deletions(-)

diff --git 
a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutorTest.java
 
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutorTest.java
index b6f120b78be..d03aaadd371 100644
--- 
a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutorTest.java
+++ 
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutorTest.java
@@ -118,7 +118,7 @@ class BatchPreparedStatementExecutorTest {
     
     private TransactionRule mockTransactionRule() {
         TransactionRule result = mock(TransactionRule.class);
-        when(result.getResource()).thenReturn(new 
ShardingSphereTransactionManagerEngine());
+        when(result.getResource()).thenReturn(new 
ShardingSphereTransactionManagerEngine(TransactionType.LOCAL));
         return result;
     }
     
diff --git 
a/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/spi/ShardingSphereTransactionManager.java
 
b/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/spi/ShardingSphereTransactionManager.java
index db16f0c29c4..104f28830f5 100644
--- 
a/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/spi/ShardingSphereTransactionManager.java
+++ 
b/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/spi/ShardingSphereTransactionManager.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.transaction.spi;
 
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPI;
 import org.apache.shardingsphere.transaction.api.TransactionType;
 
 import javax.sql.DataSource;
@@ -28,7 +29,7 @@ import java.util.Map;
 /**
  * ShardingSphere transaction manager.
  */
-public interface ShardingSphereTransactionManager extends AutoCloseable {
+public interface ShardingSphereTransactionManager extends TypedSPI, 
AutoCloseable {
     
     /**
      * Initialize transaction manager.
diff --git 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngine.java
 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngine.java
index b1e6b6e1916..6a03d35cde2 100644
--- 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngine.java
+++ 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngine.java
@@ -20,15 +20,13 @@ package org.apache.shardingsphere.transaction;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.transaction.api.TransactionType;
 import 
org.apache.shardingsphere.transaction.exception.TransactionManagerNotExistedException;
 import 
org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager;
 
 import javax.sql.DataSource;
-import java.util.EnumMap;
 import java.util.Map;
-import java.util.Map.Entry;
 
 /**
  * ShardingSphere transaction manager engine.
@@ -36,32 +34,26 @@ import java.util.Map.Entry;
 @Slf4j
 public final class ShardingSphereTransactionManagerEngine {
     
-    private final Map<TransactionType, ShardingSphereTransactionManager> 
transactionManagers = new EnumMap<>(TransactionType.class);
+    private final TransactionType transactionType;
     
-    public ShardingSphereTransactionManagerEngine() {
-        loadTransactionManager();
-    }
+    private final ShardingSphereTransactionManager transactionManager;
     
-    private void loadTransactionManager() {
-        for (ShardingSphereTransactionManager each : 
ShardingSphereServiceLoader.getServiceInstances(ShardingSphereTransactionManager.class))
 {
-            if (transactionManagers.containsKey(each.getTransactionType())) {
-                log.warn("Find more than one {} transaction manager 
implementation class, use `{}` now",
-                        each.getTransactionType(), 
transactionManagers.get(each.getTransactionType()).getClass().getName());
-                continue;
-            }
-            transactionManagers.put(each.getTransactionType(), each);
-        }
+    public ShardingSphereTransactionManagerEngine(final TransactionType 
transactionType) {
+        this.transactionType = transactionType;
+        transactionManager = TransactionType.LOCAL == transactionType ? null : 
TypedSPILoader.getService(ShardingSphereTransactionManager.class, 
transactionType.name());
     }
     
     /**
-     * Initialize transaction managers.
+     * Initialize transaction manager.
      *
      * @param databaseTypes database types
      * @param dataSourceMap data source map
      * @param providerType transaction manager provider type
      */
     public void init(final Map<String, DatabaseType> databaseTypes, final 
Map<String, DataSource> dataSourceMap, final String providerType) {
-        transactionManagers.forEach((key, value) -> value.init(databaseTypes, 
dataSourceMap, providerType));
+        if (TransactionType.LOCAL != transactionType) {
+            transactionManager.init(databaseTypes, dataSourceMap, 
providerType);
+        }
     }
     
     /**
@@ -71,19 +63,18 @@ public final class ShardingSphereTransactionManagerEngine {
      * @return transaction manager
      */
     public ShardingSphereTransactionManager getTransactionManager(final 
TransactionType transactionType) {
-        ShardingSphereTransactionManager result = 
transactionManagers.get(transactionType);
         if (TransactionType.LOCAL != transactionType) {
-            ShardingSpherePreconditions.checkNotNull(result, () -> new 
TransactionManagerNotExistedException(transactionType));
+            ShardingSpherePreconditions.checkNotNull(transactionManager, () -> 
new TransactionManagerNotExistedException(transactionType));
         }
-        return result;
+        return transactionManager;
     }
     
     /**
-     * Close transaction managers.
+     * Close transaction manager.
      */
     public void close() {
-        for (Entry<TransactionType, ShardingSphereTransactionManager> entry : 
transactionManagers.entrySet()) {
-            entry.getValue().close();
+        if (TransactionType.LOCAL != transactionType) {
+            transactionManager.close();
         }
     }
 }
diff --git 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionRule.java
 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionRule.java
index a251583ac9b..4beb2eb5e8b 100644
--- 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionRule.java
+++ 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionRule.java
@@ -65,7 +65,7 @@ public final class TransactionRule implements GlobalRule, 
ResourceHeldRule<Shard
     
     private synchronized ShardingSphereTransactionManagerEngine 
createTransactionManagerEngine(final Map<String, ShardingSphereDatabase> 
databases) {
         if (databases.isEmpty()) {
-            return new ShardingSphereTransactionManagerEngine();
+            return new ShardingSphereTransactionManagerEngine(defaultType);
         }
         Map<String, DataSource> dataSourceMap = new 
LinkedHashMap<>(databases.size(), 1);
         Map<String, DatabaseType> databaseTypes = new 
LinkedHashMap<>(databases.size(), 1);
@@ -75,9 +75,9 @@ public final class TransactionRule implements GlobalRule, 
ResourceHeldRule<Shard
             database.getResourceMetaData().getStorageTypes().forEach((key, 
value) -> databaseTypes.put(database.getName() + "." + key, value));
         }
         if (dataSourceMap.isEmpty()) {
-            return new ShardingSphereTransactionManagerEngine();
+            return new ShardingSphereTransactionManagerEngine(defaultType);
         }
-        ShardingSphereTransactionManagerEngine result = new 
ShardingSphereTransactionManagerEngine();
+        ShardingSphereTransactionManagerEngine result = new 
ShardingSphereTransactionManagerEngine(defaultType);
         result.init(databaseTypes, dataSourceMap, providerType);
         return result;
     }
@@ -128,7 +128,7 @@ public final class TransactionRule implements GlobalRule, 
ResourceHeldRule<Shard
         ShardingSphereTransactionManagerEngine engine = resource.get();
         if (null != engine) {
             closeEngine(engine);
-            resource.set(new ShardingSphereTransactionManagerEngine());
+            resource.set(new 
ShardingSphereTransactionManagerEngine(defaultType));
         }
     }
     
diff --git 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngineTest.java
 
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngineTest.java
index 741d25f767b..0ad99c6a415 100644
--- 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngineTest.java
+++ 
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngineTest.java
@@ -32,7 +32,7 @@ import static org.mockito.Mockito.verify;
 
 class ShardingSphereTransactionManagerEngineTest {
     
-    private final ShardingSphereTransactionManagerEngine 
transactionManagerEngine = new ShardingSphereTransactionManagerEngine();
+    private final ShardingSphereTransactionManagerEngine 
transactionManagerEngine = new 
ShardingSphereTransactionManagerEngine(TransactionType.XA);
     
     @Test
     void assertGetEngine() {
diff --git 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/OtherShardingSphereTransactionManagerFixture.java
 
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/OtherShardingSphereTransactionManagerFixture.java
index a55fecca053..a0e128a2c43 100644
--- 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/OtherShardingSphereTransactionManagerFixture.java
+++ 
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/OtherShardingSphereTransactionManagerFixture.java
@@ -65,4 +65,9 @@ public final class 
OtherShardingSphereTransactionManagerFixture implements Shard
     @Override
     public void close() {
     }
+    
+    @Override
+    public String getType() {
+        return TransactionType.XA.name();
+    }
 }
diff --git 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/ShardingSphereTransactionManagerFixture.java
 
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/ShardingSphereTransactionManagerFixture.java
index 84985cf9fb1..114ecf5c148 100644
--- 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/ShardingSphereTransactionManagerFixture.java
+++ 
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/ShardingSphereTransactionManagerFixture.java
@@ -72,4 +72,9 @@ public final class ShardingSphereTransactionManagerFixture 
implements ShardingSp
     @Override
     public void close() {
     }
+    
+    @Override
+    public String getType() {
+        return TransactionType.XA.name();
+    }
 }
diff --git 
a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/fixture/ShardingSphereTransactionManagerFixture.java
 
b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/fixture/ShardingSphereTransactionManagerFixture.java
index 480507c7136..a657aa5a7b9 100644
--- 
a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/fixture/ShardingSphereTransactionManagerFixture.java
+++ 
b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/fixture/ShardingSphereTransactionManagerFixture.java
@@ -72,4 +72,9 @@ public class ShardingSphereTransactionManagerFixture 
implements ShardingSphereTr
     @Override
     public void close() {
     }
+    
+    @Override
+    public String getType() {
+        return TransactionType.XA.name();
+    }
 }
diff --git 
a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutorTest.java
 
b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutorTest.java
index cdc9613afe9..78644d321af 100644
--- 
a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutorTest.java
+++ 
b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutorTest.java
@@ -23,6 +23,7 @@ import 
org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import 
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
 import org.apache.shardingsphere.test.util.PropertiesBuilder;
 import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
+import org.apache.shardingsphere.transaction.api.TransactionType;
 import 
org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
 import 
org.apache.shardingsphere.transaction.distsql.parser.statement.queryable.ShowTransactionRuleStatement;
 import org.apache.shardingsphere.transaction.rule.TransactionRule;
@@ -44,13 +45,14 @@ class ShowTransactionRuleExecutorTest {
     @Test
     void assertExecuteWithXA() {
         ShowTransactionRuleExecutor executor = new 
ShowTransactionRuleExecutor();
-        ShardingSphereRuleMetaData ruleMetaData = mockGlobalRuleMetaData("XA", 
"Atomikos", PropertiesBuilder.build(new Property("host", "127.0.0.1"), new 
Property("databaseName", "jbossts")));
+        ShardingSphereRuleMetaData ruleMetaData = 
mockGlobalRuleMetaData(TransactionType.XA.name(), "Atomikos",
+                PropertiesBuilder.build(new Property("host", "127.0.0.1"), new 
Property("databaseName", "jbossts")));
         ShardingSphereMetaData metaData = new ShardingSphereMetaData(new 
LinkedHashMap<>(), ruleMetaData, new ConfigurationProperties(new Properties()));
         Collection<LocalDataQueryResultRow> actual = 
executor.getRows(metaData, mock(ShowTransactionRuleStatement.class));
         assertThat(actual.size(), is(1));
         Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
         LocalDataQueryResultRow row = iterator.next();
-        assertThat(row.getCell(1), is("XA"));
+        assertThat(row.getCell(1), is(TransactionType.XA.name()));
         assertThat(row.getCell(2), is("Atomikos"));
         String props = (String) row.getCell(3);
         assertTrue(props.contains("databaseName=jbossts"));
@@ -60,13 +62,13 @@ class ShowTransactionRuleExecutorTest {
     @Test
     void assertExecuteWithLocal() {
         ShowTransactionRuleExecutor executor = new 
ShowTransactionRuleExecutor();
-        ShardingSphereRuleMetaData ruleMetaData = 
mockGlobalRuleMetaData("LOCAL", null, new Properties());
+        ShardingSphereRuleMetaData ruleMetaData = 
mockGlobalRuleMetaData(TransactionType.LOCAL.name(), null, new Properties());
         ShardingSphereMetaData metaData = new ShardingSphereMetaData(new 
LinkedHashMap<>(), ruleMetaData, new ConfigurationProperties(new Properties()));
         Collection<LocalDataQueryResultRow> actual = 
executor.getRows(metaData, mock(ShowTransactionRuleStatement.class));
         assertThat(actual.size(), is(1));
         Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
         LocalDataQueryResultRow row = iterator.next();
-        assertThat(row.getCell(1), is("LOCAL"));
+        assertThat(row.getCell(1), is(TransactionType.LOCAL.name()));
         assertThat(row.getCell(2), is(""));
         assertThat(row.getCell(3), is(""));
     }
@@ -83,11 +85,11 @@ class ShowTransactionRuleExecutorTest {
     }
     
     private ShardingSphereRuleMetaData mockGlobalRuleMetaData(final String 
defaultType, final String providerType, final Properties props) {
-        TransactionRule transactionRule = new 
TransactionRule(createAuthorityRuleConfiguration(defaultType, providerType, 
props), Collections.emptyMap());
+        TransactionRule transactionRule = new 
TransactionRule(createTransactionRuleConfiguration(defaultType, providerType, 
props), Collections.emptyMap());
         return new 
ShardingSphereRuleMetaData(Collections.singleton(transactionRule));
     }
     
-    private TransactionRuleConfiguration 
createAuthorityRuleConfiguration(final String defaultType, final String 
providerType, final Properties props) {
+    private TransactionRuleConfiguration 
createTransactionRuleConfiguration(final String defaultType, final String 
providerType, final Properties props) {
         return new TransactionRuleConfiguration(defaultType, providerType, 
props);
     }
 }
diff --git 
a/kernel/transaction/distsql/handler/src/test/resources/META-INF/services/org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager
 
b/kernel/transaction/distsql/handler/src/test/resources/META-INF/services/org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager
new file mode 100644
index 00000000000..c7af21e312a
--- /dev/null
+++ 
b/kernel/transaction/distsql/handler/src/test/resources/META-INF/services/org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.transaction.distsql.handler.fixture.ShardingSphereTransactionManagerFixture
diff --git 
a/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManager.java
 
b/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManager.java
index c70175c6679..46a078f298e 100644
--- 
a/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManager.java
+++ 
b/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManager.java
@@ -148,4 +148,9 @@ public final class SeataATShardingSphereTransactionManager 
implements ShardingSp
         RmNettyRemotingClient.getInstance().destroy();
         TmNettyRemotingClient.getInstance().destroy();
     }
+    
+    @Override
+    public String getType() {
+        return TransactionType.BASE.name();
+    }
 }
diff --git 
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
 
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
index 52995b97071..a762dcab471 100644
--- 
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
+++ 
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
@@ -142,4 +142,9 @@ public final class XAShardingSphereTransactionManager 
implements ShardingSphereT
     public boolean containsProviderType(final String providerType) {
         return TypedSPILoader.contains(XATransactionManagerProvider.class, 
providerType);
     }
+    
+    @Override
+    public String getType() {
+        return TransactionType.XA.name();
+    }
 }

Reply via email to