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 196bf7f0998 Remove the exceptionOccur attribute in the 
TransactionStatus class (#31245)
196bf7f0998 is described below

commit 196bf7f099876a2d07c50dd4792fc89fab74a520
Author: ZhangCheng <[email protected]>
AuthorDate: Thu May 16 18:27:16 2024 +0800

    Remove the exceptionOccur attribute in the TransactionStatus class (#31245)
---
 .../connector/jdbc/transaction/BackendTransactionManager.java     | 8 +++-----
 .../connector/jdbc/transaction/LocalTransactionManager.java       | 2 +-
 .../proxy/backend/handler/ProxyBackendHandlerFactory.java         | 2 +-
 .../backend/handler/transaction/TransactionBackendHandler.java    | 2 +-
 .../proxy/backend/session/transaction/TransactionStatus.java      | 2 --
 .../connector/jdbc/transaction/LocalTransactionManagerTest.java   | 8 +++++++-
 .../proxy/frontend/opengauss/OpenGaussFrontendEngine.java         | 5 +++--
 .../proxy/frontend/postgresql/PostgreSQLFrontendEngine.java       | 5 +++--
 8 files changed, 19 insertions(+), 15 deletions(-)

diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManager.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManager.java
index 34611c3896b..5253870c176 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManager.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManager.java
@@ -92,14 +92,13 @@ public final class BackendTransactionManager implements 
TransactionManager {
                 if (TransactionType.LOCAL == 
TransactionUtils.getTransactionType(getTransactionContext()) || null == 
shardingSphereTransactionManager) {
                     localTransactionManager.commit();
                 } else {
-                    
shardingSphereTransactionManager.commit(connection.getConnectionSession().getTransactionStatus().isExceptionOccur());
+                    
shardingSphereTransactionManager.commit(getTransactionContext().isExceptionOccur());
                 }
             } finally {
                 for (TransactionHook each : transactionHooks) {
                     
each.afterCommit(connection.getCachedConnections().values(), 
getTransactionContext(), 
ProxyContext.getInstance().getContextManager().getInstanceContext().getLockContext());
                 }
                 
connection.getConnectionSession().getTransactionStatus().setInTransaction(false);
-                
connection.getConnectionSession().getTransactionStatus().setExceptionOccur(false);
                 
connection.getConnectionSession().getConnectionContext().clearTransactionContext();
                 
connection.getConnectionSession().getConnectionContext().clearCursorContext();
             }
@@ -123,7 +122,6 @@ public final class BackendTransactionManager implements 
TransactionManager {
                     
each.afterRollback(connection.getCachedConnections().values(), 
getTransactionContext());
                 }
                 
connection.getConnectionSession().getTransactionStatus().setInTransaction(false);
-                
connection.getConnectionSession().getTransactionStatus().setExceptionOccur(false);
                 
connection.getConnectionSession().getConnectionContext().clearTransactionContext();
                 
connection.getConnectionSession().getConnectionContext().clearCursorContext();
             }
@@ -152,8 +150,8 @@ public final class BackendTransactionManager implements 
TransactionManager {
                 result.add(ex);
             }
         }
-        if (result.isEmpty() && 
connection.getConnectionSession().getTransactionStatus().isExceptionOccur()) {
-            
connection.getConnectionSession().getTransactionStatus().setExceptionOccur(false);
+        if (result.isEmpty() && getTransactionContext().isExceptionOccur()) {
+            getTransactionContext().setExceptionOccur(false);
         }
         throwSQLExceptionIfNecessary(result);
     }
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/LocalTransactionManager.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/LocalTransactionManager.java
index 2cfa9344634..8be77ba57bb 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/LocalTransactionManager.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/LocalTransactionManager.java
@@ -49,7 +49,7 @@ public final class LocalTransactionManager {
      */
     public void commit() throws SQLException {
         Collection<SQLException> exceptions = new LinkedList<>();
-        if 
(databaseConnectionManager.getConnectionSession().getTransactionStatus().isExceptionOccur())
 {
+        if 
(databaseConnectionManager.getConnectionSession().getConnectionContext().getTransactionContext().isExceptionOccur())
 {
             exceptions.addAll(rollbackConnections());
         } else {
             exceptions.addAll(commitConnections());
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
index 287108782b7..16e483db48f 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
@@ -154,7 +154,7 @@ public final class ProxyBackendHandlerFactory {
     }
     
     private static void allowExecutingWhenTransactionalError(final 
DatabaseType databaseType, final ConnectionSession connectionSession, final 
SQLStatement sqlStatement) throws SQLException {
-        if (!connectionSession.getTransactionStatus().isExceptionOccur()) {
+        if 
(!connectionSession.getConnectionContext().getTransactionContext().isExceptionOccur())
 {
             return;
         }
         Optional<TransactionalErrorAllowedSQLStatementHandler> 
allowedSQLStatementHandler = 
DatabaseTypedSPILoader.findService(TransactionalErrorAllowedSQLStatementHandler.class,
 databaseType);
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandler.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandler.java
index 666313ae8cf..a9806dac5ae 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandler.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandler.java
@@ -129,7 +129,7 @@ public final class TransactionBackendHandler implements 
ProxyBackendHandler {
     
     private SQLStatement getSQLStatementByCommit() {
         SQLStatement result = tclStatement;
-        if (connectionSession.getTransactionStatus().isExceptionOccur()) {
+        if 
(connectionSession.getConnectionContext().getTransactionContext().isExceptionOccur())
 {
             if (tclStatement instanceof OpenGaussCommitStatement) {
                 result = new OpenGaussRollbackStatement();
             } else if (tclStatement instanceof PostgreSQLCommitStatement) {
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/transaction/TransactionStatus.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/transaction/TransactionStatus.java
index 081e34188ea..aa369f6790d 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/transaction/TransactionStatus.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/transaction/TransactionStatus.java
@@ -30,8 +30,6 @@ public final class TransactionStatus {
     
     private volatile boolean inTransaction;
     
-    private volatile boolean exceptionOccur;
-    
     /**
      * Judge whether in connection held transaction.
      * 
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/LocalTransactionManagerTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/LocalTransactionManagerTest.java
index e3d88fd32ca..0cfea7ecc41 100644
--- 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/LocalTransactionManagerTest.java
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/LocalTransactionManagerTest.java
@@ -19,12 +19,14 @@ package 
org.apache.shardingsphere.proxy.backend.connector.jdbc.transaction;
 
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Multimap;
+import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
 import 
org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager;
 import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 import 
org.apache.shardingsphere.proxy.backend.session.transaction.TransactionStatus;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.mockito.junit.jupiter.MockitoSettings;
@@ -51,6 +53,9 @@ class LocalTransactionManagerTest {
     @Mock
     private TransactionStatus transactionStatus;
     
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private ConnectionContext connectionContext;
+    
     @Mock
     private Connection connection;
     
@@ -58,6 +63,7 @@ class LocalTransactionManagerTest {
     
     @BeforeEach
     void setUp() {
+        
when(connectionSession.getConnectionContext()).thenReturn(connectionContext);
         
when(connectionSession.getTransactionStatus()).thenReturn(transactionStatus);
         
when(databaseConnectionManager.getConnectionSession()).thenReturn(connectionSession);
         
when(databaseConnectionManager.getCachedConnections()).thenReturn(setCachedConnections());
@@ -82,7 +88,7 @@ class LocalTransactionManagerTest {
     @Test
     void assertCommit() throws SQLException {
         localTransactionManager.commit();
-        verify(transactionStatus).isExceptionOccur();
+        verify(connectionContext.getTransactionContext()).isExceptionOccur();
         verify(connection).commit();
     }
     
diff --git 
a/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
 
b/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
index cfe4e6067b0..cf96d2cea5a 100644
--- 
a/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
+++ 
b/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
@@ -49,8 +49,9 @@ public final class OpenGaussFrontendEngine implements 
DatabaseProtocolFrontendEn
     
     @Override
     public void handleException(final ConnectionSession connectionSession, 
final Exception exception) {
-        if (connectionSession.getTransactionStatus().isInTransaction() && 
!connectionSession.getTransactionStatus().isExceptionOccur() && !(exception 
instanceof InTransactionException)) {
-            connectionSession.getTransactionStatus().setExceptionOccur(true);
+        if (connectionSession.getTransactionStatus().isInTransaction() && 
!connectionSession.getConnectionContext().getTransactionContext().isExceptionOccur()
+                && !(exception instanceof InTransactionException)) {
+            
connectionSession.getConnectionContext().getTransactionContext().setExceptionOccur(true);
         }
     }
     
diff --git 
a/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
 
b/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
index b6855cd121e..a8e9f7edae1 100644
--- 
a/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
+++ 
b/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
@@ -47,8 +47,9 @@ public final class PostgreSQLFrontendEngine implements 
DatabaseProtocolFrontendE
     
     @Override
     public void handleException(final ConnectionSession connectionSession, 
final Exception exception) {
-        if (connectionSession.getTransactionStatus().isInTransaction() && 
!connectionSession.getTransactionStatus().isExceptionOccur() && !(exception 
instanceof InTransactionException)) {
-            connectionSession.getTransactionStatus().setExceptionOccur(true);
+        if (connectionSession.getTransactionStatus().isInTransaction() && 
!connectionSession.getConnectionContext().getTransactionContext().isExceptionOccur()
+                && !(exception instanceof InTransactionException)) {
+            
connectionSession.getConnectionContext().getTransactionContext().setExceptionOccur(true);
         }
     }
     

Reply via email to