jingshanglu commented on a change in pull request #15471:
URL: https://github.com/apache/shardingsphere/pull/15471#discussion_r809684130



##########
File path: 
shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/config/NarayanaConfigurationFileGenerator.java
##########
@@ -95,29 +107,92 @@ private NarayanaConfigEntry createEntry(final String key, 
final Collection<Strin
         return result;
     }
     
-    private void appendJdbcStoreConfiguration(final TransactionRule 
transactionRule, final NarayanaConfiguration config) {
+    private void appendUserDefinedJdbcStoreConfiguration(final TransactionRule 
transactionRule, final NarayanaConfiguration config) {
         String url = 
transactionRule.getProps().getProperty("recoveryStoreUrl");
         String user = 
transactionRule.getProps().getProperty("recoveryStoreUser");
         String password = 
String.valueOf(transactionRule.getProps().get("recoveryStorePassword"));
         String dataSourceClass = 
transactionRule.getProps().getProperty("recoveryStoreDataSource");
         if (null != url && null != user && null != password && null != 
dataSourceClass) {
-            String jdbcAccessPatten = 
DynamicDataSourceJDBCAccess.class.getName() + 
";ClassName=%s;URL=%s;User=%s;Password=%s";
-            String jdbcAccess = String.format(jdbcAccessPatten, 
dataSourceClass, url, user, password);
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.objectStoreType",
 JDBCStore.class.getName()));
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.jdbcAccess", 
jdbcAccess));
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.tablePrefix", 
"Action"));
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.dropTable", 
"true"));
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.stateStore.objectStoreType",
 JDBCStore.class.getName()));
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.stateStore.jdbcAccess",
 jdbcAccess));
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.stateStore.tablePrefix",
 "stateStore"));
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.stateStore.dropTable",
 "true"));
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.communicationStore.objectStoreType",
 JDBCStore.class.getName()));
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.communicationStore.jdbcAccess",
 jdbcAccess));
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.communicationStore.tablePrefix",
 "Communication"));
-            
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.communicationStore.dropTable",
 "true"));
+            appendJdbcStoreConfiguration(url, user, password, dataSourceClass, 
config);
         }
     }
     
+    private void appendJdbcStoreConfiguration(final String jdbcUrl, final 
String user, final String password, final String dataSourceClassName, final 
NarayanaConfiguration config) {
+        String jdbcAccessPatten = DynamicDataSourceJDBCAccess.class.getName() 
+ ";ClassName=%s;URL=%s;User=%s;Password=%s";
+        String jdbcAccess = String.format(jdbcAccessPatten, 
dataSourceClassName, jdbcUrl, user, password);
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.objectStoreType",
 JDBCStore.class.getName()));
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.jdbcAccess", 
jdbcAccess));
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.tablePrefix", 
"Action"));
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.dropTable", 
"true"));
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.stateStore.objectStoreType",
 JDBCStore.class.getName()));
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.stateStore.jdbcAccess",
 jdbcAccess));
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.stateStore.tablePrefix",
 "stateStore"));
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.stateStore.dropTable",
 "true"));
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.communicationStore.objectStoreType",
 JDBCStore.class.getName()));
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.communicationStore.jdbcAccess",
 jdbcAccess));
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.communicationStore.tablePrefix",
 "Communication"));
+        
config.getEntries().add(createEntry("ObjectStoreEnvironmentBean.communicationStore.dropTable",
 "true"));
+    }
+    
+    @Override
+    public Properties getTransactionProps(final TransactionRuleConfiguration 
transactionRuleConfiguration, final SchemaConfiguration schemaConfiguration) {
+        Properties result = new Properties();
+        if (!transactionRuleConfiguration.getProps().isEmpty()) {
+            getUserDefinedJdbcStoreConfiguration(transactionRuleConfiguration, 
result);
+        } else {
+            getDefaultJdbcStoreConfiguration(schemaConfiguration, result);
+        }
+        return result;
+    }
+    
+    private void getUserDefinedJdbcStoreConfiguration(final 
TransactionRuleConfiguration transactionRuleConfiguration, final Properties 
props) {
+        String url = 
transactionRuleConfiguration.getProps().getProperty("recoveryStoreUrl");
+        String user = 
transactionRuleConfiguration.getProps().getProperty("recoveryStoreUser");
+        String password = 
String.valueOf(transactionRuleConfiguration.getProps().get("recoveryStorePassword"));
+        String dataSourceClass = 
transactionRuleConfiguration.getProps().getProperty("recoveryStoreDataSource");
+        generateTransactionProps(url, user, password, dataSourceClass, props);
+    }
+    
+    private void getDefaultJdbcStoreConfiguration(final SchemaConfiguration 
schemaConfiguration, final Properties props) {
+        Map<String, DataSource> datasourceMap = 
schemaConfiguration.getDataSources();
+        Optional<DataSource> dataSource = 
datasourceMap.values().stream().findFirst();
+        if (dataSource.isPresent()) {
+            HikariDataSource hikariDataSource = (HikariDataSource) 
dataSource.get();
+            int endIndex = hikariDataSource.getJdbcUrl().indexOf("?");
+            String jdbcUrl = hikariDataSource.getJdbcUrl().substring(0, 
endIndex);
+            String user = hikariDataSource.getUsername();
+            String password = hikariDataSource.getPassword();
+            String dataSourceClassName = 
getDataSourceClassNameByJdbcUrl(jdbcUrl);
+            generateTransactionProps(jdbcUrl, user, password, 
dataSourceClassName, props);
+        }
+    }
+    
+    private String getDataSourceClassNameByJdbcUrl(final String jdbcUrl) {
+        DatabaseType type = DatabaseTypeRegistry.getDatabaseTypeByURL(jdbcUrl);
+        return getDataSourceClassNameByDatabaseType(type);
+    }
+    
+    private String getDataSourceClassNameByDatabaseType(final DatabaseType 
databaseType) {
+        switch (databaseType.getName()) {
+            case "MySQL":
+                return "com.mysql.jdbc.jdbc2.optional.MysqlDataSource";
+            case "PostgreSQL":
+                return "org.postgresql.ds.PGSimpleDataSource";
+            case "openGauss":
+                return "org.opengauss.ds.PGSimpleDataSource";
+            default:
+                throw new UnsupportedOperationException(String.format("Cannot 
support database type: `%s` as narayana recovery store", databaseType));

Review comment:
       ok
   




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