You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/05/21 08:14:48 UTC

[shardingsphere] branch master updated: Fix sonar issue on TransactionXAHandler (#25820)

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 9b84722fe17 Fix sonar issue on TransactionXAHandler (#25820)
9b84722fe17 is described below

commit 9b84722fe1795b435effe1a4087991b90ad67d78
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun May 21 16:14:39 2023 +0800

    Fix sonar issue on TransactionXAHandler (#25820)
---
 .../handler/transaction/TransactionXAHandler.java  | 48 +++++++++++++---------
 1 file changed, 28 insertions(+), 20 deletions(-)

diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionXAHandler.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionXAHandler.java
index ca61a09a64d..7db34e8564c 100644
--- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionXAHandler.java
+++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionXAHandler.java
@@ -36,8 +36,8 @@ import java.util.Collections;
 
 /**
  * XA transaction handler.
- * TODO Currently XA transaction started with `XA START` doesn't support for database with multiple datasource, a flag should be added for this both in init progress and add datasource from DistSQL.
  */
+// TODO Currently XA transaction started with `XA START` doesn't support for database with multiple datasource, a flag should be added for this both in init progress and add datasource from DistSQL.
 @RequiredArgsConstructor
 public final class TransactionXAHandler implements ProxyBackendHandler {
     
@@ -48,20 +48,20 @@ public final class TransactionXAHandler implements ProxyBackendHandler {
     private final DatabaseConnector backendHandler;
     
     public TransactionXAHandler(final SQLStatementContext sqlStatementContext, final String sql, final ConnectionSession connectionSession) {
-        this.tclStatement = (XAStatement) sqlStatementContext.getSqlStatement();
+        tclStatement = (XAStatement) sqlStatementContext.getSqlStatement();
         this.connectionSession = connectionSession;
-        QueryContext queryContext = new QueryContext(sqlStatementContext, sql, Collections.emptyList());
-        backendHandler = DatabaseConnectorFactory.getInstance().newInstance(queryContext, connectionSession.getDatabaseConnectionManager(), false);
+        backendHandler = DatabaseConnectorFactory.getInstance().newInstance(
+                new QueryContext(sqlStatementContext, sql, Collections.emptyList()), connectionSession.getDatabaseConnectionManager(), false);
     }
     
     @Override
     public boolean next() throws SQLException {
-        return "RECOVER".equals(this.tclStatement.getOp()) && backendHandler.next();
+        return "RECOVER".equals(tclStatement.getOp()) && backendHandler.next();
     }
     
     @Override
     public QueryResponseRow getRowData() throws SQLException {
-        return "RECOVER".equals(this.tclStatement.getOp()) ? backendHandler.getRowData() : new QueryResponseRow(Collections.emptyList());
+        return "RECOVER".equals(tclStatement.getOp()) ? backendHandler.getRowData() : new QueryResponseRow(Collections.emptyList());
     }
     
     @Override
@@ -69,28 +69,36 @@ public final class TransactionXAHandler implements ProxyBackendHandler {
         switch (tclStatement.getOp()) {
             case "START":
             case "BEGIN":
-                /*
-                 * we have to let session occupy the thread when doing xa transaction. according to https://dev.mysql.com/doc/refman/5.7/en/xa-states.html XA and local transactions are mutually
-                 * exclusive
-                 */
-                ShardingSpherePreconditions.checkState(!connectionSession.getTransactionStatus().isInTransaction(), XATransactionNestedBeginException::new);
-                ResponseHeader header = backendHandler.execute();
-                connectionSession.getConnectionContext().getTransactionContext().setInTransaction(true);
-                return header;
+                return begin();
             case "END":
             case "PREPARE":
             case "RECOVER":
                 return backendHandler.execute();
             case "COMMIT":
             case "ROLLBACK":
-                try {
-                    return backendHandler.execute();
-                } finally {
-                    connectionSession.getConnectionContext().clearTransactionConnectionContext();
-                    connectionSession.getConnectionContext().clearCursorConnectionContext();
-                }
+                return finish();
             default:
                 throw new SQLFeatureNotSupportedException(String.format("unrecognized XA statement `%s`", tclStatement.getOp()));
         }
     }
+    
+    /*
+     * We have to let session occupy the thread when doing xa transaction.
+     * According to https://dev.mysql.com/doc/refman/5.7/en/xa-states.html XA and local transactions are mutually exclusive.
+     */
+    private ResponseHeader begin() throws SQLException {
+        ShardingSpherePreconditions.checkState(!connectionSession.getTransactionStatus().isInTransaction(), XATransactionNestedBeginException::new);
+        ResponseHeader result = backendHandler.execute();
+        connectionSession.getConnectionContext().getTransactionContext().setInTransaction(true);
+        return result;
+    }
+    
+    private ResponseHeader finish() throws SQLException {
+        try {
+            return backendHandler.execute();
+        } finally {
+            connectionSession.getConnectionContext().clearTransactionConnectionContext();
+            connectionSession.getConnectionContext().clearCursorConnectionContext();
+        }
+    }
 }