FlyingZC commented on code in PR #18911:
URL: https://github.com/apache/shardingsphere/pull/18911#discussion_r916502311


##########
shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionRule.java:
##########
@@ -50,30 +51,35 @@ public final class TransactionRule implements GlobalRule, 
ResourceHeldRule<Shard
     
     private final Map<String, ShardingSphereDatabase> databases;
     
-    private final Map<String, ShardingSphereTransactionManagerEngine> 
resources;
+    private volatile ShardingSphereTransactionManagerEngine 
transactionManagerEngine;
     
     public TransactionRule(final TransactionRuleConfiguration ruleConfig, 
final Map<String, ShardingSphereDatabase> databases, final InstanceContext 
instanceContext) {
         configuration = ruleConfig;
         defaultType = 
TransactionType.valueOf(ruleConfig.getDefaultType().toUpperCase());
         providerType = ruleConfig.getProviderType();
         props = ruleConfig.getProps();
         this.databases = databases;
-        resources = createTransactionManagerEngines(databases, 
instanceContext);
+        transactionManagerEngine = createTransactionManagerEngine(databases);
     }
     
-    private Map<String, ShardingSphereTransactionManagerEngine> 
createTransactionManagerEngines(final Map<String, ShardingSphereDatabase> 
databases, final InstanceContext instanceContext) {
-        Map<String, ShardingSphereTransactionManagerEngine> result = new 
HashMap<>(databases.keySet().size(), 1);
+    private synchronized ShardingSphereTransactionManagerEngine 
createTransactionManagerEngine(final Map<String, ShardingSphereDatabase> 
databases) {
+        if (databases.size() == 0) {
+            return new ShardingSphereTransactionManagerEngine();
+        }
+        ShardingSphereTransactionManagerEngine result = new 
ShardingSphereTransactionManagerEngine();
+        Map<String, DataSource> dataSourceMap = new 
HashMap<>(databases.size());
+        DatabaseType databaseType = null;
         for (Entry<String, ShardingSphereDatabase> entry : 
databases.entrySet()) {
-            result.put(entry.getKey(), 
createTransactionManagerEngine(entry.getValue()));
+            
dataSourceMap.putAll(entry.getValue().getResource().getDataSources());
+            databaseType = entry.getValue().getProtocolType();
         }
+        result.init(databaseType, dataSourceMap, providerType);
         return result;
     }
     
-    private ShardingSphereTransactionManagerEngine 
createTransactionManagerEngine(final ShardingSphereDatabase database) {
-        ShardingSphereTransactionManagerEngine result = new 
ShardingSphereTransactionManagerEngine();
-        ShardingSphereResource resource = database.getResource();
-        result.init(resource.getDatabaseType(), resource.getDataSources(), 
providerType);
-        return result;
+    @Override
+    public ShardingSphereTransactionManagerEngine getResource() {
+        return transactionManagerEngine;

Review Comment:
   The variable name `transactionManagerEngine` is inconsistent with the 
`getResource()` method name, maybe it can't use `@Getter`.



-- 
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]

Reply via email to