This is an automated email from the ASF dual-hosted git repository.

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 59e8cab  Support transaction rule default type load in JDBC (#12878)
59e8cab is described below

commit 59e8cab321bb9e8f91fe65d56a08c80a11d2a362
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Oct 1 23:08:57 2021 +0800

    Support transaction rule default type load in JDBC (#12878)
    
    * Support transaction type switch in JDBC
    
    * Support transaction type switch in JDBC
---
 .../core/connection/ShardingSphereConnection.java   | 21 ++++++++++++++++++---
 .../core/datasource/ShardingSphereDataSource.java   |  3 +--
 .../shardingsphere/driver/state/DriverState.java    |  4 +---
 .../driver/state/DriverStateContext.java            |  6 ++----
 .../state/circuit/CircuitBreakDriverState.java      |  5 ++---
 .../driver/state/lock/LockDriverState.java          |  3 +--
 .../driver/state/ok/OKDriverState.java              |  6 ++----
 .../driver/executor/AbstractBaseExecutorTest.java   |  4 +++-
 .../driver/jdbc/adapter/ConnectionAdapterTest.java  |  2 +-
 .../connection/ShardingSphereConnectionTest.java    |  9 ++++++---
 .../UnsupportedOperationConnectionTest.java         |  5 ++---
 .../driver/state/DriverStateContextTest.java        |  6 ++----
 .../state/circuit/CircuitBreakDriverStateTest.java  |  3 +--
 .../driver/state/ok/OKDriverStateTest.java          |  4 +++-
 .../transaction/core/TransactionTypeHolder.java     |  2 +-
 .../transaction/core/TransactionTypeHolderTest.java |  7 ++++---
 16 files changed, 50 insertions(+), 40 deletions(-)

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 f46df67..9347d3e 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
@@ -24,12 +24,14 @@ import 
org.apache.shardingsphere.driver.jdbc.adapter.AbstractConnectionAdapter;
 import 
org.apache.shardingsphere.driver.jdbc.core.datasource.metadata.ShardingSphereDatabaseMetaData;
 import 
org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement;
 import 
org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement;
-import org.apache.shardingsphere.mode.manager.ContextManager;
 import 
org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
 import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.ExecutorJDBCManager;
 import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption;
+import org.apache.shardingsphere.mode.manager.ContextManager;
 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;
 import 
org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager;
 
 import javax.sql.DataSource;
@@ -64,14 +66,27 @@ public final class ShardingSphereConnection extends 
AbstractConnectionAdapter im
     @Getter(AccessLevel.NONE)
     private boolean autoCommit = true;
     
-    public ShardingSphereConnection(final String schemaName, final Map<String, 
DataSource> dataSourceMap, final ContextManager contextManager, final 
TransactionType transactionType) {
+    public ShardingSphereConnection(final String schemaName, final Map<String, 
DataSource> dataSourceMap, final ContextManager contextManager) {
         this.schemaName = schemaName;
         this.dataSourceMap = dataSourceMap;
         this.contextManager = contextManager;
-        this.transactionType = transactionType;
+        transactionType = getTransactionType(contextManager);
         transactionManager = 
contextManager.getTransactionContexts().getEngines().get(schemaName).getTransactionManager(transactionType);
     }
     
+    private TransactionType getTransactionType(final ContextManager 
contextManager) {
+        if (null != TransactionTypeHolder.get()) {
+            return TransactionTypeHolder.get();
+        }
+        Collection<TransactionRule> rules = 
contextManager.getMetaDataContexts().getGlobalRuleMetaData().findRules(TransactionRule.class);
+        if (rules.isEmpty()) {
+            return TransactionType.LOCAL;
+        }
+        TransactionType result = rules.iterator().next().getDefaultType();
+        TransactionTypeHolder.set(result);
+        return result;
+    }
+    
     /**
      * Get database connection.
      *
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
index 51d22e4..48e0f7d 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
@@ -27,7 +27,6 @@ import 
org.apache.shardingsphere.infra.config.scope.GlobalRuleConfiguration;
 import org.apache.shardingsphere.infra.config.scope.SchemaRuleConfiguration;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilderFactory;
-import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -78,7 +77,7 @@ public final class ShardingSphereDataSource extends 
AbstractDataSourceAdapter im
     
     @Override
     public Connection getConnection() {
-        return DriverStateContext.getConnection(schemaName, 
getDataSourceMap(), contextManager, TransactionTypeHolder.get());
+        return DriverStateContext.getConnection(schemaName, 
getDataSourceMap(), contextManager);
     }
     
     @Override
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverState.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverState.java
index 4a987e1..64f4fd3 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverState.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverState.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.driver.state;
 
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.spi.typed.TypedSPI;
-import org.apache.shardingsphere.transaction.core.TransactionType;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -36,8 +35,7 @@ public interface DriverState extends TypedSPI {
      * @param schemaName schema name
      * @param dataSourceMap data source map
      * @param contextManager context manager
-     * @param transactionType transaction type
      * @return connection
      */
-    Connection getConnection(String schemaName, Map<String, DataSource> 
dataSourceMap, ContextManager contextManager, TransactionType transactionType);
+    Connection getConnection(String schemaName, Map<String, DataSource> 
dataSourceMap, ContextManager contextManager);
 }
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
index 0192d8f..d85c478 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
@@ -21,7 +21,6 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.transaction.core.TransactionType;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -53,10 +52,9 @@ public final class DriverStateContext {
      * @param schemaName schema name
      * @param dataSourceMap data source map
      * @param contextManager context manager
-     * @param transactionType transaction type
      * @return connection
      */
-    public static Connection getConnection(final String schemaName, final 
Map<String, DataSource> dataSourceMap, final ContextManager contextManager, 
final TransactionType transactionType) {
-        return 
STATES.get(contextManager.getStateContext().getCurrentState().name()).getConnection(schemaName,
 dataSourceMap, contextManager, transactionType);
+    public static Connection getConnection(final String schemaName, final 
Map<String, DataSource> dataSourceMap, final ContextManager contextManager) {
+        return 
STATES.get(contextManager.getStateContext().getCurrentState().name()).getConnection(schemaName,
 dataSourceMap, contextManager);
     }
 }
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/circuit/CircuitBreakDriverState.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/circuit/CircuitBreakDriverState.java
index d477262..8a44896 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/circuit/CircuitBreakDriverState.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/circuit/CircuitBreakDriverState.java
@@ -17,10 +17,9 @@
 
 package org.apache.shardingsphere.driver.state.circuit;
 
-import 
org.apache.shardingsphere.driver.state.circuit.datasource.CircuitBreakerDataSource;
 import org.apache.shardingsphere.driver.state.DriverState;
+import 
org.apache.shardingsphere.driver.state.circuit.datasource.CircuitBreakerDataSource;
 import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.transaction.core.TransactionType;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -32,7 +31,7 @@ import java.util.Map;
 public final class CircuitBreakDriverState implements DriverState {
     
     @Override
-    public Connection getConnection(final String schemaName, final Map<String, 
DataSource> dataSourceMap, final ContextManager contextManager, final 
TransactionType transactionType) {
+    public Connection getConnection(final String schemaName, final Map<String, 
DataSource> dataSourceMap, final ContextManager contextManager) {
         return new CircuitBreakerDataSource().getConnection();
     }
     
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/lock/LockDriverState.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/lock/LockDriverState.java
index bff0b7c..8189b73 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/lock/LockDriverState.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/lock/LockDriverState.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.driver.state.lock;
 
 import org.apache.shardingsphere.driver.state.DriverState;
 import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.transaction.core.TransactionType;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -31,7 +30,7 @@ import java.util.Map;
 public final class LockDriverState implements DriverState {
     
     @Override
-    public Connection getConnection(final String schemaName, final Map<String, 
DataSource> dataSourceMap, final ContextManager contextManager, final 
TransactionType transactionType) {
+    public Connection getConnection(final String schemaName, final Map<String, 
DataSource> dataSourceMap, final ContextManager contextManager) {
         // TODO
         throw new UnsupportedOperationException("LockDriverState");
     }
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/ok/OKDriverState.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/ok/OKDriverState.java
index 52cfcf7..c2fb01e 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/ok/OKDriverState.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/ok/OKDriverState.java
@@ -20,8 +20,6 @@ package org.apache.shardingsphere.driver.state.ok;
 import 
org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
 import org.apache.shardingsphere.driver.state.DriverState;
 import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.transaction.core.TransactionType;
-import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -33,8 +31,8 @@ import java.util.Map;
 public final class OKDriverState implements DriverState {
     
     @Override
-    public Connection getConnection(final String schemaName, final Map<String, 
DataSource> dataSourceMap, final ContextManager contextManager, final 
TransactionType transactionType) {
-        return new ShardingSphereConnection(schemaName, dataSourceMap, 
contextManager, TransactionTypeHolder.get());
+    public Connection getConnection(final String schemaName, final Map<String, 
DataSource> dataSourceMap, final ContextManager contextManager) {
+        return new ShardingSphereConnection(schemaName, dataSourceMap, 
contextManager);
     }
     
     @Override
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/executor/AbstractBaseExecutorTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/executor/AbstractBaseExecutorTest.java
index 4900e18..5ed1165 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/executor/AbstractBaseExecutorTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/executor/AbstractBaseExecutorTest.java
@@ -30,6 +30,7 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
 import 
org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
 import org.apache.shardingsphere.transaction.context.TransactionContexts;
 import org.apache.shardingsphere.transaction.core.TransactionType;
+import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.runner.RunWith;
@@ -58,7 +59,8 @@ public abstract class AbstractBaseExecutorTest {
     public void setUp() throws SQLException {
         SQLExecutorExceptionHandler.setExceptionThrown(true);
         executorEngine = new 
ExecutorEngine(Runtime.getRuntime().availableProcessors());
-        connection = new ShardingSphereConnection(DefaultSchema.LOGIC_NAME, 
mockDataSourceMap(), mockContextManager(), TransactionType.LOCAL);
+        TransactionTypeHolder.set(TransactionType.LOCAL);
+        connection = new ShardingSphereConnection(DefaultSchema.LOGIC_NAME, 
mockDataSourceMap(), mockContextManager());
     }
     
     private ContextManager mockContextManager() {
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java
index 4b766c2..d432d19 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java
@@ -131,7 +131,7 @@ public final class ConnectionAdapterTest {
     }
     
     private ShardingSphereConnection mockShardingSphereConnection(final 
Connection... connections) {
-        ShardingSphereConnection result = new 
ShardingSphereConnection(DefaultSchema.LOGIC_NAME, Collections.emptyMap(), 
mock(ContextManager.class, RETURNS_DEEP_STUBS), TransactionType.LOCAL);
+        ShardingSphereConnection result = new 
ShardingSphereConnection(DefaultSchema.LOGIC_NAME, Collections.emptyMap(), 
mock(ContextManager.class, RETURNS_DEEP_STUBS));
         result.getCachedConnections().putAll("", Arrays.asList(connections));
         return result;
     }
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 e29db7c..54ecef1 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
@@ -91,7 +91,8 @@ public final class ShardingSphereConnectionTest {
         ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
         
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
         
when(contextManager.getTransactionContexts()).thenReturn(transactionContexts);
-        connection = new ShardingSphereConnection(DefaultSchema.LOGIC_NAME, 
dataSourceMap, contextManager, TransactionType.LOCAL);
+        TransactionTypeHolder.set(TransactionType.LOCAL);
+        connection = new ShardingSphereConnection(DefaultSchema.LOGIC_NAME, 
dataSourceMap, contextManager);
     }
     
     @After
@@ -128,7 +129,8 @@ public final class ShardingSphereConnectionTest {
         ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
         
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
         
when(contextManager.getTransactionContexts()).thenReturn(transactionContexts);
-        connection = new ShardingSphereConnection(connection.getSchemaName(), 
dataSourceMap, contextManager, TransactionType.XA);
+        TransactionTypeHolder.set(TransactionType.XA);
+        connection = new ShardingSphereConnection(connection.getSchemaName(), 
dataSourceMap, contextManager);
         connection.setAutoCommit(false);
         
assertTrue(XAShardingSphereTransactionManagerFixture.getInvocations().contains(TransactionOperationType.BEGIN));
         connection.commit();
@@ -142,7 +144,8 @@ public final class ShardingSphereConnectionTest {
         ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
         
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
         
when(contextManager.getTransactionContexts()).thenReturn(transactionContexts);
-        connection = new ShardingSphereConnection(connection.getSchemaName(), 
dataSourceMap, contextManager, TransactionType.BASE);
+        TransactionTypeHolder.set(TransactionType.BASE);
+        connection = new ShardingSphereConnection(connection.getSchemaName(), 
dataSourceMap, contextManager);
         connection.setAutoCommit(false);
         
assertTrue(BASEShardingSphereTransactionManagerFixture.getInvocations().contains(TransactionOperationType.BEGIN));
         connection.commit();
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java
index ce50005..45950bd 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java
@@ -18,9 +18,8 @@
 package org.apache.shardingsphere.driver.jdbc.unsupported;
 
 import 
org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
-import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.infra.database.DefaultSchema;
-import org.apache.shardingsphere.transaction.core.TransactionType;
+import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.junit.Test;
 
 import java.sql.SQLException;
@@ -34,7 +33,7 @@ import static org.mockito.Mockito.mock;
 public final class UnsupportedOperationConnectionTest {
     
     private final ShardingSphereConnection shardingSphereConnection = new 
ShardingSphereConnection(
-            DefaultSchema.LOGIC_NAME, Collections.emptyMap(), 
mock(ContextManager.class, RETURNS_DEEP_STUBS), TransactionType.LOCAL);
+            DefaultSchema.LOGIC_NAME, Collections.emptyMap(), 
mock(ContextManager.class, RETURNS_DEEP_STUBS));
     
     @Test(expected = SQLFeatureNotSupportedException.class)
     public void assertPrepareCall() throws SQLException {
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java
index 5746d80..3ebb9ea 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java
@@ -18,12 +18,11 @@
 package org.apache.shardingsphere.driver.state;
 
 import 
org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
+import org.apache.shardingsphere.infra.database.DefaultSchema;
 import org.apache.shardingsphere.infra.state.StateContext;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
-import org.apache.shardingsphere.infra.database.DefaultSchema;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
-import org.apache.shardingsphere.transaction.core.TransactionType;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -55,8 +54,7 @@ public final class DriverStateContextTest {
     
     @Test
     public void assertGetConnectionWithOkState() {
-        Connection actual = DriverStateContext.getConnection(
-                DefaultSchema.LOGIC_NAME, Collections.singletonMap("ds", 
mock(DataSource.class, RETURNS_DEEP_STUBS)), contextManager, 
TransactionType.LOCAL);
+        Connection actual = 
DriverStateContext.getConnection(DefaultSchema.LOGIC_NAME, 
Collections.singletonMap("ds", mock(DataSource.class, RETURNS_DEEP_STUBS)), 
contextManager);
         assertThat(actual, instanceOf(ShardingSphereConnection.class));
     }
 }
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/circuit/CircuitBreakDriverStateTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/circuit/CircuitBreakDriverStateTest.java
index 7c240dad..060f919 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/circuit/CircuitBreakDriverStateTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/circuit/CircuitBreakDriverStateTest.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.driver.state.circuit;
 import 
org.apache.shardingsphere.driver.state.circuit.connection.CircuitBreakerConnection;
 import org.apache.shardingsphere.infra.database.DefaultSchema;
 import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.transaction.core.TransactionType;
 import org.junit.Test;
 
 import java.sql.Connection;
@@ -35,7 +34,7 @@ public final class CircuitBreakDriverStateTest {
     
     @Test
     public void assertGetConnection() {
-        Connection actual = new 
CircuitBreakDriverState().getConnection(DefaultSchema.LOGIC_NAME, 
Collections.emptyMap(), mock(ContextManager.class, RETURNS_DEEP_STUBS), 
TransactionType.LOCAL);
+        Connection actual = new 
CircuitBreakDriverState().getConnection(DefaultSchema.LOGIC_NAME, 
Collections.emptyMap(), mock(ContextManager.class, RETURNS_DEEP_STUBS));
         assertThat(actual, instanceOf(CircuitBreakerConnection.class));
     }
 }
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/ok/OKDriverStateTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/ok/OKDriverStateTest.java
index 3e33950..20c4078 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/ok/OKDriverStateTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/ok/OKDriverStateTest.java
@@ -21,6 +21,7 @@ import 
org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConne
 import org.apache.shardingsphere.infra.database.DefaultSchema;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.transaction.core.TransactionType;
+import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
 import org.junit.Test;
 
 import javax.sql.DataSource;
@@ -36,8 +37,9 @@ public final class OKDriverStateTest {
     
     @Test
     public void assertGetConnection() {
+        TransactionTypeHolder.set(TransactionType.LOCAL);
         Connection actual = new OKDriverState().getConnection(
-                DefaultSchema.LOGIC_NAME, Collections.singletonMap("ds", 
mock(DataSource.class, RETURNS_DEEP_STUBS)), mock(ContextManager.class, 
RETURNS_DEEP_STUBS), TransactionType.LOCAL);
+                DefaultSchema.LOGIC_NAME, Collections.singletonMap("ds", 
mock(DataSource.class, RETURNS_DEEP_STUBS)), mock(ContextManager.class, 
RETURNS_DEEP_STUBS));
         assertThat(actual, instanceOf(ShardingSphereConnection.class));
     }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/TransactionTypeHolder.java
 
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/TransactionTypeHolder.java
index 1560077..5fcb48f 100644
--- 
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/TransactionTypeHolder.java
+++ 
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/TransactionTypeHolder.java
@@ -26,7 +26,7 @@ import lombok.NoArgsConstructor;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class TransactionTypeHolder {
     
-    private static final ThreadLocal<TransactionType> CONTEXT = 
ThreadLocal.withInitial(() -> TransactionType.LOCAL);
+    private static final ThreadLocal<TransactionType> CONTEXT = new 
ThreadLocal<>();
     
     /**
      * Get transaction type for current thread.
diff --git 
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/TransactionTypeHolderTest.java
 
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/TransactionTypeHolderTest.java
index 42cdbe9..04c9d80 100644
--- 
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/TransactionTypeHolderTest.java
+++ 
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/TransactionTypeHolderTest.java
@@ -21,6 +21,7 @@ import org.junit.Before;
 import org.junit.Test;
 
 import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 
 public final class TransactionTypeHolderTest {
@@ -31,8 +32,8 @@ public final class TransactionTypeHolderTest {
     }
     
     @Test
-    public void assertGetWithDefaultValue() {
-        assertThat(TransactionTypeHolder.get(), is(TransactionType.LOCAL));
+    public void assertGetWithoutSetValue() {
+        assertNull(TransactionTypeHolder.get());
     }
     
     @Test
@@ -45,6 +46,6 @@ public final class TransactionTypeHolderTest {
     public void assertClear() {
         TransactionTypeHolder.set(TransactionType.XA);
         TransactionTypeHolder.clear();
-        assertThat(TransactionTypeHolder.get(), is(TransactionType.LOCAL));
+        assertNull(TransactionTypeHolder.get());
     }
 }

Reply via email to