roodkcab commented on a change in pull request #14130: URL: https://github.com/apache/shardingsphere/pull/14130#discussion_r771772218
########## File path: shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/transaction/TransactionXAHandler.java ########## @@ -0,0 +1,105 @@ +/* + * 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.text.transaction; + +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext; +import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.JDBCBackendConnection; +import org.apache.shardingsphere.proxy.backend.communication.jdbc.transaction.BackendTransactionManager; +import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader; +import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; +import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler; +import org.apache.shardingsphere.proxy.backend.text.data.impl.SchemaAssignedDatabaseBackendHandler; +import org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.TCLStatement; +import org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.XAStatement; +import org.apache.shardingsphere.transaction.TransactionHolder; +import org.apache.shardingsphere.transaction.core.TransactionType; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.Collections; + +/** + * XA transaction handler. + */ +@RequiredArgsConstructor +public final class TransactionXAHandler implements TextProtocolBackendHandler { + + private final XAStatement tclStatement; + + private final ConnectionSession connectionSession; + + private final SchemaAssignedDatabaseBackendHandler backendHandler; + + public TransactionXAHandler(final SQLStatementContext<? extends TCLStatement> sqlStatementContext, final String sql, final ConnectionSession connectionSession) { + this.tclStatement = (XAStatement) sqlStatementContext.getSqlStatement(); + this.connectionSession = connectionSession; + this.backendHandler = new SchemaAssignedDatabaseBackendHandler(sqlStatementContext, sql, connectionSession); + } + + @Override + public boolean next() throws SQLException { + return this.tclStatement.getOp().equals("RECOVER") && this.backendHandler.next(); + } + + @Override + public Collection<Object> getRowData() throws SQLException { + return this.tclStatement.getOp().equals("RECOVER") ? this.backendHandler.getRowData() : Collections.emptyList(); + } + + @Override + public ResponseHeader execute() throws SQLException { + 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 + */ + if (connectionSession.getTransactionStatus().isInTransaction()) { + if (TransactionType.MANUALXA != connectionSession.getTransactionStatus().getTransactionType()) { + BackendTransactionManager backendTransactionManager = new BackendTransactionManager((JDBCBackendConnection) connectionSession.getBackendConnection()); + backendTransactionManager.commit(); + } else { + throw new SQLException("already in XA transaction"); + } + } + TransactionHolder.setInTransaction(); + connectionSession.setAutoCommit(false); + connectionSession.getTransactionStatus().setManualXA(true); + JDBCBackendConnection connection = (JDBCBackendConnection) connectionSession.getBackendConnection(); + connection.closeDatabaseCommunicationEngines(true); + connection.closeConnections(false); + return backendHandler.execute(); Review comment: yes, you are right. this handler is different than JTA, and shouldn't close connection with backend database -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
