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 55fd43b8875 Split XAProxyBackendHandler (#36793)
55fd43b8875 is described below
commit 55fd43b8875ba7fd7421b082e77a734e4a794c5b
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Oct 5 20:48:27 2025 +0800
Split XAProxyBackendHandler (#36793)
---
.../handler/tcl/TCLProxyBackendHandlerFactory.java | 30 +++++++++--
...andler.java => XABeginProxyBackendHandler.java} | 60 +++-------------------
.../tcl/xa/XACommitProxyBackendHandler.java | 47 +++++++++++++++++
.../xa/XAOtherOperationProxyBackendHandler.java | 39 ++++++++++++++
.../tcl/xa/XARecoveryProxyBackendHandler.java | 50 ++++++++++++++++++
.../tcl/xa/XARollbackProxyBackendHandler.java | 47 +++++++++++++++++
6 files changed, 215 insertions(+), 58 deletions(-)
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/TCLProxyBackendHandlerFactory.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/TCLProxyBackendHandlerFactory.java
index 76056287945..5a75dccbacb 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/TCLProxyBackendHandlerFactory.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/TCLProxyBackendHandlerFactory.java
@@ -22,6 +22,7 @@ import lombok.NoArgsConstructor;
import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.session.query.QueryContext;
+import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnector;
import
org.apache.shardingsphere.proxy.backend.connector.DatabaseConnectorFactory;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
@@ -33,7 +34,11 @@ import
org.apache.shardingsphere.proxy.backend.handler.tcl.local.RollbackSavepoi
import
org.apache.shardingsphere.proxy.backend.handler.tcl.local.SetAutoCommitProxyBackendHandler;
import
org.apache.shardingsphere.proxy.backend.handler.tcl.local.SetSavepointProxyBackendHandler;
import
org.apache.shardingsphere.proxy.backend.handler.tcl.local.SetTransactionProxyBackendHandler;
-import
org.apache.shardingsphere.proxy.backend.handler.tcl.xa.XAProxyBackendHandler;
+import
org.apache.shardingsphere.proxy.backend.handler.tcl.xa.XABeginProxyBackendHandler;
+import
org.apache.shardingsphere.proxy.backend.handler.tcl.xa.XACommitProxyBackendHandler;
+import
org.apache.shardingsphere.proxy.backend.handler.tcl.xa.XAOtherOperationProxyBackendHandler;
+import
org.apache.shardingsphere.proxy.backend.handler.tcl.xa.XARecoveryProxyBackendHandler;
+import
org.apache.shardingsphere.proxy.backend.handler.tcl.xa.XARollbackProxyBackendHandler;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import
org.apache.shardingsphere.sql.parser.statement.core.enums.OperationScope;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.BeginTransactionStatement;
@@ -44,6 +49,10 @@ import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.Sa
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.SetAutoCommitStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.SetTransactionStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.TCLStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XABeginStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XACommitStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XARecoveryStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XARollbackStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XAStatement;
import java.util.Collections;
@@ -87,11 +96,24 @@ public final class TCLProxyBackendHandlerFactory {
if (sqlStatement instanceof SetTransactionStatement &&
!((SetTransactionStatement)
sqlStatement).isDesiredScope(OperationScope.GLOBAL)) {
return new
SetTransactionProxyBackendHandler((SetTransactionStatement) sqlStatement,
connectionSession);
}
+ QueryContext queryContext = new QueryContext(sqlStatementContext, sql,
+ Collections.emptyList(), new HintValueContext(),
connectionSession.getConnectionContext(),
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData());
+ DatabaseConnector databaseConnector =
DatabaseConnectorFactory.getInstance().newInstance(queryContext,
connectionSession.getDatabaseConnectionManager(), false);
+ if (sqlStatement instanceof XARecoveryStatement) {
+ return new XARecoveryProxyBackendHandler(databaseConnector);
+ }
+ if (sqlStatement instanceof XABeginStatement) {
+ return new XABeginProxyBackendHandler(connectionSession,
databaseConnector);
+ }
+ if (sqlStatement instanceof XACommitStatement) {
+ return new XACommitProxyBackendHandler(connectionSession,
databaseConnector);
+ }
+ if (sqlStatement instanceof XARollbackStatement) {
+ return new XARollbackProxyBackendHandler(connectionSession,
databaseConnector);
+ }
if (sqlStatement instanceof XAStatement) {
- return new XAProxyBackendHandler(sqlStatementContext, sql,
connectionSession);
+ return new XAOtherOperationProxyBackendHandler(databaseConnector);
}
- QueryContext queryContext = new QueryContext(sqlStatementContext, sql,
Collections.emptyList(), new HintValueContext(),
connectionSession.getConnectionContext(),
-
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData());
return
DatabaseConnectorFactory.getInstance().newInstance(queryContext,
connectionSession.getDatabaseConnectionManager(), false);
}
}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XAProxyBackendHandler.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XABeginProxyBackendHandler.java
similarity index 50%
rename from
proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XAProxyBackendHandler.java
rename to
proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XABeginProxyBackendHandler.java
index d25d3411b3d..48d10afcba9 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XAProxyBackendHandler.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XABeginProxyBackendHandler.java
@@ -17,74 +17,35 @@
package org.apache.shardingsphere.proxy.backend.handler.tcl.xa;
-import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
-import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnector;
-import
org.apache.shardingsphere.proxy.backend.connector.DatabaseConnectorFactory;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
-import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseRow;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XABeginStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XACommitStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XARecoveryStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XARollbackStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XAStatement;
import
org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import
org.apache.shardingsphere.transaction.xa.jta.exception.XATransactionNestedBeginException;
import java.sql.SQLException;
-import java.util.Collections;
/**
- * XA proxy backend handler.
+ * XA begin proxy backend 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.
-public final class XAProxyBackendHandler implements ProxyBackendHandler {
-
- private final XAStatement sqlStatement;
+@RequiredArgsConstructor
+public final class XABeginProxyBackendHandler implements ProxyBackendHandler {
private final ConnectionSession connectionSession;
private final DatabaseConnector databaseConnector;
- public XAProxyBackendHandler(final SQLStatementContext
sqlStatementContext, final String sql, final ConnectionSession
connectionSession) {
- sqlStatement = (XAStatement) sqlStatementContext.getSqlStatement();
- this.connectionSession = connectionSession;
- QueryContext queryContext = new QueryContext(sqlStatementContext, sql,
- Collections.emptyList(), new HintValueContext(),
connectionSession.getConnectionContext(),
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData());
- databaseConnector =
DatabaseConnectorFactory.getInstance().newInstance(queryContext,
connectionSession.getDatabaseConnectionManager(), false);
- }
-
- @Override
- public boolean next() throws SQLException {
- return sqlStatement instanceof XARecoveryStatement &&
databaseConnector.next();
- }
-
- @Override
- public QueryResponseRow getRowData() throws SQLException {
- return sqlStatement instanceof XARecoveryStatement ?
databaseConnector.getRowData() : new QueryResponseRow(Collections.emptyList());
- }
-
- @Override
- public ResponseHeader execute() throws SQLException {
- if (sqlStatement instanceof XABeginStatement) {
- return begin();
- }
- if (sqlStatement instanceof XACommitStatement || sqlStatement
instanceof XARollbackStatement) {
- return finish();
- }
- return databaseConnector.execute();
- }
-
/*
* 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 {
+ @Override
+ public ResponseHeader execute() throws SQLException {
ShardingSpherePreconditions.checkState(!connectionSession.getTransactionStatus().isInTransaction(),
XATransactionNestedBeginException::new);
ResponseHeader result = databaseConnector.execute();
TransactionRule transactionRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TransactionRule.class);
@@ -92,13 +53,4 @@ public final class XAProxyBackendHandler implements
ProxyBackendHandler {
connectionSession.getConnectionContext().getTransactionContext().beginTransaction(transactionRule.getDefaultType().name(),
engine.getTransactionManager(transactionRule.getDefaultType()));
return result;
}
-
- private ResponseHeader finish() throws SQLException {
- try {
- return databaseConnector.execute();
- } finally {
- connectionSession.getConnectionContext().clearTransactionContext();
- connectionSession.getConnectionContext().clearCursorContext();
- }
- }
}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XACommitProxyBackendHandler.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XACommitProxyBackendHandler.java
new file mode 100644
index 00000000000..ca31f2fb18f
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XACommitProxyBackendHandler.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.handler.tcl.xa;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnector;
+import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+
+import java.sql.SQLException;
+
+/**
+ * XA commit proxy backend handler.
+ */
+@RequiredArgsConstructor
+public final class XACommitProxyBackendHandler implements ProxyBackendHandler {
+
+ private final ConnectionSession connectionSession;
+
+ private final DatabaseConnector databaseConnector;
+
+ @Override
+ public ResponseHeader execute() throws SQLException {
+ try {
+ return databaseConnector.execute();
+ } finally {
+ connectionSession.getConnectionContext().clearTransactionContext();
+ connectionSession.getConnectionContext().clearCursorContext();
+ }
+ }
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XAOtherOperationProxyBackendHandler.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XAOtherOperationProxyBackendHandler.java
new file mode 100644
index 00000000000..f531ce14681
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XAOtherOperationProxyBackendHandler.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.handler.tcl.xa;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnector;
+import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+
+import java.sql.SQLException;
+
+/**
+ * XA other operation proxy backend handler.
+ */
+@RequiredArgsConstructor
+public final class XAOtherOperationProxyBackendHandler implements
ProxyBackendHandler {
+
+ private final DatabaseConnector databaseConnector;
+
+ @Override
+ public ResponseHeader execute() throws SQLException {
+ return databaseConnector.execute();
+ }
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XARecoveryProxyBackendHandler.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XARecoveryProxyBackendHandler.java
new file mode 100644
index 00000000000..eec8178d535
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XARecoveryProxyBackendHandler.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.handler.tcl.xa;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnector;
+import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
+import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseRow;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+
+import java.sql.SQLException;
+
+/**
+ * XA recovery proxy backend handler.
+ */
+@RequiredArgsConstructor
+public final class XARecoveryProxyBackendHandler implements
ProxyBackendHandler {
+
+ private final DatabaseConnector databaseConnector;
+
+ @Override
+ public boolean next() throws SQLException {
+ return databaseConnector.next();
+ }
+
+ @Override
+ public QueryResponseRow getRowData() throws SQLException {
+ return databaseConnector.getRowData();
+ }
+
+ @Override
+ public ResponseHeader execute() throws SQLException {
+ return databaseConnector.execute();
+ }
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XARollbackProxyBackendHandler.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XARollbackProxyBackendHandler.java
new file mode 100644
index 00000000000..f43ae271368
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XARollbackProxyBackendHandler.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.handler.tcl.xa;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnector;
+import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+
+import java.sql.SQLException;
+
+/**
+ * XA rollback proxy backend handler.
+ */
+@RequiredArgsConstructor
+public final class XARollbackProxyBackendHandler implements
ProxyBackendHandler {
+
+ private final ConnectionSession connectionSession;
+
+ private final DatabaseConnector databaseConnector;
+
+ @Override
+ public ResponseHeader execute() throws SQLException {
+ try {
+ return databaseConnector.execute();
+ } finally {
+ connectionSession.getConnectionContext().clearTransactionContext();
+ connectionSession.getConnectionContext().clearCursorContext();
+ }
+ }
+}