Skip to content

Commit

Permalink
Remove TransactionTypeHolder(apache#25674) (apache#25696)
Browse files Browse the repository at this point in the history
* Remove `TransactionTypeHolder`(apache#25674)
  • Loading branch information
FlyingZC authored May 16, 2023
1 parent 56dc176 commit f0f5062
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import org.apache.shardingsphere.driver.jdbc.adapter.executor.ForceExecuteTemplate;
import org.apache.shardingsphere.driver.jdbc.adapter.invocation.MethodInvocationRecorder;
import org.apache.shardingsphere.driver.jdbc.core.ShardingSphereSavepoint;
import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext;
import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
import org.apache.shardingsphere.infra.exception.OverallConnectionNotEnoughException;
Expand All @@ -36,13 +34,13 @@
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext;
import org.apache.shardingsphere.metadata.persist.MetaDataBasedPersistService;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.transaction.ConnectionSavepointManager;
import org.apache.shardingsphere.transaction.ConnectionTransaction;
import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.transaction.rule.TransactionRule;

import javax.sql.DataSource;
Expand Down Expand Up @@ -132,9 +130,8 @@ private String createJdbcUrl(final ProxyInstanceMetaData instanceMetaData, final
}

private ConnectionTransaction createConnectionTransaction(final String databaseName, final ContextManager contextManager) {
TransactionType type = TransactionTypeHolder.get();
TransactionRule rule = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TransactionRule.class);
return null == type ? new ConnectionTransaction(databaseName, rule) : new ConnectionTransaction(databaseName, type, rule);
return new ConnectionTransaction(databaseName, rule);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.traffic.rule.builder.DefaultTrafficRuleConfigurationBuilder;
import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -64,6 +63,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -90,7 +90,6 @@ class BatchPreparedStatementExecutorTest {
@BeforeEach
void setUp() {
SQLExecutorExceptionHandler.setExceptionThrown(true);
TransactionTypeHolder.set(TransactionType.LOCAL);
ShardingSphereConnection connection = new ShardingSphereConnection("foo_db", mockContextManager(), mock(JDBCContext.class));
executor = new BatchPreparedStatementExecutor(
connection.getContextManager().getMetaDataContexts(), new JDBCExecutor(executorEngine, connection.getDatabaseConnectionManager().getConnectionContext()), "foo_db");
Expand All @@ -117,9 +116,7 @@ private MetaDataContexts mockMetaDataContexts() {
}

private TransactionRule mockTransactionRule() {
TransactionRule result = mock(TransactionRule.class);
when(result.getResource()).thenReturn(new ShardingSphereTransactionManagerEngine(TransactionType.LOCAL));
return result;
return new TransactionRule(new TransactionRuleConfiguration(TransactionType.LOCAL.name(), "", new Properties()), Collections.emptyMap());
}

private ShardingRule mockShardingRule() {
Expand All @@ -139,7 +136,6 @@ private Map<String, DataSource> mockDataSourceMap() {
@AfterEach
void tearDown() {
executorEngine.close();
TransactionTypeHolder.clear();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.traffic.rule.builder.DefaultTrafficRuleConfigurationBuilder;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
Expand All @@ -44,11 +42,6 @@

class ConnectionAdapterTest {

@AfterEach
void tearDown() {
TransactionTypeHolder.clear();
}

@Test
void assertGetWarnings() throws SQLException {
try (Connection actual = createConnectionAdaptor()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@
import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -151,16 +149,6 @@ void assertGetConnectionWhenConfigTrafficRule() throws SQLException {
assertThat(actual.get(0).getMetaData().getURL(), is("jdbc:mysql://127.0.0.1:3307/logic_db?serverTimezone=UTC&useSSL=false"));
}

@Test
void assertGetConnectionWhenConfigTrafficRuleInXaTransaction() throws SQLException {
TransactionTypeHolder.set(TransactionType.XA);
List<Connection> actual = databaseConnectionManager.getConnections("127.0.0.1@3307", 1, ConnectionMode.MEMORY_STRICTLY);
assertThat(actual.size(), is(1));
assertThat(actual.get(0).getMetaData().getUserName(), is("root"));
assertThat(actual.get(0).getMetaData().getURL(), is("jdbc:mysql://127.0.0.1:3307/logic_db?serverTimezone=UTC&useSSL=false"));
TransactionTypeHolder.clear();
}

@Test
void assertGetConnectionsWhenAllInCache() throws SQLException {
Connection expected = databaseConnectionManager.getConnections("ds", 1, ConnectionMode.MEMORY_STRICTLY).get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@

import lombok.SneakyThrows;
import org.apache.shardingsphere.driver.jdbc.context.JDBCContext;
import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
import org.apache.shardingsphere.infra.database.DefaultDatabase;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.transaction.ConnectionTransaction;
import org.apache.shardingsphere.transaction.ConnectionTransaction.DistributedTransactionOperationType;
import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -40,6 +40,7 @@
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -57,23 +58,25 @@ class ShardingSphereConnectionTest {

@BeforeEach
void setUp() {
TransactionTypeHolder.set(TransactionType.LOCAL);
connection = new ShardingSphereConnection(DefaultDatabase.LOGIC_NAME, mockContextManager(), mock(JDBCContext.class));
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(result.getDataSourceMap(DefaultDatabase.LOGIC_NAME)).thenReturn(Collections.singletonMap("ds", mock(DataSource.class, RETURNS_DEEP_STUBS)));
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData())
.thenReturn(new ShardingSphereRuleMetaData(Arrays.asList(mock(TransactionRule.class, RETURNS_DEEP_STUBS), mock(TrafficRule.class))));
.thenReturn(new ShardingSphereRuleMetaData(Arrays.asList(mockTransactionRule(), mock(TrafficRule.class))));
return result;
}

private TransactionRule mockTransactionRule() {
return new TransactionRule(new TransactionRuleConfiguration(TransactionType.LOCAL.name(), "", new Properties()), Collections.emptyMap());
}

@AfterEach
void clear() {
try {
connection.close();
TransactionTypeHolder.clear();
} catch (final SQLException ignored) {
}
}
Expand Down Expand Up @@ -104,6 +107,7 @@ void assertSetAutoCommitWithLocalTransaction() throws SQLException {
void assertSetAutoCommitWithDistributedTransaction() throws SQLException {
ConnectionTransaction connectionTransaction = mock(ConnectionTransaction.class);
when(connectionTransaction.getDistributedTransactionOperationType(true)).thenReturn(DistributedTransactionOperationType.COMMIT);
when(connectionTransaction.getTransactionType()).thenReturn(TransactionType.XA);
mockConnectionManager(connectionTransaction);
connection.setAutoCommit(true);
assertTrue(connection.getAutoCommit());
Expand All @@ -128,6 +132,7 @@ void assertCommitWithLocalTransaction() throws SQLException {
void assertCommitWithDistributedTransaction() throws SQLException {
ConnectionTransaction connectionTransaction = mock(ConnectionTransaction.class);
when(connectionTransaction.getDistributedTransactionOperationType(false)).thenReturn(DistributedTransactionOperationType.BEGIN);
when(connectionTransaction.getTransactionType()).thenReturn(TransactionType.XA);
DriverDatabaseConnectionManager databaseConnectionManager = mockConnectionManager(connectionTransaction);
connection.setAutoCommit(false);
assertTrue(databaseConnectionManager.getConnectionContext().getTransactionContext().isInTransaction());
Expand All @@ -154,6 +159,7 @@ void assertRollbackWithLocalTransaction() throws SQLException {
void assertRollbackWithDistributedTransaction() throws SQLException {
ConnectionTransaction connectionTransaction = mock(ConnectionTransaction.class);
when(connectionTransaction.getDistributedTransactionOperationType(false)).thenReturn(DistributedTransactionOperationType.BEGIN);
when(connectionTransaction.getTransactionType()).thenReturn(TransactionType.XA);
final DriverDatabaseConnectionManager databaseConnectionManager = mockConnectionManager(connectionTransaction);
connection.setAutoCommit(false);
assertFalse(connection.getAutoCommit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.mockito.internal.configuration.plugins.Plugins;

Expand All @@ -53,11 +51,6 @@

class ShardingSphereDataSourceTest {

@AfterEach
void tearDown() {
TransactionTypeHolder.set(null);
}

@Test
void assertNewConstructorWithModeConfigurationOnly() throws Exception {
try (ShardingSphereDataSource actual = new ShardingSphereDataSource(DefaultDatabase.LOGIC_NAME, null)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import org.apache.shardingsphere.infra.util.exception.external.sql.type.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.sql.SQLFeatureNotSupportedException;
Expand All @@ -48,12 +46,7 @@ class UnsupportedOperationConnectionTest {
new ShardingSphereRuleMetaData(Arrays.asList(mock(TransactionRule.class, RETURNS_DEEP_STUBS), mock(TrafficRule.class))));
shardingSphereConnection = new ShardingSphereConnection(DefaultDatabase.LOGIC_NAME, contextManager, mock(JDBCContext.class));
}

@AfterEach
void tearDown() {
TransactionTypeHolder.clear();
}


@Test
void assertPrepareCall() {
assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereConnection.prepareCall(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.state.cluster.ClusterStateContext;
import org.apache.shardingsphere.infra.state.instance.InstanceStateContext;
import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -83,9 +81,4 @@ void assertGetConnectionWithOkState() {
Connection actual = DriverStateContext.getConnection(DefaultDatabase.LOGIC_NAME, contextManager, mock(JDBCContext.class));
assertThat(actual, instanceOf(ShardingSphereConnection.class));
}

@AfterEach
void tearDown() {
TransactionTypeHolder.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
Expand All @@ -39,11 +37,6 @@

class OKDriverStateTest {

@AfterEach
void tearDown() {
TransactionTypeHolder.clear();
}

@Test
void assertGetConnection() {
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import lombok.Setter;
import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext;
import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager;

Expand Down Expand Up @@ -53,7 +52,6 @@ public ConnectionTransaction(final String databaseName, final TransactionType tr
this.databaseName = databaseName;
this.transactionType = transactionType;
transactionManager = rule.getResource().getTransactionManager(transactionType);
TransactionTypeHolder.set(transactionType);
}

/**
Expand Down

This file was deleted.

Loading

0 comments on commit f0f5062

Please sign in to comment.