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/01/11 14:04:43 UTC

[shardingsphere] branch master updated: fix issue #14663 (#14690)

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 8e5a856  fix issue #14663 (#14690)
8e5a856 is described below

commit 8e5a856e749b95860ec630c247a9582d86d5d261
Author: JingShang Lu <lu...@apache.org>
AuthorDate: Tue Jan 11 22:03:31 2022 +0800

    fix issue #14663 (#14690)
    
    * fix issue #14663
    
    * Update XATransactionDataSource.java
---
 .../xa/jta/datasource/XATransactionDataSource.java        | 15 ++++++++-------
 .../xa/XAShardingSphereTransactionManagerTest.java        |  9 ++++-----
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionDataSource.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionData [...]
index ce9bd63..eca0869 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionDataSource.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionDataSource.java
@@ -33,7 +33,8 @@ import javax.transaction.Transaction;
 import java.lang.reflect.Method;
 import java.sql.Connection;
 import java.sql.SQLException;
-import java.util.HashSet;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -43,7 +44,7 @@ public final class XATransactionDataSource implements AutoCloseable {
     
     private static final Set<String> CONTAINER_DATASOURCE_NAMES = Sets.newHashSet("AtomikosDataSourceBean", "BasicManagedDataSource");
     
-    private final ThreadLocal<Set<Transaction>> enlistedTransactions = ThreadLocal.withInitial(HashSet::new);
+    private final ThreadLocal<Map<Transaction, Connection>> enlistedTransactions = ThreadLocal.withInitial(HashMap::new);
     
     private final DatabaseType databaseType;
     
@@ -78,10 +79,10 @@ public final class XATransactionDataSource implements AutoCloseable {
         if (CONTAINER_DATASOURCE_NAMES.contains(dataSource.getClass().getSimpleName())) {
             return dataSource.getConnection();
         }
-        Connection result = dataSource.getConnection();
-        XAConnection xaConnection = XAConnectionFactory.createXAConnection(databaseType, xaDataSource, result);
         Transaction transaction = xaTransactionManagerProvider.getTransactionManager().getTransaction();
-        if (!enlistedTransactions.get().contains(transaction)) {
+        if (!enlistedTransactions.get().containsKey(transaction)) {
+            Connection result = dataSource.getConnection();
+            XAConnection xaConnection = XAConnectionFactory.createXAConnection(databaseType, xaDataSource, result);
             transaction.enlistResource(new SingleXAResource(resourceName, xaConnection.getXAResource()));
             transaction.registerSynchronization(new Synchronization() {
                 @Override
@@ -94,9 +95,9 @@ public final class XATransactionDataSource implements AutoCloseable {
                     enlistedTransactions.get().clear();
                 }
             });
-            enlistedTransactions.get().add(transaction);
+            enlistedTransactions.get().put(transaction, result);
         }
-        return result;
+        return enlistedTransactions.get().get(transaction);
     }
     
     @Override
diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManagerTest.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManag [...]
index c9e4ae9..9be061c 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManagerTest.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManagerTest.java
@@ -42,7 +42,6 @@ import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
@@ -100,7 +99,7 @@ public final class XAShardingSphereTransactionManagerTest {
     
     @Test
     public void assertGetConnectionOfNestedTransaction() throws SQLException {
-        ThreadLocal<Set<Transaction>> transactions = getEnlistedTransactions(getCachedDataSources().get("ds1"));
+        ThreadLocal<Map<Transaction, Connection>> transactions = getEnlistedTransactions(getCachedDataSources().get("ds1"));
         xaTransactionManager.begin();
         assertTrue(transactions.get().isEmpty());
         xaTransactionManager.getConnection("ds1");
@@ -111,7 +110,7 @@ public final class XAShardingSphereTransactionManagerTest {
         assertTrue(transactions.get().isEmpty());
     }
     
-    private void executeNestedTransaction(final ThreadLocal<Set<Transaction>> transactions) throws SQLException {
+    private void executeNestedTransaction(final ThreadLocal<Map<Transaction, Connection>> transactions) throws SQLException {
         xaTransactionManager.begin();
         xaTransactionManager.getConnection("ds1");
         assertThat(transactions.get().size(), is(2));
@@ -152,10 +151,10 @@ public final class XAShardingSphereTransactionManagerTest {
     
     @SneakyThrows(ReflectiveOperationException.class)
     @SuppressWarnings("unchecked")
-    private ThreadLocal<Set<Transaction>> getEnlistedTransactions(final XATransactionDataSource transactionDataSource) {
+    private ThreadLocal<Map<Transaction, Connection>> getEnlistedTransactions(final XATransactionDataSource transactionDataSource) {
         Field field = transactionDataSource.getClass().getDeclaredField("enlistedTransactions");
         field.setAccessible(true);
-        return (ThreadLocal<Set<Transaction>>) field.get(transactionDataSource);
+        return (ThreadLocal<Map<Transaction, Connection>>) field.get(transactionDataSource);
     }
     
     private Collection<ResourceDataSource> createResourceDataSources(final DatabaseType databaseType) {