You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/04/29 12:15:19 UTC

[shardingsphere] branch master updated: Support xa-atomikos transactions for jdbc (#17207)

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

zhangliang 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 a8ce45109e2 Support xa-atomikos transactions for jdbc (#17207)
a8ce45109e2 is described below

commit a8ce45109e2327847979b42530c4b21601c1f8df
Author: Guocheng Tang <to...@apache.org>
AuthorDate: Fri Apr 29 20:15:13 2022 +0800

    Support xa-atomikos transactions for jdbc (#17207)
---
 .../config/YamlExampleConfigurationSupportedValue.java   |  2 +-
 .../src/main/resources/config.yaml                       |  1 +
 .../jdbc/java/repository/jdbc/OrderRepository.ftl        | 16 ++++++++++++++++
 .../src/main/resources/template/jdbc/pom.ftl             |  7 +++++++
 4 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/yaml/config/YamlExampleConfigurationSupportedValue.java b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/yaml/config/YamlExampleConfigurationSupportedValue.java
index 44620792f68..28c9a12f07c 100644
--- a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/yaml/config/YamlExampleConfigurationSupportedValue.java
+++ b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/core/yaml/config/YamlExampleConfigurationSupportedValue.java
@@ -34,7 +34,7 @@ public enum YamlExampleConfigurationSupportedValue {
     
     MODES("modes", Sets.newHashSet("memory", "proxy", "cluster-zookeeper", "cluster-etcd", "standalone-file")),
     
-    TRANSACTIONS("transactions", Sets.newHashSet("local")),
+    TRANSACTIONS("transactions", Sets.newHashSet("local", "xa-atomikos")),
     
     FEATURES("features", Sets.newHashSet("shadow", "sharding", "readwrite-splitting", "encrypt", "db-discovery")),
     
diff --git a/examples/shardingsphere-example-generator/src/main/resources/config.yaml b/examples/shardingsphere-example-generator/src/main/resources/config.yaml
index ff22c9adf49..7baefd2fc97 100644
--- a/examples/shardingsphere-example-generator/src/main/resources/config.yaml
+++ b/examples/shardingsphere-example-generator/src/main/resources/config.yaml
@@ -26,6 +26,7 @@ modes:
 # supported: local
 transactions: 
   - local
+  - xa-atomikos
 
 # supported: sharding,readwrite-splitting,encrypt,shadow,db-discovery
 features: 
diff --git a/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/java/repository/jdbc/OrderRepository.ftl b/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/java/repository/jdbc/OrderRepository.ftl
index 813b8206833..c264d4a046f 100644
--- a/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/java/repository/jdbc/OrderRepository.ftl
+++ b/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/java/repository/jdbc/OrderRepository.ftl
@@ -19,6 +19,10 @@
 package org.apache.shardingsphere.example.${package}.${framework?replace('-', '.')}.repository;
 
 import org.apache.shardingsphere.example.${package}.${framework?replace('-', '.')}.entity.Order;
+<#if transaction?contains("xa")>
+import org.apache.shardingsphere.transaction.core.TransactionType;
+import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
+</#if>
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -104,8 +108,14 @@ public final class OrderRepository {
     
     public Long insert(final Order order) throws SQLException {
         String sql = "INSERT INTO t_order (user_id, order_type, address_id, status) VALUES (?, ?, ?, ?)";
+    <#if transaction?contains("xa")>
+        TransactionTypeHolder.set(TransactionType.XA);
+    </#if>
         try (Connection connection = dataSource.getConnection();
              PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
+        <#if transaction?contains("xa")>
+            connection.setAutoCommit(false);
+        </#if>
             preparedStatement.setInt(1, order.getUserId());
             preparedStatement.setInt(2, order.getOrderType());
             preparedStatement.setLong(3, order.getAddressId());
@@ -116,7 +126,13 @@ public final class OrderRepository {
                     order.setOrderId(resultSet.getLong(1));
                 }
             }
+        <#if transaction?contains("xa")>
+            connection.commit();
+        </#if>
+        }<#if transaction?contains("xa")> finally {
+            TransactionTypeHolder.clear();
         }
+        </#if>
         return order.getOrderId();
     }
     
diff --git a/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/pom.ftl b/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/pom.ftl
index 80b0a957fac..5140c6307c1 100644
--- a/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/pom.ftl
+++ b/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/pom.ftl
@@ -40,6 +40,13 @@
             <version>${r'${project.version}'}</version>
         </dependency>
     </#if>
+    <#if transaction?contains("xa")>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-transaction-xa-core</artifactId>
+            <version>${r'${project.version}'}</version>
+        </dependency>
+    </#if>
     <#if framework?contains("jpa")>
         <dependency>
             <groupId>org.hibernate.javax.persistence</groupId>