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

terrymanu 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 e81d2b9fafb Verify XA connections close when enlistment fails (#40103)
e81d2b9fafb is described below

commit e81d2b9fafbeaf632147532e8f45285140d7f05b
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Oct 7 20:50:14 2026 +0800

    Verify XA connections close when enlistment fails (#40103)
---
 .../xa/jta/datasource/XATransactionDataSourceTest.java | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git 
a/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionDataSourceTest.java
 
b/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionDataSourceTest.java
index 3d9ad2423f1..cb6de94f00f 100644
--- 
a/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionDataSourceTest.java
+++ 
b/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/XATransactionDataSourceTest.java
@@ -194,29 +194,35 @@ class XATransactionDataSourceTest {
     }
     
     @Test
-    void assertCloseConnectionWhenEnlistResourceFailedWithSystemException() 
throws SystemException, RollbackException {
+    void assertCloseConnectionWhenEnlistResourceFailedWithSystemException() 
throws SQLException, SystemException, RollbackException {
         DataSource dataSource = DataSourceUtils.build(HikariDataSource.class, 
TypedSPILoader.getService(DatabaseType.class, "H2"), "ds1");
         XATransactionDataSource transactionDataSource = new 
XATransactionDataSource(TypedSPILoader.getService(DatabaseType.class, "H2"), 
"ds1", dataSource, xaTransactionManagerProvider);
         when(transaction.enlistResource(any())).thenThrow(new 
SystemException("enlist resource failed"));
-        assertThrows(SystemException.class, () -> 
transactionDataSource.getConnection(mock()));
+        AtomicReference<Connection> actualConnection = new AtomicReference<>();
+        assertThrows(SystemException.class, () -> 
transactionDataSource.getConnection(actualConnection::set));
+        assertTrue(actualConnection.get().isClosed());
         verify(transaction).enlistResource(any(SingleXAResource.class));
     }
     
     @Test
-    void assertCloseConnectionWhenEnlistResourceFailedWithRollbackException() 
throws RollbackException, SystemException {
+    void assertCloseConnectionWhenEnlistResourceFailedWithRollbackException() 
throws SQLException, RollbackException, SystemException {
         DataSource dataSource = DataSourceUtils.build(HikariDataSource.class, 
TypedSPILoader.getService(DatabaseType.class, "H2"), "ds1");
         XATransactionDataSource transactionDataSource = new 
XATransactionDataSource(TypedSPILoader.getService(DatabaseType.class, "H2"), 
"ds1", dataSource, xaTransactionManagerProvider);
         when(transaction.enlistResource(any())).thenThrow(new 
RollbackException("enlist resource failed"));
-        assertThrows(RollbackException.class, () -> 
transactionDataSource.getConnection(mock()));
+        AtomicReference<Connection> actualConnection = new AtomicReference<>();
+        assertThrows(RollbackException.class, () -> 
transactionDataSource.getConnection(actualConnection::set));
+        assertTrue(actualConnection.get().isClosed());
         verify(transaction).enlistResource(any(SingleXAResource.class));
     }
     
     @Test
-    void assertCloseConnectionWhenEnlistResourceFailedWithRuntimeException() 
throws SystemException, RollbackException {
+    void assertCloseConnectionWhenEnlistResourceFailedWithRuntimeException() 
throws SQLException, SystemException, RollbackException {
         DataSource dataSource = DataSourceUtils.build(HikariDataSource.class, 
TypedSPILoader.getService(DatabaseType.class, "H2"), "ds1");
         XATransactionDataSource transactionDataSource = new 
XATransactionDataSource(TypedSPILoader.getService(DatabaseType.class, "H2"), 
"ds1", dataSource, xaTransactionManagerProvider);
         when(transaction.enlistResource(any())).thenThrow(new 
RuntimeException("enlist resource failed"));
-        assertThrows(RuntimeException.class, () -> 
transactionDataSource.getConnection(mock()));
+        AtomicReference<Connection> actualConnection = new AtomicReference<>();
+        assertThrows(RuntimeException.class, () -> 
transactionDataSource.getConnection(actualConnection::set));
+        assertTrue(actualConnection.get().isClosed());
         verify(transaction).enlistResource(any(SingleXAResource.class));
     }
 }

Reply via email to