terrymanu commented on a change in pull request #1825: for #1812 make 
ShardingTransactionManager support multi instance.
URL: 
https://github.com/apache/incubator-shardingsphere/pull/1825#discussion_r251205494
 
 

 ##########
 File path: 
sharding-transaction/sharding-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/ShardingTransactionManagerAdapter.java
 ##########
 @@ -0,0 +1,71 @@
+/*
+ * 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.
+ */
+
+package org.apache.shardingsphere.transaction.core;
+
+import org.apache.shardingsphere.core.constant.DatabaseType;
+import org.apache.shardingsphere.transaction.spi.ShardingTransactionManager;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Sharding transaction manager adapter.
+ *
+ * @author zhaojun
+ */
+public abstract class ShardingTransactionManagerAdapter implements 
ShardingTransactionManager {
+    
+    private final String uniqueKey = 
ResourceIDGenerator.getInstance().nextId();
+    
+    @Override
+    public final void init(final DatabaseType databaseType, final Map<String, 
DataSource> dataSourceMap) {
+        doInit(databaseType, convertUniqueKeyDataSourceMap(dataSourceMap));
+    }
+    
+    /**
+     * Do init.
+     *
+     * @param databaseType database type
+     * @param dataSourceMap data source map
+     */
+    public abstract void doInit(DatabaseType databaseType, Map<String, 
DataSource> dataSourceMap);
+    
+    private Map<String, DataSource> convertUniqueKeyDataSourceMap(final 
Map<String, DataSource> dataSourceMap) {
+        Map<String, DataSource> result = new HashMap<>(dataSourceMap.size(), 
1);
+        for (Map.Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
+            result.put(uniqueKey + entry.getKey(), entry.getValue());
+        }
+        return result;
+    }
+    
+    @Override
+    public final Connection getConnection(final String dataSourceName) {
+        return doGetConnection(uniqueKey + dataSourceName);
+    }
+    
+    /**
+     * Do get connection.
+     *
+     * @param dataSourceName data source name
+     * @return connection
+     */
+    public abstract Connection doGetConnection(String dataSourceName);
 
 Review comment:
   create new class named ResourceDataSource to deal with resource name, maybe 
is better than use `getConnection` and `doGetConnection`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to