diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouter.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouter.java index aed1481843d3f..022447575d2a1 100644 --- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouter.java +++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouter.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.infra.session.ConnectionContext; import java.util.Collection; import java.util.Collections; @@ -40,7 +41,8 @@ public final class DatabaseDiscoverySQLRouter implements SQLRouter { @Override - public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final DatabaseDiscoveryRule rule, final ConfigurationProperties props) { + public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, + final DatabaseDiscoveryRule rule, final ConfigurationProperties props, final ConnectionContext connectionContext) { RouteContext result = new RouteContext(); DatabaseDiscoveryDataSourceRule singleDataSourceRule = rule.getSingleDataSourceRule(); String dataSourceName = new DatabaseDiscoveryDataSourceRouter(singleDataSourceRule).route(); @@ -50,7 +52,8 @@ public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSp @Override public void decorateRouteContext(final RouteContext routeContext, - final LogicSQL logicSQL, final ShardingSphereDatabase database, final DatabaseDiscoveryRule rule, final ConfigurationProperties props) { + final LogicSQL logicSQL, final ShardingSphereDatabase database, final DatabaseDiscoveryRule rule, + final ConfigurationProperties props, final ConnectionContext connectionContext) { Collection toBeRemoved = new LinkedList<>(); Collection toBeAdded = new LinkedList<>(); for (RouteUnit each : routeContext.getRouteUnits()) { diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java index 2add1136a70d6..1c96fff713d27 100644 --- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java +++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java @@ -37,6 +37,7 @@ import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.schedule.core.ScheduleContextFactory; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; import org.apache.shardingsphere.test.mock.MockedDataSource; @@ -98,7 +99,7 @@ public void assertCreateRouteContextToPrimaryWithoutRouteUnits() { LogicSQL logicSQL = new LogicSQL(mock(SQLStatementContext.class), "", Collections.emptyList()); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), new ShardingSphereRuleMetaData(Collections.singleton(rule)), Collections.emptyMap()); - RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(PRIMARY_DATA_SOURCE)); } @@ -109,7 +110,7 @@ public void assertDecorateRouteContextToPrimaryDataSource() { LogicSQL logicSQL = new LogicSQL(mock(SQLStatementContext.class), "", Collections.emptyList()); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), new ShardingSphereRuleMetaData(Collections.singleton(rule)), Collections.emptyMap()); - sqlRouter.decorateRouteContext(actual, logicSQL, database, rule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(actual, logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(NONE_DB_DISCOVERY_DATA_SOURCE_NAME)); assertThat(routedDataSourceNames.next(), is(PRIMARY_DATA_SOURCE)); @@ -120,7 +121,7 @@ public void assertCreateRouteContextToPrimaryDataSourceWithLock() { LogicSQL logicSQL = new LogicSQL(sqlStatementContext, "", Collections.emptyList()); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), new ShardingSphereRuleMetaData(Collections.singleton(rule)), Collections.emptyMap()); - RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(PRIMARY_DATA_SOURCE)); } @@ -131,7 +132,7 @@ public void assertDecorateRouteContextToPrimaryDataSourceWithLock() { LogicSQL logicSQL = new LogicSQL(sqlStatementContext, "", Collections.emptyList()); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), new ShardingSphereRuleMetaData(Collections.singleton(rule)), Collections.emptyMap()); - sqlRouter.decorateRouteContext(actual, logicSQL, database, rule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(actual, logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(NONE_DB_DISCOVERY_DATA_SOURCE_NAME)); assertThat(routedDataSourceNames.next(), is(PRIMARY_DATA_SOURCE)); @@ -142,7 +143,7 @@ public void assertCreateRouteContextToPrimaryDataSource() { LogicSQL logicSQL = new LogicSQL(sqlStatementContext, "", Collections.emptyList()); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), new ShardingSphereRuleMetaData(Collections.singleton(rule)), Collections.emptyMap()); - RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(PRIMARY_DATA_SOURCE)); } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-api/src/main/java/org/apache/shardingsphere/readwritesplitting/spi/ReadQueryLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-api/src/main/java/org/apache/shardingsphere/readwritesplitting/spi/ReadQueryLoadBalanceAlgorithm.java index dab0efe59ac55..4b78dea5d8fa4 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-api/src/main/java/org/apache/shardingsphere/readwritesplitting/spi/ReadQueryLoadBalanceAlgorithm.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-api/src/main/java/org/apache/shardingsphere/readwritesplitting/spi/ReadQueryLoadBalanceAlgorithm.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.readwritesplitting.spi; import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.infra.util.spi.type.required.RequiredSPI; import java.util.List; @@ -33,7 +34,8 @@ public interface ReadQueryLoadBalanceAlgorithm extends ShardingSphereAlgorithm, * @param name read query logic data source name * @param writeDataSourceName name of write data source * @param readDataSourceNames names of read data sources + * @param context context * @return name of selected data source */ - String getDataSource(String name, String writeDataSourceName, List readDataSourceNames); + String getDataSource(String name, String writeDataSourceName, List readDataSourceNames, TransactionConnectionContext context); } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-api/src/test/java/org/apache/shardingsphere/readwritesplitting/factory/fixture/ReadQueryLoadBalanceAlgorithmFixture.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-api/src/test/java/org/apache/shardingsphere/readwritesplitting/factory/fixture/ReadQueryLoadBalanceAlgorithmFixture.java index 2686e26174358..b8d4e2c914304 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-api/src/test/java/org/apache/shardingsphere/readwritesplitting/factory/fixture/ReadQueryLoadBalanceAlgorithmFixture.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-api/src/test/java/org/apache/shardingsphere/readwritesplitting/factory/fixture/ReadQueryLoadBalanceAlgorithmFixture.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.readwritesplitting.factory.fixture; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm; import java.util.List; @@ -25,7 +26,7 @@ public final class ReadQueryLoadBalanceAlgorithmFixture implements ReadQueryLoadBalanceAlgorithm { @Override - public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames) { + public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames, final TransactionConnectionContext context) { return null; } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedPrimaryReadQueryLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedPrimaryReadQueryLoadBalanceAlgorithm.java index f11f1be9d76a3..14f03900eafb8 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedPrimaryReadQueryLoadBalanceAlgorithm.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedPrimaryReadQueryLoadBalanceAlgorithm.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import lombok.Getter; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm; import java.util.List; @@ -37,7 +38,7 @@ public void init(final Properties props) { } @Override - public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames) { + public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames, final TransactionConnectionContext context) { return writeDataSourceName; } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithm.java index 621b304bb284b..c40afc28e35e3 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithm.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithm.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import lombok.Getter; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm; -import org.apache.shardingsphere.transaction.TransactionHolder; import java.util.List; import java.util.Properties; @@ -39,12 +39,12 @@ public void init(final Properties props) { } @Override - public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames) { - if (TransactionHolder.isTransaction()) { - if (null == TransactionHolder.getReadWriteSplitRoutedReplica()) { - TransactionHolder.setReadWriteSplitRoutedReplica(readDataSourceNames.get(ThreadLocalRandom.current().nextInt(readDataSourceNames.size()))); + public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames, final TransactionConnectionContext context) { + if (context.isInTransaction()) { + if (null == context.getReadWriteSplitReplicaRoute()) { + context.setReadWriteSplitReplicaRoute(readDataSourceNames.get(ThreadLocalRandom.current().nextInt(readDataSourceNames.size()))); } - return TransactionHolder.getReadWriteSplitRoutedReplica(); + return context.getReadWriteSplitReplicaRoute(); } return readDataSourceNames.get(ThreadLocalRandom.current().nextInt(readDataSourceNames.size())); } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm.java index c6561127d9e3a..eb1860eab86df 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import lombok.Getter; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm; -import org.apache.shardingsphere.transaction.TransactionHolder; import java.util.List; import java.util.Properties; @@ -41,12 +41,12 @@ public void init(final Properties props) { } @Override - public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames) { - if (TransactionHolder.isTransaction()) { - if (null == TransactionHolder.getReadWriteSplitRoutedReplica()) { - TransactionHolder.setReadWriteSplitRoutedReplica(readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size())); + public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames, final TransactionConnectionContext context) { + if (context.isInTransaction()) { + if (null == context.getReadWriteSplitReplicaRoute()) { + context.setReadWriteSplitReplicaRoute(readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size())); } - return TransactionHolder.getReadWriteSplitRoutedReplica(); + return context.getReadWriteSplitReplicaRoute(); } return readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size()); } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java index e7e21a27376a5..21a4a3ed9a18f 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import lombok.Getter; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm; -import org.apache.shardingsphere.transaction.TransactionHolder; import java.util.Arrays; import java.util.List; @@ -45,14 +45,14 @@ public void init(final Properties props) { } @Override - public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames) { + public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames, final TransactionConnectionContext context) { double[] weight = WEIGHT_MAP.containsKey(name) ? WEIGHT_MAP.get(name) : initWeight(readDataSourceNames); WEIGHT_MAP.putIfAbsent(name, weight); - if (TransactionHolder.isTransaction()) { - if (null == TransactionHolder.getReadWriteSplitRoutedReplica()) { - TransactionHolder.setReadWriteSplitRoutedReplica(getDataSourceName(readDataSourceNames, weight)); + if (context.isInTransaction()) { + if (null == context.getReadWriteSplitReplicaRoute()) { + context.setReadWriteSplitReplicaRoute(getDataSourceName(readDataSourceNames, weight)); } - return TransactionHolder.getReadWriteSplitRoutedReplica(); + return context.getReadWriteSplitReplicaRoute(); } return getDataSourceName(readDataSourceNames, weight); } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithm.java index fc89c07e58519..d89f934fce1f9 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithm.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithm.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import lombok.Getter; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm; -import org.apache.shardingsphere.transaction.TransactionHolder; import java.util.List; import java.util.Properties; @@ -39,8 +39,8 @@ public void init(final Properties props) { } @Override - public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames) { - if (TransactionHolder.isTransaction()) { + public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames, final TransactionConnectionContext context) { + if (context.isInTransaction()) { return writeDataSourceName; } return readDataSourceNames.get(ThreadLocalRandom.current().nextInt(readDataSourceNames.size())); diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithm.java index 00f93b198c360..e176e9922b03f 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithm.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithm.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import lombok.Getter; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm; -import org.apache.shardingsphere.transaction.TransactionHolder; import java.util.List; import java.util.Properties; @@ -41,8 +41,8 @@ public void init(final Properties props) { } @Override - public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames) { - if (TransactionHolder.isTransaction()) { + public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames, final TransactionConnectionContext context) { + if (context.isInTransaction()) { return writeDataSourceName; } return readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size()); diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithm.java index 11098637cb5a3..7448706094562 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithm.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithm.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import lombok.Getter; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm; import java.util.List; @@ -38,7 +39,7 @@ public void init(final Properties props) { } @Override - public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames) { + public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames, final TransactionConnectionContext context) { return readDataSourceNames.get(ThreadLocalRandom.current().nextInt(readDataSourceNames.size())); } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithm.java index e018cbb5656c1..6f567b0b836d9 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithm.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithm.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import lombok.Getter; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm; import java.util.List; @@ -40,7 +41,7 @@ public void init(final Properties props) { } @Override - public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames) { + public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames, final TransactionConnectionContext context) { return readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size()); } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java index c9d593f7d8be3..64680ccebe752 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import lombok.Getter; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm; import java.util.Arrays; @@ -44,7 +45,7 @@ public void init(final Properties props) { } @Override - public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames) { + public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames, final TransactionConnectionContext context) { double[] weight = WEIGHT_MAP.containsKey(name) ? WEIGHT_MAP.get(name) : initWeight(readDataSourceNames); WEIGHT_MAP.putIfAbsent(name, weight); return getDataSourceName(readDataSourceNames, weight); diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java index 8685412cdaeed..3a3f4dcb4cdfb 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import lombok.Getter; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm; -import org.apache.shardingsphere.transaction.TransactionHolder; import java.util.Arrays; import java.util.List; @@ -45,8 +45,8 @@ public void init(final Properties props) { } @Override - public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames) { - if (TransactionHolder.isTransaction()) { + public String getDataSource(final String name, final String writeDataSourceName, final List readDataSourceNames, final TransactionConnectionContext context) { + if (context.isInTransaction()) { return writeDataSourceName; } double[] weight = weightMap.containsKey(name) ? weightMap.get(name) : initWeight(readDataSourceNames); diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouter.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouter.java index e94adc359dc48..a2fd6ef14fae0 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouter.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouter.java @@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.readwritesplitting.constant.ReadwriteSplittingOrder; import org.apache.shardingsphere.readwritesplitting.route.impl.ReadwriteSplittingDataSourceRouter; import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingDataSourceRule; @@ -40,17 +41,19 @@ public final class ReadwriteSplittingSQLRouter implements SQLRouter { @Override - public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ReadwriteSplittingRule rule, final ConfigurationProperties props) { + public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ReadwriteSplittingRule rule, + final ConfigurationProperties props, final ConnectionContext connectionContext) { RouteContext result = new RouteContext(); ReadwriteSplittingDataSourceRule singleDataSourceRule = rule.getSingleDataSourceRule(); - String dataSourceName = new ReadwriteSplittingDataSourceRouter(singleDataSourceRule).route(logicSQL.getSqlStatementContext()); + String dataSourceName = new ReadwriteSplittingDataSourceRouter(singleDataSourceRule, connectionContext).route(logicSQL.getSqlStatementContext()); result.getRouteUnits().add(new RouteUnit(new RouteMapper(singleDataSourceRule.getName(), dataSourceName), Collections.emptyList())); return result; } @Override public void decorateRouteContext(final RouteContext routeContext, - final LogicSQL logicSQL, final ShardingSphereDatabase database, final ReadwriteSplittingRule rule, final ConfigurationProperties props) { + final LogicSQL logicSQL, final ShardingSphereDatabase database, final ReadwriteSplittingRule rule, + final ConfigurationProperties props, final ConnectionContext connectionContext) { Collection toBeRemoved = new LinkedList<>(); Collection toBeAdded = new LinkedList<>(); for (RouteUnit each : routeContext.getRouteUnits()) { @@ -58,7 +61,7 @@ public void decorateRouteContext(final RouteContext routeContext, Optional dataSourceRule = rule.findDataSourceRule(dataSourceName); if (dataSourceRule.isPresent() && dataSourceRule.get().getName().equalsIgnoreCase(each.getDataSourceMapper().getActualName())) { toBeRemoved.add(each); - String actualDataSourceName = new ReadwriteSplittingDataSourceRouter(dataSourceRule.get()).route(logicSQL.getSqlStatementContext()); + String actualDataSourceName = new ReadwriteSplittingDataSourceRouter(dataSourceRule.get(), connectionContext).route(logicSQL.getSqlStatementContext()); toBeAdded.add(new RouteUnit(new RouteMapper(each.getDataSourceMapper().getLogicName(), actualDataSourceName), each.getTableMappers())); } } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/impl/ReadwriteSplittingDataSourceRouter.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/impl/ReadwriteSplittingDataSourceRouter.java index 5773ff964e150..34248c126370c 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/impl/ReadwriteSplittingDataSourceRouter.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/impl/ReadwriteSplittingDataSourceRouter.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.hint.HintManager; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingDataSourceRule; import org.apache.shardingsphere.readwritesplitting.strategy.type.DynamicReadwriteSplittingStrategy; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; @@ -36,6 +37,8 @@ public final class ReadwriteSplittingDataSourceRouter { private final ReadwriteSplittingDataSourceRule rule; + private final ConnectionContext connectionContext; + /** * Route. * @@ -46,7 +49,8 @@ public String route(final SQLStatementContext sqlStatementContext) { if (isPrimaryRoute(sqlStatementContext)) { return rule.getWriteDataSource(); } - return rule.getLoadBalancer().getDataSource(rule.getName(), rule.getWriteDataSource(), rule.getEnabledReplicaDataSources()); + return rule.getLoadBalancer().getDataSource(rule.getName(), rule.getWriteDataSource(), rule.getEnabledReplicaDataSources(), + connectionContext.getTransactionConnectionContext()); } private boolean isPrimaryRoute(final SQLStatementContext sqlStatementContext) { diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedPrimaryReadQueryLoadBalanceAlgorithmTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedPrimaryReadQueryLoadBalanceAlgorithmTest.java index 20b1bc55bb22f..17fce4ecd4d18 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedPrimaryReadQueryLoadBalanceAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedPrimaryReadQueryLoadBalanceAlgorithmTest.java @@ -17,13 +17,14 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.junit.Test; import java.util.Arrays; import java.util.List; -import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; public final class FixedPrimaryReadQueryLoadBalanceAlgorithmTest { @@ -35,6 +36,6 @@ public void assertGetDataSource() { String readDataSourceName1 = "test_replica_ds_1"; String readDataSourceName2 = "test_replica_ds_2"; List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(writeDataSourceName)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, new TransactionConnectionContext()), is(writeDataSourceName)); } } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithmTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithmTest.java index 74a35bc845638..a99075df8583b 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithmTest.java @@ -17,16 +17,16 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; -import org.apache.shardingsphere.transaction.TransactionHolder; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.junit.Test; import java.util.Arrays; import java.util.LinkedList; import java.util.List; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public final class FixedReplicaRandomReadQueryLoadBalanceAlgorithmTest { @@ -38,12 +38,12 @@ public void assertGetDataSourceInTransaction() { String readDataSourceName1 = "test_replica_ds_1"; String readDataSourceName2 = "test_replica_ds_2"; List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - TransactionHolder.setInTransaction(); - String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames); - assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames))); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(routeDataSource)); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(routeDataSource)); - TransactionHolder.clear(); + TransactionConnectionContext context = new TransactionConnectionContext(); + context.setInTransaction(true); + String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context); + assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context))); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource)); } @Test @@ -54,7 +54,7 @@ public void assertGetDataSourceWithoutTransaction() { List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); List noTransactionReadDataSourceNames = new LinkedList<>(); for (int i = 0; i < 5; i++) { - String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames); + String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, new TransactionConnectionContext()); noTransactionReadDataSourceNames.add(routeDataSource); } assertTrue(noTransactionReadDataSourceNames.size() > 1); diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithmTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithmTest.java index 3ed90dd433860..28f796e314420 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithmTest.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; -import org.apache.shardingsphere.transaction.TransactionHolder; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.junit.Test; import java.util.Arrays; @@ -38,13 +38,13 @@ public void assertGetDataSourceInTransaction() { String readDataSourceName1 = "test_replica_ds_1"; String readDataSourceName2 = "test_replica_ds_2"; List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - TransactionHolder.setInTransaction(); - String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames); - assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames))); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(routeDataSource)); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(routeDataSource)); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(routeDataSource)); - TransactionHolder.clear(); + TransactionConnectionContext context = new TransactionConnectionContext(); + context.setInTransaction(true); + String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context); + assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context))); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource)); } @Test @@ -55,7 +55,7 @@ public void assertGetDataSourceWithoutTransaction() { List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); List noTransactionReadDataSourceNames = new LinkedList<>(); for (int i = 0; i < 5; i++) { - String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames); + String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, new TransactionConnectionContext()); noTransactionReadDataSourceNames.add(routeDataSource); } assertTrue(noTransactionReadDataSourceNames.size() > 1); diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithmTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithmTest.java index abca353a9b6c6..afd34211007f3 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithmTest.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.factory.ReadQueryLoadBalanceAlgorithmFactory; -import org.apache.shardingsphere.transaction.TransactionHolder; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -32,8 +32,8 @@ import java.util.Map; import java.util.Properties; -import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public final class FixedReplicaWeightReadQueryLoadBalanceAlgorithmTest { @@ -50,11 +50,11 @@ public void reset() throws NoSuchFieldException, IllegalAccessException { @Test public void assertGetSingleReadDataSourceInTransaction() { FixedReplicaWeightReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = createReadQueryLoadBalanceAlgorithm(createSingleDataSourceProperties()); - TransactionHolder.isTransaction(); - String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1")); + TransactionConnectionContext context = new TransactionConnectionContext(); + context.setInTransaction(true); + String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1"), context); assertThat(routeDataSource, is("test_read_ds_1")); - assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1")), is(routeDataSource)); - TransactionHolder.clear(); + assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1"), context), is(routeDataSource)); } private Properties createSingleDataSourceProperties() { @@ -72,7 +72,7 @@ public void assertGetMultipleReadDataSourcesWithoutTransaction() { List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); List noTransactionReadDataSourceNames = new LinkedList<>(); for (int i = 0; i < 5; i++) { - String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames); + String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, new TransactionConnectionContext()); noTransactionReadDataSourceNames.add(routeDataSource); } assertTrue(noTransactionReadDataSourceNames.size() > 1); @@ -85,12 +85,12 @@ public void assertGetMultipleReadDataSourcesInTransaction() { String readDataSourceName1 = "test_read_ds_1"; String readDataSourceName2 = "test_read_ds_2"; List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - TransactionHolder.setInTransaction(); - String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", readDataSourceNames); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(routeDataSource)); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(routeDataSource)); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(routeDataSource)); - TransactionHolder.clear(); + TransactionConnectionContext transactionConnectionContext = new TransactionConnectionContext(); + transactionConnectionContext.setInTransaction(true); + String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", readDataSourceNames, transactionConnectionContext); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, transactionConnectionContext), is(routeDataSource)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, transactionConnectionContext), is(routeDataSource)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, transactionConnectionContext), is(routeDataSource)); } private Properties createMultipleDataSourcesProperties() { diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithmTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithmTest.java index 675ed75f0825c..e14d1e4b1c1e7 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithmTest.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; -import org.apache.shardingsphere.transaction.TransactionHolder; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.junit.Test; import java.util.Arrays; @@ -35,9 +35,9 @@ public void assertGetDataSourceInTransaction() { String readDataSourceName1 = "test_replica_ds_1"; String readDataSourceName2 = "test_replica_ds_2"; List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - TransactionHolder.setInTransaction(); - assertTrue(writeDataSourceName.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames))); - TransactionHolder.clear(); + TransactionConnectionContext context = new TransactionConnectionContext(); + context.setInTransaction(true); + assertTrue(writeDataSourceName.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context))); } @Test @@ -46,8 +46,9 @@ public void assertGetDataSourceNotInTransaction() { String readDataSourceName1 = "test_replica_ds_1"; String readDataSourceName2 = "test_replica_ds_2"; List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames))); - assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames))); - assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames))); + TransactionConnectionContext context = new TransactionConnectionContext(); + assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context))); + assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context))); + assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context))); } } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithmTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithmTest.java index 37b6d80a84daa..d9a49bdec50b8 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithmTest.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; -import org.apache.shardingsphere.transaction.TransactionHolder; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.junit.Test; import java.util.Arrays; @@ -35,9 +35,10 @@ public void assertGetDataSource() { String readDataSourceName2 = "test_read_ds_2"; RoundRobinReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new RoundRobinReadQueryLoadBalanceAlgorithm(); List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(readDataSourceName1)); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(readDataSourceName2)); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(readDataSourceName1)); + TransactionConnectionContext context = new TransactionConnectionContext(); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(readDataSourceName1)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(readDataSourceName2)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(readDataSourceName1)); } @Test @@ -47,8 +48,8 @@ public void assertGetDataSourceInTransaction() { String readDataSourceName2 = "test_read_ds_2"; RoundRobinReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new RoundRobinReadQueryLoadBalanceAlgorithm(); List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - TransactionHolder.setInTransaction(); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(writeDataSourceName)); - TransactionHolder.clear(); + TransactionConnectionContext context = new TransactionConnectionContext(); + context.setInTransaction(true); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(writeDataSourceName)); } } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithmTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithmTest.java index c85740dd7a171..2c286410650d4 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithmTest.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; -import org.apache.shardingsphere.transaction.TransactionHolder; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.junit.Test; import java.util.Arrays; @@ -35,10 +35,10 @@ public void assertGetDataSourceInTransaction() { String readDataSourceName1 = "test_replica_ds_1"; String readDataSourceName2 = "test_replica_ds_2"; List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - TransactionHolder.setInTransaction(); - assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames))); - assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames))); - assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames))); - TransactionHolder.clear(); + TransactionConnectionContext context = new TransactionConnectionContext(); + context.setInTransaction(true); + assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context))); + assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context))); + assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context))); } } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithmTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithmTest.java index 5c80607b30101..4e24b3764ab8f 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithmTest.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; -import org.apache.shardingsphere.transaction.TransactionHolder; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.junit.Test; import java.util.Arrays; @@ -35,9 +35,10 @@ public void assertGetDataSource() { String readDataSourceName2 = "test_read_ds_2"; TransactionRoundRobinReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new TransactionRoundRobinReadQueryLoadBalanceAlgorithm(); List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(readDataSourceName1)); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(readDataSourceName2)); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(readDataSourceName1)); + TransactionConnectionContext context = new TransactionConnectionContext(); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(readDataSourceName1)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(readDataSourceName2)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(readDataSourceName1)); } @Test @@ -47,10 +48,10 @@ public void assertGetDataSourceInTransaction() { String readDataSourceName2 = "test_read_ds_2"; TransactionRoundRobinReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new TransactionRoundRobinReadQueryLoadBalanceAlgorithm(); List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - TransactionHolder.setInTransaction(); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(readDataSourceName1)); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(readDataSourceName2)); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(readDataSourceName1)); - TransactionHolder.clear(); + TransactionConnectionContext context = new TransactionConnectionContext(); + context.setInTransaction(true); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(readDataSourceName1)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(readDataSourceName2)); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(readDataSourceName1)); } } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithmTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithmTest.java index 5ca7a17061a98..cece62aade3c8 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithmTest.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.factory.ReadQueryLoadBalanceAlgorithmFactory; -import org.apache.shardingsphere.transaction.TransactionHolder; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -50,9 +50,9 @@ public void reset() throws NoSuchFieldException, IllegalAccessException { @Test public void assertGetSingleReadDataSource() { TransactionWeightReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = createReadQueryLoadBalanceAlgorithm(createSingleDataSourceProperties()); - TransactionHolder.setInTransaction(); - assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1")), is("test_read_ds_1")); - TransactionHolder.clear(); + TransactionConnectionContext context = new TransactionConnectionContext(); + context.setInTransaction(true); + assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1"), context), is("test_read_ds_1")); } private Properties createSingleDataSourceProperties() { @@ -68,12 +68,12 @@ public void assertGetMultipleReadDataSources() { String readDataSourceName1 = "test_read_ds_1"; String readDataSourceName2 = "test_read_ds_2"; List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - TransactionHolder.setInTransaction(); - assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames))); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), notNullValue()); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), notNullValue()); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), notNullValue()); - TransactionHolder.clear(); + TransactionConnectionContext context = new TransactionConnectionContext(); + context.setInTransaction(true); + assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context))); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), notNullValue()); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), notNullValue()); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), notNullValue()); } private Properties createMultipleDataSourcesProperties() { diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithmTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithmTest.java index b5ddcf2a97778..01a0f14d467e1 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithmTest.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.readwritesplitting.factory.ReadQueryLoadBalanceAlgorithmFactory; -import org.apache.shardingsphere.transaction.TransactionHolder; import org.junit.Test; import java.util.Arrays; @@ -36,7 +36,7 @@ public final class WeightReadQueryLoadBalanceAlgorithmTest { @Test public void assertGetSingleReadDataSource() { WeightReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = createReadQueryLoadBalanceAlgorithm(createSingleDataSourceProperties()); - assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1")), is("test_read_ds_1")); + assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1"), new TransactionConnectionContext()), is("test_read_ds_1")); } private Properties createSingleDataSourceProperties() { @@ -52,9 +52,9 @@ public void assertGetMultipleReadDataSources() { String readDataSourceName1 = "test_read_ds_1"; String readDataSourceName2 = "test_read_ds_2"; List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), notNullValue()); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), notNullValue()); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), notNullValue()); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, new TransactionConnectionContext()), notNullValue()); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, new TransactionConnectionContext()), notNullValue()); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, new TransactionConnectionContext()), notNullValue()); } private Properties createMultipleDataSourcesProperties() { @@ -75,8 +75,8 @@ public void assertGetReadDataSourceInTransaction() { String readDataSourceName1 = "test_read_ds_1"; String readDataSourceName2 = "test_read_ds_2"; List readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2); - TransactionHolder.setInTransaction(); - assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames), is(writeDataSourceName)); - TransactionHolder.clear(); + TransactionConnectionContext context = new TransactionConnectionContext(); + context.setInTransaction(true); + assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(writeDataSourceName)); } } diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java index 90cb15ce83b7c..90997bd57b5bd 100644 --- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java +++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java @@ -32,6 +32,7 @@ import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; import org.apache.shardingsphere.infra.rule.identifier.type.DynamicDataSourceContainedRule; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.api.strategy.DynamicReadwriteSplittingStrategyConfiguration; @@ -103,7 +104,7 @@ public void assertCreateRouteContextToPrimaryWithoutRouteUnits() { ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(rule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); assertThat(actual.getRouteUnits().size(), is(1)); RouteUnit routeUnit = actual.getRouteUnits().iterator().next(); assertThat(routeUnit.getDataSourceMapper().getLogicName(), is(DATASOURCE_NAME)); @@ -119,7 +120,7 @@ public void assertDecorateRouteContextToPrimaryDataSource() { ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(rule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - sqlRouter.decorateRouteContext(actual, logicSQL, database, rule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(actual, logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(NONE_READWRITE_SPLITTING_DATASOURCE_NAME)); assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE)); @@ -134,7 +135,7 @@ public void assertCreateRouteContextToReplicaDataSource() { ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(rule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); assertThat(actual.getRouteUnits().size(), is(1)); RouteUnit routeUnit = actual.getRouteUnits().iterator().next(); assertThat(routeUnit.getDataSourceMapper().getLogicName(), is(DATASOURCE_NAME)); @@ -153,7 +154,7 @@ public void assertDecorateRouteContextToReplicaDataSource() { ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(rule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - sqlRouter.decorateRouteContext(actual, logicSQL, database, rule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(actual, logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(NONE_READWRITE_SPLITTING_DATASOURCE_NAME)); assertThat(routedDataSourceNames.next(), is(READ_DATASOURCE)); @@ -168,7 +169,7 @@ public void assertCreateRouteContextToPrimaryDataSourceWithLock() { ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(rule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE)); } @@ -183,7 +184,7 @@ public void assertDecorateRouteContextToPrimaryDataSourceWithLock() { ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(rule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - sqlRouter.decorateRouteContext(actual, logicSQL, database, rule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(actual, logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(NONE_READWRITE_SPLITTING_DATASOURCE_NAME)); assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE)); @@ -196,7 +197,7 @@ public void assertCreateRouteContextToPrimaryDataSource() { ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(rule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE)); } @@ -209,14 +210,14 @@ public void assertCreateRouteContextToReadDataSource() { ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(rule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE)); MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement); when(selectStatement.getLock()).thenReturn(Optional.empty()); logicSQL = new LogicSQL(sqlStatementContext, "", Collections.emptyList()); - actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties())); + actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(READ_DATASOURCE)); } @@ -232,7 +233,7 @@ public void assertSqlHintRouteWriteOnly() { ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(rule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = sqlRouter.createRouteContext(logicSQL, database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE)); } @@ -245,7 +246,7 @@ public void assertCreateRouteContextToPrimaryDataSourceWithWriteDataSourceQueryE ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(dynamicRule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - RouteContext actual = dynamicSqlRouter.createRouteContext(logicSQL, database, dynamicRule, new ConfigurationProperties(new Properties())); + RouteContext actual = dynamicSqlRouter.createRouteContext(logicSQL, database, dynamicRule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE)); } @@ -259,7 +260,7 @@ public void assertDecorateRouteContextToPrimaryDataSourceWithWriteDataSourceQuer ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(dynamicRule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - dynamicSqlRouter.decorateRouteContext(actual, logicSQL, database, dynamicRule, new ConfigurationProperties(new Properties())); + dynamicSqlRouter.decorateRouteContext(actual, logicSQL, database, dynamicRule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(NONE_READWRITE_SPLITTING_DATASOURCE_NAME)); assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE)); diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java index 8ab6752a03183..85391f51ec90b 100644 --- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java +++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.route.SQLRouter; import org.apache.shardingsphere.infra.route.context.RouteContext; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.shadow.constant.ShadowOrder; import org.apache.shardingsphere.shadow.route.engine.ShadowRouteEngineFactory; import org.apache.shardingsphere.shadow.rule.ShadowRule; @@ -32,14 +33,15 @@ public final class ShadowSQLRouter implements SQLRouter { @Override - public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ShadowRule rule, final ConfigurationProperties props) { + public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, + final ShadowRule rule, final ConfigurationProperties props, final ConnectionContext connectionContext) { // TODO return new RouteContext(); } @Override - public void decorateRouteContext(final RouteContext routeContext, - final LogicSQL logicSQL, final ShardingSphereDatabase database, final ShadowRule rule, final ConfigurationProperties props) { + public void decorateRouteContext(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereDatabase database, + final ShadowRule rule, final ConfigurationProperties props, final ConnectionContext connectionContext) { ShadowRouteEngineFactory.newInstance(logicSQL).route(routeContext, rule); } diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouterTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouterTest.java index 37c88c04eb001..08800a1cbf79c 100644 --- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouterTest.java +++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouterTest.java @@ -20,6 +20,7 @@ import org.apache.shardingsphere.infra.binder.LogicSQL; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.junit.Test; @@ -30,6 +31,7 @@ public final class ShadowSQLRouterTest { @Test public void assertCreateRouteContext() { - assertNotNull(new ShadowSQLRouter().createRouteContext(mock(LogicSQL.class), mock(ShardingSphereDatabase.class), mock(ShadowRule.class), mock(ConfigurationProperties.class))); + assertNotNull(new ShadowSQLRouter().createRouteContext(mock(LogicSQL.class), mock(ShardingSphereDatabase.class), mock(ShadowRule.class), mock(ConfigurationProperties.class), + new ConnectionContext())); } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java index 287179a877d6d..1548c7e204e2e 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.route.SQLRouter; import org.apache.shardingsphere.infra.route.context.RouteContext; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.sharding.constant.ShardingOrder; import org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition; import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions; @@ -46,7 +47,8 @@ public final class ShardingSQLRouter implements SQLRouter { @SuppressWarnings({"rawtypes", "unchecked"}) @Override - public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ShardingRule rule, final ConfigurationProperties props) { + public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ShardingRule rule, + final ConfigurationProperties props, final ConnectionContext connectionContext) { SQLStatement sqlStatement = logicSQL.getSqlStatementContext().getSqlStatement(); ShardingConditions shardingConditions = createShardingConditions(logicSQL, database, rule); Optional validator = ShardingStatementValidatorFactory.newInstance(sqlStatement, shardingConditions); @@ -72,7 +74,8 @@ private ShardingConditions createShardingConditions(final LogicSQL logicSQL, fin } @Override - public void decorateRouteContext(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereDatabase database, final ShardingRule rule, final ConfigurationProperties props) { + public void decorateRouteContext(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereDatabase database, final ShardingRule rule, + final ConfigurationProperties props, final ConnectionContext connectionContext) { // TODO } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java index c4ca764d9a188..87ccf08971c91 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java @@ -33,6 +33,7 @@ import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngine; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.engine.SQLRouteEngine; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.sharding.route.engine.fixture.AbstractRoutingEngineTest; import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.apache.shardingsphere.singletable.rule.SingleTableRule; @@ -66,7 +67,7 @@ protected final RouteContext assertRoute(final String sql, final List pa Map databases = Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database); SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(databases, parameters, sqlStatementParserEngine.parse(sql, false), DefaultDatabase.LOGIC_NAME); LogicSQL logicSQL = new LogicSQL(sqlStatementContext, sql, parameters); - return new SQLRouteEngine(Arrays.asList(shardingRule, singleTableRule), props).route(logicSQL, database); + return new SQLRouteEngine(Arrays.asList(shardingRule, singleTableRule), props).route(logicSQL, database, new ConnectionContext()); } private Map buildSchemas() { diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/session/ConnectionContext.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/session/ConnectionContext.java index 5dd3fdc328230..27f65afff467a 100644 --- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/session/ConnectionContext.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/session/ConnectionContext.java @@ -20,17 +20,20 @@ import lombok.Getter; import lombok.Setter; import org.apache.shardingsphere.infra.session.cursor.CursorConnectionContext; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import java.util.Optional; /** * Connection context. */ +@Getter public final class ConnectionContext implements AutoCloseable { - @Getter private final CursorConnectionContext cursorConnectionContext = new CursorConnectionContext(); + private final TransactionConnectionContext transactionConnectionContext = new TransactionConnectionContext(); + @Setter private String trafficInstanceId; @@ -47,6 +50,7 @@ public Optional getTrafficInstanceId() { public void close() { clearTrafficInstance(); clearCursorConnectionContext(); + clearTransactionConnectionContext(); } /** @@ -62,4 +66,11 @@ public void clearTrafficInstance() { public void clearCursorConnectionContext() { cursorConnectionContext.close(); } + + /** + * Clear transaction connection context. + */ + public void clearTransactionConnectionContext() { + transactionConnectionContext.close(); + } } diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/TransactionHolderTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/session/transaction/TransactionConnectionContext.java similarity index 56% rename from shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/TransactionHolderTest.java rename to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/session/transaction/TransactionConnectionContext.java index b73713db89125..c7eaa1ccae512 100644 --- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/TransactionHolderTest.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/session/transaction/TransactionConnectionContext.java @@ -15,27 +15,25 @@ * limitations under the License. */ -package org.apache.shardingsphere.transaction; +package org.apache.shardingsphere.infra.session.transaction; -import org.junit.Before; -import org.junit.Test; +import lombok.Getter; +import lombok.Setter; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public final class TransactionHolderTest { +/** + * Transaction connection context. + */ +@Getter +@Setter +public final class TransactionConnectionContext implements AutoCloseable { - @Before - public void assertSetInTransaction() { - assertFalse(TransactionHolder.isTransaction()); - TransactionHolder.setInTransaction(); - assertTrue(TransactionHolder.isTransaction()); - } + private volatile boolean inTransaction; + + private volatile String readWriteSplitReplicaRoute; - @Test - public void assertClear() { - assertTrue(TransactionHolder.isTransaction()); - TransactionHolder.clear(); - assertFalse(TransactionHolder.isTransaction()); + @Override + public void close() { + inTransaction = false; + readWriteSplitReplicaRoute = null; } } diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java index 55b3e271c89e0..a414eda2377aa 100644 --- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java +++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java @@ -48,15 +48,15 @@ public final class KernelProcessor { */ public ExecutionContext generateExecutionContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ShardingSphereRuleMetaData globalRuleMetaData, final ConfigurationProperties props, final ConnectionContext connectionContext) { - RouteContext routeContext = route(logicSQL, database, props); + RouteContext routeContext = route(logicSQL, database, props, connectionContext); SQLRewriteResult rewriteResult = rewrite(logicSQL, database, globalRuleMetaData, props, routeContext, connectionContext); ExecutionContext result = createExecutionContext(logicSQL, database, routeContext, rewriteResult); logSQL(logicSQL, props, result); return result; } - private RouteContext route(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ConfigurationProperties props) { - return new SQLRouteEngine(database.getRuleMetaData().getRules(), props).route(logicSQL, database); + private RouteContext route(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ConfigurationProperties props, final ConnectionContext connectionContext) { + return new SQLRouteEngine(database.getRuleMetaData().getRules(), props).route(logicSQL, database, connectionContext); } private SQLRewriteResult rewrite(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ShardingSphereRuleMetaData globalRuleMetaData, diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/SQLRouter.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/SQLRouter.java index 35b2f9be3a982..c4253ded9af7e 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/SQLRouter.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/SQLRouter.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.infra.util.spi.annotation.SingletonSPI; import org.apache.shardingsphere.infra.util.spi.type.ordered.OrderedSPI; @@ -40,18 +41,19 @@ public interface SQLRouter extends OrderedSPI { * @param database database * @param rule rule * @param props configuration properties + * @param connectionContext connection context * @return route context */ - RouteContext createRouteContext(LogicSQL logicSQL, ShardingSphereDatabase database, T rule, ConfigurationProperties props); + RouteContext createRouteContext(LogicSQL logicSQL, ShardingSphereDatabase database, T rule, ConfigurationProperties props, ConnectionContext connectionContext); /** * Decorate route context. - * - * @param routeContext route context + * @param routeContext route context * @param logicSQL logic SQL * @param database database * @param rule rule * @param props configuration properties + * @param connectionContext connection context */ - void decorateRouteContext(RouteContext routeContext, LogicSQL logicSQL, ShardingSphereDatabase database, T rule, ConfigurationProperties props); + void decorateRouteContext(RouteContext routeContext, LogicSQL logicSQL, ShardingSphereDatabase database, T rule, ConfigurationProperties props, ConnectionContext connectionContext); } diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java index 10ddb09473b76..14f3c16efbbff 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.route.engine.impl.AllSQLRouteExecutor; import org.apache.shardingsphere.infra.route.engine.impl.PartialSQLRouteExecutor; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTableStatusStatement; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement; @@ -46,11 +47,12 @@ public final class SQLRouteEngine { * * @param logicSQL logic SQL * @param database database + * @param connectionContext connection context * @return route context */ - public RouteContext route(final LogicSQL logicSQL, final ShardingSphereDatabase database) { + public RouteContext route(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ConnectionContext connectionContext) { SQLRouteExecutor executor = isNeedAllSchemas(logicSQL.getSqlStatementContext().getSqlStatement()) ? new AllSQLRouteExecutor() : new PartialSQLRouteExecutor(rules, props); - return executor.route(logicSQL, database); + return executor.route(logicSQL, database, connectionContext); } // TODO use dynamic config to judge UnconfiguredSchema diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java index 712a73dae2a6c..896c96f00160e 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java @@ -18,8 +18,9 @@ package org.apache.shardingsphere.infra.route.engine; import org.apache.shardingsphere.infra.binder.LogicSQL; -import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.route.context.RouteContext; +import org.apache.shardingsphere.infra.session.ConnectionContext; /** * SQL route executor. @@ -31,7 +32,8 @@ public interface SQLRouteExecutor { * * @param logicSQL logic SQL * @param database database + * @param connectionContext connection context * @return route context */ - RouteContext route(LogicSQL logicSQL, ShardingSphereDatabase database); + RouteContext route(LogicSQL logicSQL, ShardingSphereDatabase database, ConnectionContext connectionContext); } diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java index 1e7e75cb087c6..f70c676a73e1b 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; import org.apache.shardingsphere.infra.route.engine.SQLRouteExecutor; +import org.apache.shardingsphere.infra.session.ConnectionContext; import java.util.Collections; @@ -32,7 +33,7 @@ public final class AllSQLRouteExecutor implements SQLRouteExecutor { @Override - public RouteContext route(final LogicSQL logicSQL, final ShardingSphereDatabase database) { + public RouteContext route(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ConnectionContext connectionContext) { RouteContext result = new RouteContext(); for (String each : database.getResource().getDataSources().keySet()) { result.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())); diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java index e3bb6eaafc0a6..c9bb7da097703 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.infra.route.context.RouteUnit; import org.apache.shardingsphere.infra.route.engine.SQLRouteExecutor; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; +import org.apache.shardingsphere.infra.session.ConnectionContext; import java.util.Collection; import java.util.Collections; @@ -50,13 +51,13 @@ public PartialSQLRouteExecutor(final Collection rules, final @Override @SuppressWarnings({"unchecked", "rawtypes"}) - public RouteContext route(final LogicSQL logicSQL, final ShardingSphereDatabase database) { + public RouteContext route(final LogicSQL logicSQL, final ShardingSphereDatabase database, final ConnectionContext connectionContext) { RouteContext result = new RouteContext(); for (Entry entry : routers.entrySet()) { if (result.getRouteUnits().isEmpty()) { - result = entry.getValue().createRouteContext(logicSQL, database, entry.getKey(), props); + result = entry.getValue().createRouteContext(logicSQL, database, entry.getKey(), props, connectionContext); } else { - entry.getValue().decorateRouteContext(result, logicSQL, database, entry.getKey(), props); + entry.getValue().decorateRouteContext(result, logicSQL, database, entry.getKey(), props, connectionContext); } } if (result.getRouteUnits().isEmpty() && 1 == database.getResource().getDataSources().size()) { diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/AllSQLRouteExecutorTest.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/AllSQLRouteExecutorTest.java index b45bd1c6880f2..bd164b0382ee1 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/AllSQLRouteExecutorTest.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/AllSQLRouteExecutorTest.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteUnit; import org.apache.shardingsphere.infra.route.engine.impl.AllSQLRouteExecutor; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.junit.Test; import java.util.stream.Collectors; @@ -42,7 +43,7 @@ public void assertRouteSuccess() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResource().getDataSources().keySet()).thenReturn(Stream.of(name).collect(Collectors.toSet())); AllSQLRouteExecutor allSQLRouteExecutor = new AllSQLRouteExecutor(); - RouteContext actual = allSQLRouteExecutor.route(mock(LogicSQL.class), database); + RouteContext actual = allSQLRouteExecutor.route(mock(LogicSQL.class), database, new ConnectionContext()); assertThat(actual.getRouteUnits().size(), is(1)); RouteUnit routeUnit = actual.getRouteUnits().iterator().next(); assertThat(routeUnit.getDataSourceMapper().getLogicName(), is(name)); diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java index e45d41156622a..7ee920c05e468 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.infra.route.context.RouteUnit; import org.apache.shardingsphere.infra.route.fixture.rule.RouteFailureRuleFixture; import org.apache.shardingsphere.infra.route.fixture.rule.RouteRuleFixture; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.junit.Test; import java.util.Collections; @@ -48,7 +49,7 @@ public void assertRouteSuccess() { ShardingSphereDatabase database = new ShardingSphereDatabase("logic_schema", mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(Collections.singleton(new RouteRuleFixture()), new ConfigurationProperties(new Properties())); - RouteContext actual = sqlRouteEngine.route(logicSQL, database); + RouteContext actual = sqlRouteEngine.route(logicSQL, database, new ConnectionContext()); assertThat(actual.getRouteUnits().size(), is(1)); RouteUnit routeUnit = actual.getRouteUnits().iterator().next(); assertThat(routeUnit.getDataSourceMapper().getLogicName(), is("ds")); @@ -62,6 +63,6 @@ public void assertRouteFailure() { ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.singleton(new RouteRuleFixture())); ShardingSphereDatabase database = new ShardingSphereDatabase("logic_schema", mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - new SQLRouteEngine(Collections.singleton(new RouteFailureRuleFixture()), new ConfigurationProperties(new Properties())).route(logicSQL, database); + new SQLRouteEngine(Collections.singleton(new RouteFailureRuleFixture()), new ConfigurationProperties(new Properties())).route(logicSQL, database, new ConnectionContext()); } } diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/SQLRouterFailureFixture.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/SQLRouterFailureFixture.java index 4ae47f7798dde..c5d7de267e1f9 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/SQLRouterFailureFixture.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/SQLRouterFailureFixture.java @@ -17,23 +17,25 @@ package org.apache.shardingsphere.infra.route.fixture.router; +import org.apache.shardingsphere.infra.binder.LogicSQL; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.route.SQLRouter; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.fixture.rule.RouteFailureRuleFixture; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.binder.LogicSQL; +import org.apache.shardingsphere.infra.session.ConnectionContext; public final class SQLRouterFailureFixture implements SQLRouter { @Override - public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final RouteFailureRuleFixture rule, final ConfigurationProperties props) { + public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final RouteFailureRuleFixture rule, + final ConfigurationProperties props, final ConnectionContext connectionContext) { throw new UnsupportedOperationException("Route failure."); } @Override - public void decorateRouteContext(final RouteContext routeContext, - final LogicSQL logicSQL, final ShardingSphereDatabase database, final RouteFailureRuleFixture rule, final ConfigurationProperties props) { + public void decorateRouteContext(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereDatabase database, + final RouteFailureRuleFixture rule, final ConfigurationProperties props, final ConnectionContext connectionContext) { throw new UnsupportedOperationException("Route failure."); } diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/SQLRouterFixture.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/SQLRouterFixture.java index f1161075ecac7..0bd085daf542c 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/SQLRouterFixture.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/SQLRouterFixture.java @@ -17,21 +17,23 @@ package org.apache.shardingsphere.infra.route.fixture.router; +import org.apache.shardingsphere.infra.binder.LogicSQL; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.route.SQLRouter; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; import org.apache.shardingsphere.infra.route.fixture.rule.RouteRuleFixture; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.binder.LogicSQL; +import org.apache.shardingsphere.infra.session.ConnectionContext; import java.util.Collections; public final class SQLRouterFixture implements SQLRouter { @Override - public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final RouteRuleFixture rule, final ConfigurationProperties props) { + public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final RouteRuleFixture rule, + final ConfigurationProperties props, final ConnectionContext connectionContext) { RouteContext result = new RouteContext(); result.getRouteUnits().add(new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.emptyList())); return result; @@ -39,7 +41,8 @@ public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSp @Override public void decorateRouteContext(final RouteContext routeContext, - final LogicSQL logicSQL, final ShardingSphereDatabase database, final RouteRuleFixture rule, final ConfigurationProperties props) { + final LogicSQL logicSQL, final ShardingSphereDatabase database, final RouteRuleFixture rule, + final ConfigurationProperties props, final ConnectionContext connectionContext) { routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.emptyList())); } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java index 92c7c8fc79c25..a2f669c5f5c44 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java @@ -25,7 +25,6 @@ import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement; import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.transaction.TransactionHolder; import java.sql.Array; import java.sql.DatabaseMetaData; @@ -147,7 +146,7 @@ public void setAutoCommit(final boolean autoCommit) throws SQLException { private void processLocalTransaction() throws SQLException { connectionManager.setAutoCommit(autoCommit); if (!autoCommit) { - TransactionHolder.setInTransaction(); + connectionContext.getTransactionConnectionContext().setInTransaction(true); } } @@ -156,7 +155,7 @@ private void processDistributeTransaction() throws SQLException { case BEGIN: connectionManager.close(); connectionManager.getConnectionTransaction().begin(); - TransactionHolder.setInTransaction(); + getConnectionContext().getTransactionConnectionContext().setInTransaction(true); break; case COMMIT: connectionManager.getConnectionTransaction().commit(); @@ -172,7 +171,7 @@ public void commit() throws SQLException { connectionManager.commit(); } finally { connectionManager.getConnectionTransaction().setRollbackOnly(false); - TransactionHolder.clear(); + connectionContext.clearTransactionConnectionContext(); connectionContext.clearTrafficInstance(); connectionContext.clearCursorConnectionContext(); } @@ -184,7 +183,7 @@ public void rollback() throws SQLException { connectionManager.rollback(); } finally { connectionManager.getConnectionTransaction().setRollbackOnly(false); - TransactionHolder.clear(); + connectionContext.clearTransactionConnectionContext(); connectionContext.clearTrafficInstance(); connectionContext.clearCursorConnectionContext(); } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java index 015a8277d9e4a..70bb636135494 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java @@ -26,7 +26,6 @@ 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.TransactionHolder; import org.apache.shardingsphere.transaction.core.TransactionType; import org.apache.shardingsphere.transaction.core.TransactionTypeHolder; import org.apache.shardingsphere.transaction.rule.TransactionRule; @@ -118,10 +117,10 @@ public void assertCommitWithLocalTransaction() throws SQLException { connection.getConnectionManager().getConnections("ds", 1, ConnectionMode.MEMORY_STRICTLY); connection.setAutoCommit(false); assertFalse(connection.getAutoCommit()); - assertTrue(TransactionHolder.isTransaction()); + assertTrue(connection.getConnectionContext().getTransactionConnectionContext().isInTransaction()); verify(physicalConnection).setAutoCommit(false); connection.commit(); - assertFalse(TransactionHolder.isTransaction()); + assertFalse(connection.getConnectionContext().getTransactionConnectionContext().isInTransaction()); verify(physicalConnection).commit(); } @@ -132,10 +131,10 @@ public void assertCommitWithDistributedTransaction() throws SQLException { final ConnectionManager connectionManager = mockConnectionManager(connectionTransaction); connection.setAutoCommit(false); assertFalse(connection.getAutoCommit()); - assertTrue(TransactionHolder.isTransaction()); + assertTrue(connection.getConnectionContext().getTransactionConnectionContext().isInTransaction()); verify(connectionTransaction).begin(); connection.commit(); - assertFalse(TransactionHolder.isTransaction()); + assertFalse(connection.getConnectionContext().getTransactionConnectionContext().isInTransaction()); verify(connectionManager).commit(); } @@ -157,10 +156,10 @@ public void assertRollbackWithDistributedTransaction() throws SQLException { final ConnectionManager connectionManager = mockConnectionManager(connectionTransaction); connection.setAutoCommit(false); assertFalse(connection.getAutoCommit()); - assertTrue(TransactionHolder.isTransaction()); + assertTrue(connection.getConnectionContext().getTransactionConnectionContext().isInTransaction()); verify(connectionTransaction).begin(); connection.rollback(); - assertFalse(TransactionHolder.isTransaction()); + assertFalse(connection.getConnectionContext().getTransactionConnectionContext().isInTransaction()); verify(connectionManager).rollback(); } diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouter.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouter.java index 0727ad654f210..5f2da4e86f93a 100644 --- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouter.java +++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouter.java @@ -33,6 +33,7 @@ import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.singletable.constant.SingleTableOrder; import org.apache.shardingsphere.singletable.route.engine.SingleTableRouteEngineFactory; import org.apache.shardingsphere.singletable.route.validator.SingleTableMetadataValidator; @@ -53,7 +54,8 @@ public final class SingleTableSQLRouter implements SQLRouter { @SuppressWarnings({"rawtypes", "unchecked"}) @Override - public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final SingleTableRule rule, final ConfigurationProperties props) { + public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSphereDatabase database, final SingleTableRule rule, + final ConfigurationProperties props, final ConnectionContext connectionContext) { if (1 == database.getResource().getDataSources().size()) { return createSingleDataSourceRouteContext(rule, database); } @@ -71,7 +73,7 @@ public RouteContext createRouteContext(final LogicSQL logicSQL, final ShardingSp @Override public void decorateRouteContext(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereDatabase database, - final SingleTableRule rule, final ConfigurationProperties props) { + final SingleTableRule rule, final ConfigurationProperties props, final ConnectionContext connectionContext) { SQLStatementContext sqlStatementContext = logicSQL.getSqlStatementContext(); Collection singleTableNames = getSingleTableNames(sqlStatementContext, database, rule, routeContext); if (singleTableNames.isEmpty()) { diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java index 061e5d2ff4343..773196fa78d59 100644 --- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java +++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java @@ -30,6 +30,7 @@ import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.infra.session.ConnectionContext; import org.apache.shardingsphere.singletable.config.SingleTableRuleConfiguration; import org.apache.shardingsphere.singletable.rule.SingleTableRule; import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment; @@ -67,7 +68,7 @@ public void assertCreateRouteContextWithSingleDataSource() throws SQLException { DefaultDatabase.LOGIC_NAME, Collections.singletonMap("foo_ds", new MockedDataSource(mockConnection())), Collections.emptyList()); rule.getSingleTableDataNodes().put("t_order", Collections.singletonList(createDataNode("foo_ds"))); ShardingSphereDatabase database = mockSingleDatabase(); - RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); assertThat(actual.getRouteUnits().size(), is(1)); RouteUnit routeUnit = actual.getRouteUnits().iterator().next(); assertThat(routeUnit.getDataSourceMapper().getLogicName(), is("foo_ds")); @@ -87,7 +88,7 @@ public void assertCreateRouteContextWithReadwriteSplittingDataSource() throws SQ DefaultDatabase.LOGIC_NAME, Collections.singletonMap("readwrite_ds", new MockedDataSource(mockConnection())), Collections.emptyList()); rule.getSingleTableDataNodes().put("t_order", Collections.singletonList(createDataNode("write_ds"))); ShardingSphereDatabase database = mockReadwriteSplittingDatabase(); - RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); assertThat(actual.getRouteUnits().size(), is(1)); RouteUnit routeUnit = actual.getRouteUnits().iterator().next(); assertThat(routeUnit.getDataSourceMapper().getLogicName(), is("readwrite_ds")); @@ -107,7 +108,7 @@ public void assertCreateRouteContextWithMultiDataSource() throws SQLException { SingleTableRule rule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, createMultiDataSourceMap(), Collections.emptyList()); rule.getSingleTableDataNodes().put("t_order", Collections.singleton(createDataNode("ds_0"))); ShardingSphereDatabase database = mockDatabaseWithMultipleResources(); - RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, rule, new ConfigurationProperties(new Properties())); + RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); List routeUnits = new ArrayList<>(actual.getRouteUnits()); assertThat(actual.getRouteUnits().size(), is(1)); assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(), is(routeUnits.get(0).getDataSourceMapper().getActualName())); @@ -154,7 +155,7 @@ public void assertDecorateRouteContextWithSingleDataSource() { RouteContext routeContext = new RouteContext(); routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper("foo_ds", "foo_ds"), Collections.singletonList(new RouteMapper("t_order", "t_order")))); SingleTableSQLRouter sqlRouter = (SingleTableSQLRouter) SQLRouterFactory.getInstances(Collections.singleton(rule)).get(rule); - sqlRouter.decorateRouteContext(routeContext, createLogicSQL(), mockReadwriteSplittingDatabase(), rule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(routeContext, createLogicSQL(), mockReadwriteSplittingDatabase(), rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = routeContext.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is("foo_ds")); } @@ -171,7 +172,7 @@ public void assertDecorateRouteContextWithReadwriteSplittingDataSource() { SingleTableSQLRouter sqlRouter = (SingleTableSQLRouter) SQLRouterFactory.getInstances(Collections.singleton(rule)).get(rule); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), new ShardingSphereRuleMetaData(Collections.singleton(rule)), Collections.emptyMap()); - sqlRouter.decorateRouteContext(routeContext, createLogicSQL(), database, rule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(routeContext, createLogicSQL(), database, rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = routeContext.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is("write_ds")); assertThat(routedDataSourceNames.next(), is("readwrite_ds")); @@ -184,7 +185,7 @@ public void assertDecorateRouteContextWithMultiDataSource() throws SQLException routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.emptyList())); routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.emptyList())); SingleTableSQLRouter sqlRouter = (SingleTableSQLRouter) SQLRouterFactory.getInstances(Collections.singleton(rule)).get(rule); - sqlRouter.decorateRouteContext(routeContext, createLogicSQL(), mockDatabaseWithMultipleResources(), rule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(routeContext, createLogicSQL(), mockDatabaseWithMultipleResources(), rule, new ConfigurationProperties(new Properties()), new ConnectionContext()); Iterator routedDataSourceNames = routeContext.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is("ds_1")); assertThat(routedDataSourceNames.next(), is("ds_0")); diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/TransactionHolder.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/TransactionHolder.java deleted file mode 100644 index 8f72e52cf4410..0000000000000 --- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/TransactionHolder.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.transaction; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -/** - * Transaction holder. - * - *

is transaction or not in current thread.

- */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class TransactionHolder { - - private static final ThreadLocal TRANSACTION = ThreadLocal.withInitial(() -> false); - - private static final ThreadLocal READ_WRITE_SPLIT_REPLICA_ROUTE_HOLDER = new ThreadLocal<>(); - - /** - * Judge is transaction in current thread. - * - * @return is transaction in current thread. - */ - public static boolean isTransaction() { - return TRANSACTION.get(); - } - - /** - * Set transaction in current thread. - */ - public static void setInTransaction() { - TRANSACTION.set(true); - } - - /** - * Get read write split routed replica. - * - * @return routed replica. - */ - public static String getReadWriteSplitRoutedReplica() { - return READ_WRITE_SPLIT_REPLICA_ROUTE_HOLDER.get(); - } - - /** - * Set read write split routed replica. - * - * @param replica routed replica. - */ - public static void setReadWriteSplitRoutedReplica(final String replica) { - READ_WRITE_SPLIT_REPLICA_ROUTE_HOLDER.set(replica); - } - - /** - * Clear transaction. - */ - public static void clear() { - TRANSACTION.remove(); - READ_WRITE_SPLIT_REPLICA_ROUTE_HOLDER.remove(); - } -} diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/JDBCBackendTransactionManager.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/JDBCBackendTransactionManager.java index 1f0dcde46d5e4..f1da4ffbb755b 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/JDBCBackendTransactionManager.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/JDBCBackendTransactionManager.java @@ -22,7 +22,6 @@ import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.transaction.ConnectionSavepointManager; import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine; -import org.apache.shardingsphere.transaction.TransactionHolder; import org.apache.shardingsphere.transaction.core.TransactionType; import org.apache.shardingsphere.transaction.rule.TransactionRule; import org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager; @@ -58,7 +57,7 @@ public JDBCBackendTransactionManager(final JDBCBackendConnection backendConnecti public Void begin() throws SQLException { if (!connection.getConnectionSession().getTransactionStatus().isInTransaction()) { connection.getConnectionSession().getTransactionStatus().setInTransaction(true); - TransactionHolder.setInTransaction(); + connection.getConnectionSession().getConnectionContext().getTransactionConnectionContext().setInTransaction(true); connection.closeHandlers(true); connection.closeConnections(false); } @@ -82,7 +81,7 @@ public Void commit() throws SQLException { } finally { connection.getConnectionSession().getTransactionStatus().setInTransaction(false); connection.getConnectionSession().getTransactionStatus().setRollbackOnly(false); - TransactionHolder.clear(); + connection.getConnectionSession().getConnectionContext().clearTransactionConnectionContext(); connection.getConnectionSession().getConnectionContext().clearCursorConnectionContext(); } } @@ -101,7 +100,7 @@ public Void rollback() throws SQLException { } finally { connection.getConnectionSession().getTransactionStatus().setInTransaction(false); connection.getConnectionSession().getTransactionStatus().setRollbackOnly(false); - TransactionHolder.clear(); + connection.getConnectionSession().getConnectionContext().clearTransactionConnectionContext(); connection.getConnectionSession().getConnectionContext().clearCursorConnectionContext(); } } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionXAHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionXAHandler.java index 74105ab7a32e5..e17f72928e24b 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionXAHandler.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionXAHandler.java @@ -28,7 +28,6 @@ import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; 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 java.sql.SQLException; import java.util.Collections; @@ -76,7 +75,7 @@ public ResponseHeader execute() throws SQLException { throw new SQLException("can not start in a Active transaction"); } ResponseHeader header = backendHandler.execute(); - TransactionHolder.setInTransaction(); + connectionSession.getConnectionContext().getTransactionConnectionContext().setInTransaction(true); return header; case "END": case "PREPARE": @@ -87,7 +86,7 @@ public ResponseHeader execute() throws SQLException { try { return backendHandler.execute(); } finally { - TransactionHolder.clear(); + connectionSession.getConnectionContext().clearTransactionConnectionContext(); connectionSession.getConnectionContext().clearCursorConnectionContext(); } default: diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/JDBCBackendTransactionManagerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/JDBCBackendTransactionManagerTest.java index 5c33a32e75dd6..5c183546b1576 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/JDBCBackendTransactionManagerTest.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/JDBCBackendTransactionManagerTest.java @@ -20,6 +20,7 @@ import lombok.SneakyThrows; import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData; import org.apache.shardingsphere.infra.session.ConnectionContext; +import org.apache.shardingsphere.infra.session.transaction.TransactionConnectionContext; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.JDBCBackendConnection; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; @@ -72,6 +73,8 @@ public void setUp() { when(backendConnection.getConnectionSession()).thenReturn(connectionSession); ConnectionContext connectionContext = mock(ConnectionContext.class); when(connectionSession.getConnectionContext()).thenReturn(connectionContext); + TransactionConnectionContext context = new TransactionConnectionContext(); + when(connectionContext.getTransactionConnectionContext()).thenReturn(context); } private void setTransactionContexts() { diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java index 79df1d8967907..7cd8ff68082a0 100644 --- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java +++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java @@ -128,7 +128,7 @@ private Collection createSQLRewriteUnits() throws IOException, S } LogicSQL logicSQL = new LogicSQL(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters()); ConfigurationProperties props = new ConfigurationProperties(rootConfig.getProps()); - RouteContext routeContext = new SQLRouteEngine(databaseRules, props).route(logicSQL, database); + RouteContext routeContext = new SQLRouteEngine(databaseRules, props).route(logicSQL, database, new ConnectionContext()); SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(database, new ShardingSphereRuleMetaData(Collections.singleton(new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()))), props); ConnectionContext connectionContext = mock(ConnectionContext.class); when(connectionContext.getCursorConnectionContext()).thenReturn(new CursorConnectionContext());