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 4c07e394c52 Use same transaction manager in one transaction (#34206) 4c07e394c52 is described below commit 4c07e394c528e47d8113e8e1a539e1acc781a728 Author: ZhangCheng <chengzh...@apache.org> AuthorDate: Mon Dec 30 12:24:58 2024 +0800 Use same transaction manager in one transaction (#34206) * Use same transaction manager in one transaction * Use same transaction manager in one transaction --- .../connector/jdbc/transaction/BackendTransactionManager.java | 7 ++++++- .../handler/transaction/TransactionBackendHandlerFactoryTest.java | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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 53bad50acec..5c2331ebba1 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 @@ -60,9 +60,14 @@ public final class BackendTransactionManager implements TransactionManager { connection = databaseConnectionManager; localTransactionManager = new LocalTransactionManager(databaseConnectionManager); TransactionRule transactionRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TransactionRule.class); + TransactionConnectionContext transactionContext = getTransactionContext(); transactionType = transactionRule.getDefaultType(); ShardingSphereTransactionManagerEngine engine = transactionRule.getResource(); - distributedTransactionManager = null == engine ? null : engine.getTransactionManager(transactionType); + if (transactionContext.getTransactionManager().isPresent()) { + distributedTransactionManager = (ShardingSphereDistributedTransactionManager) transactionContext.getTransactionManager().get(); + } else { + distributedTransactionManager = null == engine ? null : engine.getTransactionManager(transactionType); + } transactionHooks = OrderedSPILoader.getServices(TransactionHook.class, ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules()); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerFactoryTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerFactoryTest.java index e59ff3c652a..b325704dab8 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerFactoryTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerFactoryTest.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementCont import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.session.connection.ConnectionContext; +import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext; import org.apache.shardingsphere.infra.session.query.QueryContext; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnector; @@ -66,6 +67,8 @@ class TransactionBackendHandlerFactoryTest { ConnectionSession connectionSession = mock(ConnectionSession.class, Answers.RETURNS_DEEP_STUBS); ProxyDatabaseConnectionManager databaseConnectionManager = mock(ProxyDatabaseConnectionManager.class); when(connectionSession.getDatabaseConnectionManager()).thenReturn(databaseConnectionManager); + when(databaseConnectionManager.getConnectionSession()).thenReturn(connectionSession); + when(databaseConnectionManager.getConnectionSession().getConnectionContext().getTransactionContext()).thenReturn(new TransactionConnectionContext()); SQLStatementContext context = mock(SQLStatementContext.class); when(context.getSqlStatement()).thenReturn(mock(CommitStatement.class)); ContextManager contextManager = mockContextManager(); @@ -82,6 +85,9 @@ class TransactionBackendHandlerFactoryTest { ConnectionSession connectionSession = mock(ConnectionSession.class, Answers.RETURNS_DEEP_STUBS); ProxyDatabaseConnectionManager databaseConnectionManager = mock(ProxyDatabaseConnectionManager.class); when(connectionSession.getDatabaseConnectionManager()).thenReturn(databaseConnectionManager); + when(databaseConnectionManager.getConnectionSession()).thenReturn(connectionSession); + when(databaseConnectionManager.getConnectionSession().getConnectionContext().getTransactionContext()).thenReturn(new TransactionConnectionContext()); + when(connectionSession.getDatabaseConnectionManager().getConnectionSession().getConnectionContext().getTransactionContext()).thenReturn(new TransactionConnectionContext()); SQLStatementContext context = mock(SQLStatementContext.class); when(context.getSqlStatement()).thenReturn(mock(RollbackStatement.class)); ContextManager contextManager = mockContextManager();