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

jianbin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git


The following commit(s) were added to refs/heads/2.x by this push:
     new ca44cfed9e refactor: Refactor DataSourceProxy (#7615)
ca44cfed9e is described below

commit ca44cfed9ed1a19c2fbb83842793c866dc38493c
Author: jimin <[email protected]>
AuthorDate: Thu Oct 16 13:35:48 2025 +0800

    refactor: Refactor DataSourceProxy (#7615)
---
 changes/en-us/2.x.md                               |   1 +
 changes/zh-cn/2.x.md                               |   1 +
 .../seata/rm/datasource/DataSourceProxy.java       | 192 ++-------------------
 .../initializer/AbstractResourceIdInitializer.java |  29 ++++
 .../initializer/ResourceIdInitializer.java         |  28 +++
 .../initializer/ResourceIdInitializerRegistry.java |  52 ++++++
 .../initializer/db/DMResourceIdInitializer.java    |  59 +++++++
 .../db/DefaultResourceIdInitializer.java           |  38 ++++
 .../initializer/db/MysqlResourceIdInitializer.java |  55 ++++++
 .../db/OracleResourceIdInitializer.java            |  39 +++++
 .../initializer/db/OscarResourceIdInitializer.java |  39 +++++
 .../db/PostgresqlResourceIdInitializer.java        |  68 ++++++++
 .../db/SqlServerResourceIdInitializer.java         |  76 ++++++++
 13 files changed, 501 insertions(+), 176 deletions(-)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 7e5811a8eb..732c64e8d9 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -84,6 +84,7 @@ Add changes here for all PR submitted to the 2.x branch.
 
 ### refactor:
 
+- [[#7615](https://github.com/seata/seata/pull/7615)] Refactor DataSourceProxy
 - [[#7617](https://github.com/seata/seata/pull/7617)] Refactor Alibaba Dubbo 
and HSF
 
 
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 6bf12ba6de..6ba6657a71 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -83,6 +83,7 @@
 
 ### refactor:
 
+- [[#7615](https://github.com/seata/seata/pull/7615)] 重构 DataSourceProxy
 - [[#7617](https://github.com/seata/seata/pull/7617)] 重构 Alibaba Dubbo 和 HSF 模块
 
 
diff --git 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/DataSourceProxy.java
 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/DataSourceProxy.java
index fc0ef6aec7..fef0d212c3 100644
--- 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/DataSourceProxy.java
+++ 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/DataSourceProxy.java
@@ -18,13 +18,14 @@ package org.apache.seata.rm.datasource;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.seata.common.ConfigurationKeys;
-import org.apache.seata.common.Constants;
 import org.apache.seata.common.loader.EnhancedServiceNotFoundException;
 import org.apache.seata.config.ConfigurationFactory;
 import org.apache.seata.core.context.RootContext;
 import org.apache.seata.core.model.BranchType;
 import org.apache.seata.core.model.Resource;
 import org.apache.seata.rm.DefaultResourceManager;
+import org.apache.seata.rm.datasource.initializer.ResourceIdInitializer;
+import 
org.apache.seata.rm.datasource.initializer.ResourceIdInitializerRegistry;
 import org.apache.seata.rm.datasource.sql.struct.TableMetaCacheFactory;
 import org.apache.seata.rm.datasource.undo.UndoLogManager;
 import org.apache.seata.rm.datasource.undo.UndoLogManagerFactory;
@@ -45,7 +46,6 @@ import static 
org.apache.seata.common.DefaultValues.DEFAULT_TRANSACTION_UNDO_LOG
 
 /**
  * The type Data source proxy.
- *
  */
 public class DataSourceProxy extends AbstractDataSourceProxy implements 
Resource {
 
@@ -131,7 +131,6 @@ public class DataSourceProxy extends 
AbstractDataSourceProxy implements Resource
 
     /**
      * Define derivative product version for MySQL Kernel
-     *
      */
     private void checkDerivativeProduct() {
         if (!JdbcConstants.MYSQL.equals(dbType)) {
@@ -234,179 +233,8 @@ public class DataSourceProxy extends 
AbstractDataSourceProxy implements Resource
     }
 
     private void initResourceId() {
-        if (JdbcConstants.POSTGRESQL.equals(dbType)) {
-            initPGResourceId();
-        } else if (JdbcConstants.ORACLE.equals(dbType) && userName != null) {
-            initOracleResourceId();
-        } else if (JdbcConstants.MYSQL.equals(dbType) || 
JdbcConstants.POLARDBX.equals(dbType)) {
-            initMysqlResourceId();
-        } else if (JdbcConstants.SQLSERVER.equals(dbType)) {
-            initSqlServerResourceId();
-        } else if (JdbcConstants.DM.equals(dbType)) {
-            initDMResourceId();
-        } else if (JdbcConstants.OSCAR.equals(dbType)) {
-            initOscarResourceId();
-        } else {
-            initDefaultResourceId();
-        }
-    }
-
-    /**
-     * init the default resource id
-     */
-    private void initDefaultResourceId() {
-        if (jdbcUrl.contains("?")) {
-            resourceId = jdbcUrl.substring(0, jdbcUrl.indexOf('?'));
-        } else {
-            resourceId = jdbcUrl;
-        }
-    }
-
-    /**
-     * init the oracle resource id
-     */
-    private void initOracleResourceId() {
-        if (jdbcUrl.contains("?")) {
-            resourceId = jdbcUrl.substring(0, jdbcUrl.indexOf('?')) + "/" + 
userName;
-        } else {
-            resourceId = jdbcUrl + "/" + userName;
-        }
-    }
-
-    /**
-     * prevent mysql url like
-     * jdbc:mysql:loadbalance://192.168.100.2:3306,192.168.100.1:3306/seata
-     * it will cause the problem like
-     * 1.rm client is not connected
-     */
-    private void initMysqlResourceId() {
-        String startsWith = "jdbc:mysql:loadbalance://";
-        if (jdbcUrl.startsWith(startsWith)) {
-            String url;
-            if (jdbcUrl.contains("?")) {
-                url = jdbcUrl.substring(0, jdbcUrl.indexOf('?'));
-            } else {
-                url = jdbcUrl;
-            }
-            resourceId = url.replace(",", "|");
-        } else {
-            initDefaultResourceId();
-        }
-    }
-
-    private void initDMResourceId() {
-        LOGGER.warn("support for the dameng database is currently an 
experimental feature ");
-        if (jdbcUrl.contains("?")) {
-            StringBuilder jdbcUrlBuilder = new StringBuilder();
-            jdbcUrlBuilder.append(jdbcUrl, 0, jdbcUrl.indexOf('?'));
-
-            StringBuilder paramsBuilder = new StringBuilder();
-            String paramUrl = jdbcUrl.substring(jdbcUrl.indexOf('?') + 1);
-            String[] urlParams = paramUrl.split("&");
-            for (String urlParam : urlParams) {
-                if (urlParam.contains("schema")) {
-                    // remove the '"'
-                    if (urlParam.contains("\"")) {
-                        urlParam = urlParam.replaceAll("\"", "");
-                    }
-                    paramsBuilder.append(urlParam);
-                    break;
-                }
-            }
-
-            if (paramsBuilder.length() > 0) {
-                jdbcUrlBuilder.append("?");
-                jdbcUrlBuilder.append(paramsBuilder);
-            }
-            resourceId = jdbcUrlBuilder.toString();
-        } else {
-            resourceId = jdbcUrl;
-        }
-    }
-
-    /**
-     * init the oscar resource id
-     * jdbc:oscar://192.168.x.xx:2003/OSRDB
-     */
-    private void initOscarResourceId() {
-        if (jdbcUrl.contains("?")) {
-            resourceId = jdbcUrl.substring(0, jdbcUrl.indexOf('?')) + "/" + 
userName;
-        } else {
-            resourceId = jdbcUrl + "/" + userName;
-        }
-    }
-
-    /**
-     * prevent pg sql url like
-     * jdbc:postgresql://127.0.0.1:5432/seata?currentSchema=public
-     * jdbc:postgresql://127.0.0.1:5432/seata?currentSchema=seata
-     * cause the duplicated resourceId
-     * it will cause the problem like
-     * 1.get file lock fail
-     * 2.error table meta cache
-     */
-    private void initPGResourceId() {
-        if (jdbcUrl.contains("?")) {
-            StringBuilder jdbcUrlBuilder = new StringBuilder();
-            jdbcUrlBuilder.append(jdbcUrl, 0, jdbcUrl.indexOf('?'));
-
-            StringBuilder paramsBuilder = new StringBuilder();
-            String paramUrl = jdbcUrl.substring(jdbcUrl.indexOf('?') + 1);
-            String[] urlParams = paramUrl.split("&");
-            for (String urlParam : urlParams) {
-                if (urlParam.contains("currentSchema")) {
-                    if (urlParam.contains(Constants.DBKEYS_SPLIT_CHAR)) {
-                        urlParam = 
urlParam.replace(Constants.DBKEYS_SPLIT_CHAR, "!");
-                    }
-                    paramsBuilder.append(urlParam);
-                    break;
-                }
-            }
-
-            if (paramsBuilder.length() > 0) {
-                jdbcUrlBuilder.append("?");
-                jdbcUrlBuilder.append(paramsBuilder);
-            }
-            resourceId = jdbcUrlBuilder.toString();
-        } else {
-            resourceId = jdbcUrl;
-        }
-        if (resourceId.contains(",")) {
-            resourceId = resourceId.replace(",", "|");
-        }
-    }
-
-    /**
-     * The general form of the connection URL for SqlServer is
-     * 
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
-     * required connection properties: [INSTANCENAME], [databaseName,database]
-     *
-     */
-    private void initSqlServerResourceId() {
-        if (jdbcUrl.contains(";")) {
-            StringBuilder jdbcUrlBuilder = new StringBuilder();
-            jdbcUrlBuilder.append(jdbcUrl, 0, jdbcUrl.indexOf(';'));
-            StringBuilder paramsBuilder = new StringBuilder();
-            String paramUrl = jdbcUrl.substring(jdbcUrl.indexOf(';') + 1);
-            String[] urlParams = paramUrl.split(";");
-            for (String urlParam : urlParams) {
-                String[] paramSplit = urlParam.split("=");
-                String propertyName = paramSplit[0];
-                if ("INSTANCENAME".equalsIgnoreCase(propertyName)
-                        || "databaseName".equalsIgnoreCase(propertyName)
-                        || "database".equalsIgnoreCase(propertyName)) {
-                    paramsBuilder.append(urlParam);
-                }
-            }
-
-            if (paramsBuilder.length() > 0) {
-                jdbcUrlBuilder.append(";");
-                jdbcUrlBuilder.append(paramsBuilder);
-            }
-            resourceId = jdbcUrlBuilder.toString();
-        } else {
-            resourceId = jdbcUrl;
-        }
+        ResourceIdInitializer initializer = 
ResourceIdInitializerRegistry.getInitializer(dbType, this);
+        initializer.initResourceId(this);
     }
 
     @Override
@@ -453,6 +281,18 @@ public class DataSourceProxy extends 
AbstractDataSourceProxy implements Resource
         }
     }
 
+    public String getJdbcUrl() {
+        return jdbcUrl;
+    }
+
+    public void setResourceId(String resourceId) {
+        this.resourceId = resourceId;
+    }
+
+    public String getUserName() {
+        return userName;
+    }
+
     public void close() throws Exception {
         // TODO: Need to unregister resource from DefaultResourceManager
         TableMetaCacheFactory.shutdown(resourceId);
diff --git 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/AbstractResourceIdInitializer.java
 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/AbstractResourceIdInitializer.java
new file mode 100644
index 0000000000..91f1bc513a
--- /dev/null
+++ 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/AbstractResourceIdInitializer.java
@@ -0,0 +1,29 @@
+/*
+ * 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.seata.rm.datasource.initializer;
+
+import org.apache.seata.rm.datasource.DataSourceProxy;
+
+public abstract class AbstractResourceIdInitializer implements 
ResourceIdInitializer {
+    @Override
+    public void initResourceId(DataSourceProxy proxy) {
+        doInitResourceId(proxy);
+    }
+
+    protected abstract void doInitResourceId(DataSourceProxy proxy);
+}
diff --git 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/ResourceIdInitializer.java
 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/ResourceIdInitializer.java
new file mode 100644
index 0000000000..75aefcad5c
--- /dev/null
+++ 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/ResourceIdInitializer.java
@@ -0,0 +1,28 @@
+/*
+ * 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.seata.rm.datasource.initializer;
+
+import org.apache.seata.rm.datasource.DataSourceProxy;
+
+public interface ResourceIdInitializer {
+    String JDBC_URL_SPLIT_CHAR = "?";
+
+    boolean supports(String dbType, DataSourceProxy proxy);
+
+    void initResourceId(DataSourceProxy proxy);
+}
diff --git 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/ResourceIdInitializerRegistry.java
 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/ResourceIdInitializerRegistry.java
new file mode 100644
index 0000000000..1767f1a43e
--- /dev/null
+++ 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/ResourceIdInitializerRegistry.java
@@ -0,0 +1,52 @@
+/*
+ * 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.seata.rm.datasource.initializer;
+
+import org.apache.seata.rm.datasource.DataSourceProxy;
+import org.apache.seata.rm.datasource.initializer.db.DMResourceIdInitializer;
+import 
org.apache.seata.rm.datasource.initializer.db.DefaultResourceIdInitializer;
+import 
org.apache.seata.rm.datasource.initializer.db.MysqlResourceIdInitializer;
+import 
org.apache.seata.rm.datasource.initializer.db.OracleResourceIdInitializer;
+import 
org.apache.seata.rm.datasource.initializer.db.OscarResourceIdInitializer;
+import 
org.apache.seata.rm.datasource.initializer.db.PostgresqlResourceIdInitializer;
+import 
org.apache.seata.rm.datasource.initializer.db.SqlServerResourceIdInitializer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ResourceIdInitializerRegistry {
+    private static final List<ResourceIdInitializer> INITIALIZERS = new 
ArrayList<>();
+
+    static {
+        INITIALIZERS.add(new PostgresqlResourceIdInitializer());
+        INITIALIZERS.add(new OracleResourceIdInitializer());
+        INITIALIZERS.add(new MysqlResourceIdInitializer());
+        INITIALIZERS.add(new SqlServerResourceIdInitializer());
+        INITIALIZERS.add(new DMResourceIdInitializer());
+        INITIALIZERS.add(new OscarResourceIdInitializer());
+    }
+
+    public static ResourceIdInitializer getInitializer(String dbType, 
DataSourceProxy proxy) {
+        for (ResourceIdInitializer initializer : INITIALIZERS) {
+            if (initializer.supports(dbType, proxy)) {
+                return initializer;
+            }
+        }
+        return new DefaultResourceIdInitializer();
+    }
+}
diff --git 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/DMResourceIdInitializer.java
 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/DMResourceIdInitializer.java
new file mode 100644
index 0000000000..9734f68813
--- /dev/null
+++ 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/DMResourceIdInitializer.java
@@ -0,0 +1,59 @@
+/*
+ * 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.seata.rm.datasource.initializer.db;
+
+import org.apache.seata.rm.datasource.DataSourceProxy;
+import 
org.apache.seata.rm.datasource.initializer.AbstractResourceIdInitializer;
+import org.apache.seata.sqlparser.util.JdbcConstants;
+
+public class DMResourceIdInitializer extends AbstractResourceIdInitializer {
+    @Override
+    public boolean supports(String dbType, DataSourceProxy proxy) {
+        return JdbcConstants.DM.equals(dbType);
+    }
+
+    @Override
+    protected void doInitResourceId(DataSourceProxy proxy) {
+        String resourceId = proxy.getJdbcUrl();
+        if (resourceId.contains(JDBC_URL_SPLIT_CHAR)) {
+            StringBuilder jdbcUrlBuilder = new StringBuilder();
+            jdbcUrlBuilder.append(resourceId, 0, 
resourceId.indexOf(JDBC_URL_SPLIT_CHAR));
+
+            StringBuilder paramsBuilder = new StringBuilder();
+            String paramUrl = 
resourceId.substring(resourceId.indexOf(JDBC_URL_SPLIT_CHAR) + 1);
+            String[] urlParams = paramUrl.split("&");
+            for (String urlParam : urlParams) {
+                if (urlParam.contains("schema")) {
+                    // remove the '"'
+                    if (urlParam.contains("\"")) {
+                        urlParam = urlParam.replaceAll("\"", "");
+                    }
+                    paramsBuilder.append(urlParam);
+                    break;
+                }
+            }
+
+            if (paramsBuilder.length() > 0) {
+                jdbcUrlBuilder.append(JDBC_URL_SPLIT_CHAR);
+                jdbcUrlBuilder.append(paramsBuilder);
+            }
+            resourceId = jdbcUrlBuilder.toString();
+        }
+        proxy.setResourceId(resourceId);
+    }
+}
diff --git 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/DefaultResourceIdInitializer.java
 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/DefaultResourceIdInitializer.java
new file mode 100644
index 0000000000..19cef208ce
--- /dev/null
+++ 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/DefaultResourceIdInitializer.java
@@ -0,0 +1,38 @@
+/*
+ * 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.seata.rm.datasource.initializer.db;
+
+import org.apache.seata.rm.datasource.DataSourceProxy;
+import 
org.apache.seata.rm.datasource.initializer.AbstractResourceIdInitializer;
+
+public class DefaultResourceIdInitializer extends 
AbstractResourceIdInitializer {
+    @Override
+    protected void doInitResourceId(DataSourceProxy proxy) {
+        String jdbcUrl = proxy.getJdbcUrl();
+        String resourceId = jdbcUrl;
+        if (jdbcUrl.contains(JDBC_URL_SPLIT_CHAR)) {
+            resourceId = jdbcUrl.substring(0, 
jdbcUrl.indexOf(JDBC_URL_SPLIT_CHAR));
+        }
+        proxy.setResourceId(resourceId);
+    }
+
+    @Override
+    public boolean supports(String dbType, DataSourceProxy proxy) {
+        return false;
+    }
+}
diff --git 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/MysqlResourceIdInitializer.java
 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/MysqlResourceIdInitializer.java
new file mode 100644
index 0000000000..e1df765316
--- /dev/null
+++ 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/MysqlResourceIdInitializer.java
@@ -0,0 +1,55 @@
+/*
+ * 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.seata.rm.datasource.initializer.db;
+
+import org.apache.seata.rm.datasource.DataSourceProxy;
+import 
org.apache.seata.rm.datasource.initializer.AbstractResourceIdInitializer;
+import org.apache.seata.sqlparser.util.JdbcConstants;
+
+public class MysqlResourceIdInitializer extends AbstractResourceIdInitializer {
+    @Override
+    public boolean supports(String dbType, DataSourceProxy proxy) {
+        return JdbcConstants.MYSQL.equals(dbType) || 
JdbcConstants.POLARDBX.equals(dbType);
+    }
+
+    /**
+     * jdbc:mysql:loadbalance://192.168.100.2:3306,192.168.100.1:3306/seata
+     * @param proxy
+     */
+    @Override
+    protected void doInitResourceId(DataSourceProxy proxy) {
+        String startsWith = "jdbc:mysql:loadbalance://";
+        if (proxy.getJdbcUrl().startsWith(startsWith)) {
+            String url = proxy.getJdbcUrl();
+            if (url.contains(JDBC_URL_SPLIT_CHAR)) {
+                url = url.substring(0, url.indexOf(JDBC_URL_SPLIT_CHAR));
+            }
+            proxy.setResourceId(url.replace(",", "|"));
+        } else {
+            initDefaultResourceId(proxy);
+        }
+    }
+
+    private void initDefaultResourceId(DataSourceProxy proxy) {
+        String resourceId = proxy.getJdbcUrl();
+        if (resourceId.contains(JDBC_URL_SPLIT_CHAR)) {
+            resourceId = resourceId.substring(0, 
resourceId.indexOf(JDBC_URL_SPLIT_CHAR));
+        }
+        proxy.setResourceId(resourceId);
+    }
+}
diff --git 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/OracleResourceIdInitializer.java
 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/OracleResourceIdInitializer.java
new file mode 100644
index 0000000000..ae2789972b
--- /dev/null
+++ 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/OracleResourceIdInitializer.java
@@ -0,0 +1,39 @@
+/*
+ * 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.seata.rm.datasource.initializer.db;
+
+import org.apache.seata.rm.datasource.DataSourceProxy;
+import 
org.apache.seata.rm.datasource.initializer.AbstractResourceIdInitializer;
+import org.apache.seata.sqlparser.util.JdbcConstants;
+
+public class OracleResourceIdInitializer extends AbstractResourceIdInitializer 
{
+    @Override
+    public boolean supports(String dbType, DataSourceProxy proxy) {
+        return JdbcConstants.ORACLE.equals(dbType);
+    }
+
+    @Override
+    protected void doInitResourceId(DataSourceProxy proxy) {
+        String resourceId = proxy.getJdbcUrl();
+        if (proxy.getJdbcUrl().contains(JDBC_URL_SPLIT_CHAR)) {
+            resourceId = proxy.getJdbcUrl().substring(0, 
proxy.getJdbcUrl().indexOf(JDBC_URL_SPLIT_CHAR));
+        }
+        resourceId = resourceId + "/" + proxy.getUserName();
+        proxy.setResourceId(resourceId);
+    }
+}
diff --git 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/OscarResourceIdInitializer.java
 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/OscarResourceIdInitializer.java
new file mode 100644
index 0000000000..fab830e4e5
--- /dev/null
+++ 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/OscarResourceIdInitializer.java
@@ -0,0 +1,39 @@
+/*
+ * 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.seata.rm.datasource.initializer.db;
+
+import org.apache.seata.rm.datasource.DataSourceProxy;
+import 
org.apache.seata.rm.datasource.initializer.AbstractResourceIdInitializer;
+import org.apache.seata.sqlparser.util.JdbcConstants;
+
+public class OscarResourceIdInitializer extends AbstractResourceIdInitializer {
+    @Override
+    public boolean supports(String dbType, DataSourceProxy proxy) {
+        return JdbcConstants.OSCAR.equals(dbType);
+    }
+
+    @Override
+    protected void doInitResourceId(DataSourceProxy proxy) {
+        String resourceId = proxy.getJdbcUrl();
+        if (resourceId.contains(JDBC_URL_SPLIT_CHAR)) {
+            resourceId = resourceId.substring(0, 
resourceId.indexOf(JDBC_URL_SPLIT_CHAR));
+        }
+        resourceId = resourceId + "/" + proxy.getUserName();
+        proxy.setResourceId(resourceId);
+    }
+}
diff --git 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/PostgresqlResourceIdInitializer.java
 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/PostgresqlResourceIdInitializer.java
new file mode 100644
index 0000000000..676074d55b
--- /dev/null
+++ 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/PostgresqlResourceIdInitializer.java
@@ -0,0 +1,68 @@
+/*
+ * 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.seata.rm.datasource.initializer.db;
+
+import org.apache.seata.common.Constants;
+import org.apache.seata.rm.datasource.DataSourceProxy;
+import 
org.apache.seata.rm.datasource.initializer.AbstractResourceIdInitializer;
+import org.apache.seata.sqlparser.util.JdbcConstants;
+
+public class PostgresqlResourceIdInitializer extends 
AbstractResourceIdInitializer {
+    @Override
+    public boolean supports(String dbType, DataSourceProxy proxy) {
+        return JdbcConstants.POSTGRESQL.equals(dbType);
+    }
+
+    /**
+     * jdbc:postgresql://127.0.0.1:5432/seata?currentSchema=public
+     * jdbc:postgresql://127.0.0.1:5432/seata?currentSchema=seata
+     *
+     * @param proxy
+     */
+    @Override
+    protected void doInitResourceId(DataSourceProxy proxy) {
+        String resourceId = proxy.getJdbcUrl();
+        if (resourceId.contains(JDBC_URL_SPLIT_CHAR)) {
+            StringBuilder jdbcUrlBuilder = new StringBuilder();
+            jdbcUrlBuilder.append(resourceId, 0, 
resourceId.indexOf(JDBC_URL_SPLIT_CHAR));
+
+            StringBuilder paramsBuilder = new StringBuilder();
+            String paramUrl = 
resourceId.substring(resourceId.indexOf(JDBC_URL_SPLIT_CHAR) + 1);
+            String[] urlParams = paramUrl.split("&");
+            for (String urlParam : urlParams) {
+                if (urlParam.contains("currentSchema")) {
+                    if (urlParam.contains(Constants.DBKEYS_SPLIT_CHAR)) {
+                        urlParam = 
urlParam.replace(Constants.DBKEYS_SPLIT_CHAR, "!");
+                    }
+                    paramsBuilder.append(urlParam);
+                    break;
+                }
+            }
+
+            if (paramsBuilder.length() > 0) {
+                jdbcUrlBuilder.append(JDBC_URL_SPLIT_CHAR);
+                jdbcUrlBuilder.append(paramsBuilder);
+            }
+            resourceId = jdbcUrlBuilder.toString();
+        }
+        if (resourceId.contains(",")) {
+            resourceId = resourceId.replace(",", "|");
+        }
+        proxy.setResourceId(resourceId);
+    }
+}
diff --git 
a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/SqlServerResourceIdInitializer.java
 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/SqlServerResourceIdInitializer.java
new file mode 100644
index 0000000000..ecf50093af
--- /dev/null
+++ 
b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/initializer/db/SqlServerResourceIdInitializer.java
@@ -0,0 +1,76 @@
+/*
+ * 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.seata.rm.datasource.initializer.db;
+
+import org.apache.seata.rm.datasource.DataSourceProxy;
+import 
org.apache.seata.rm.datasource.initializer.AbstractResourceIdInitializer;
+import org.apache.seata.sqlparser.util.JdbcConstants;
+import org.jetbrains.annotations.NotNull;
+
+public class SqlServerResourceIdInitializer extends 
AbstractResourceIdInitializer {
+    @Override
+    public boolean supports(String dbType, DataSourceProxy proxy) {
+        return JdbcConstants.SQLSERVER.equals(dbType);
+    }
+
+    /**
+     * The general form of the connection URL for SqlServer is
+     * 
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
+     * required connection properties: [INSTANCENAME], [databaseName,database]
+     *
+     * @param proxy
+     */
+    @Override
+    protected void doInitResourceId(DataSourceProxy proxy) {
+        String resourceId = proxy.getJdbcUrl();
+        if (resourceId.contains(";")) {
+            StringBuilder jdbcUrlBuilder = new StringBuilder();
+            jdbcUrlBuilder.append(resourceId, 0, resourceId.indexOf(';'));
+            StringBuilder paramsBuilder = getParamsBuilder(resourceId);
+
+            if (paramsBuilder.length() > 0) {
+                jdbcUrlBuilder.append(";");
+                jdbcUrlBuilder.append(paramsBuilder);
+            }
+            resourceId = jdbcUrlBuilder.toString();
+        }
+        proxy.setResourceId(resourceId);
+    }
+
+    @NotNull
+    private static StringBuilder getParamsBuilder(String resourceId) {
+        StringBuilder paramsBuilder = new StringBuilder();
+        String paramUrl = resourceId.substring(resourceId.indexOf(';') + 1);
+        String[] urlParams = paramUrl.split(";");
+        boolean first = true;
+        for (String urlParam : urlParams) {
+            String[] paramSplit = urlParam.split("=");
+            String propertyName = paramSplit[0];
+            if ("INSTANCENAME".equalsIgnoreCase(propertyName)
+                    || "databaseName".equalsIgnoreCase(propertyName)
+                    || "database".equalsIgnoreCase(propertyName)) {
+                if (!first) {
+                    paramsBuilder.append(";");
+                }
+                paramsBuilder.append(urlParam);
+                first = false;
+            }
+        }
+        return paramsBuilder;
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


Reply via email to