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

duanzhengqiang 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 97f2616092c Fix enlist xa resource thread safety issue (#37136)
97f2616092c is described below

commit 97f2616092ca9d8f4dc7c0e5fbd16a5a64b136cb
Author: ZhangCheng <[email protected]>
AuthorDate: Wed Nov 19 09:02:03 2025 +0800

    Fix enlist xa resource thread safety issue (#37136)
    
    * Fix enlist xa resource thread safety issue
    
    * Fix enlist xa resource thread safety issue
---
 .../xa/jta/datasource/XATransactionDataSource.java       | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git 
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionDataSource.java
 
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionDataSource.java
index 7d5d8ac269e..449e95eda4a 100644
--- 
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionDataSource.java
+++ 
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionDataSource.java
@@ -54,7 +54,7 @@ public final class XATransactionDataSource implements 
AutoCloseable {
     
     private final ThreadLocal<Map<Transaction, Collection<Connection>>> 
enlistedTransactions = ThreadLocal.withInitial(HashMap::new);
     
-    private final AtomicInteger uniqueName = new AtomicInteger();
+    private final ThreadLocal<AtomicInteger> uniqueName = 
ThreadLocal.withInitial(AtomicInteger::new);
     
     private final String resourceName;
     
@@ -93,13 +93,20 @@ public final class XATransactionDataSource implements 
AutoCloseable {
         Transaction transaction = 
xaTransactionManagerProvider.getTransactionManager().getTransaction();
         Connection connection = dataSource.getConnection();
         XAConnection xaConnection = xaConnectionWrapper.wrap(xaDataSource, 
connection);
-        transaction.enlistResource(new SingleXAResource(resourceName, 
String.valueOf(uniqueName.getAndIncrement()), xaConnection.getXAResource()));
+        transaction.enlistResource(new SingleXAResource(resourceName, 
String.valueOf(uniqueName.get().getAndIncrement()), 
xaConnection.getXAResource()));
+        registerSynchronization(transaction);
+        enlistedTransactions.get().computeIfAbsent(transaction, key -> new 
LinkedList<>());
+        enlistedTransactions.get().get(transaction).add(connection);
+        return connection;
+    }
+    
+    private void registerSynchronization(final Transaction transaction) throws 
RollbackException, SystemException {
         transaction.registerSynchronization(new Synchronization() {
             
             @Override
             public void beforeCompletion() {
                 enlistedTransactions.get().remove(transaction);
-                uniqueName.set(0);
+                uniqueName.remove();
             }
             
             @Override
@@ -107,9 +114,6 @@ public final class XATransactionDataSource implements 
AutoCloseable {
                 enlistedTransactions.get().clear();
             }
         });
-        enlistedTransactions.get().computeIfAbsent(transaction, key -> new 
LinkedList<>());
-        enlistedTransactions.get().get(transaction).add(connection);
-        return connection;
     }
     
     @Override

Reply via email to