You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/12/15 07:34:15 UTC
[shardingsphere] branch master updated: xa transaction manager type
spi (#8620)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 bfa6b71 xa transaction manager type spi (#8620)
bfa6b71 is described below
commit bfa6b713d4d86f893b6154338f04fce88b238c6e
Author: xiaoyu <54...@qq.com>
AuthorDate: Tue Dec 15 15:33:52 2020 +0800
xa transaction manager type spi (#8620)
* xa transaction manager type spi
* fix checkstyle
---
.../transaction/GovernanceTransactionContexts.java | 7 ++-
.../GovernanceTransactionContextsTest.java | 7 +--
.../properties/ConfigurationPropertyKey.java | 2 +-
.../core/datasource/ShardingSphereDataSource.java | 8 ++--
.../AbstractShardingTransactionManagerFixture.java | 2 +-
.../GovernanceShardingSphereDataSource.java | 13 +++--
.../shardingsphere-proxy-bootstrap/pom.xml | 20 --------
.../impl/AbstractBootstrapInitializer.java | 12 ++---
.../impl/GovernanceBootstrapInitializer.java | 4 +-
.../impl/StandardBootstrapInitializer.java | 2 +-
.../src/main/resources/conf/server.yaml | 1 +
.../impl/AbstractBootstrapInitializerTest.java | 4 +-
.../impl/GovernanceBootstrapInitializerTest.java | 3 +-
.../impl/StandardBootstrapInitializerTest.java | 3 +-
.../manager/AtomikosTransactionManager.java | 10 ++--
.../manager/AtomikosTransactionManagerTest.java | 6 ---
.../manager/BitronixXATransactionManager.java | 7 +--
.../manager/BitronixXATransactionManagerTest.java | 9 +---
.../xa/XAShardingTransactionManager.java | 7 +--
.../xa/manager/XATransactionManagerLoader.java | 42 +++++++++--------
.../xa/XAShardingTransactionManagerTest.java | 39 +++++----------
.../xa/manager/XATransactionManagerLoaderTest.java | 3 +-
.../manager/NarayanaXATransactionManager.java | 13 +++--
.../manager/NarayanaXATransactionManagerTest.java | 7 ---
.../at/SeataATShardingTransactionManager.java | 2 +-
.../at/SeataATShardingTransactionManagerTest.java | 2 +-
.../ShardingTransactionManagerEngine.java | 5 +-
...anagerType.java => TransactionManagerType.java} | 20 ++++----
.../core/XATransactionManagerTypeHolder.java | 55 ----------------------
.../spi/ShardingTransactionManager.java | 3 +-
.../ShardingTransactionManagerEngineTest.java | 3 +-
.../OtherShardingTransactionManagerFixture.java | 2 +-
.../fixture/ShardingTransactionManagerFixture.java | 2 +-
33 files changed, 122 insertions(+), 203 deletions(-)
diff --git a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/transaction/GovernanceTransactionContexts.java b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/transaction/GovernanceTransactionContexts.java
index 2b5adc4..513fd92 100644
--- a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/transaction/GovernanceTransactionContexts.java
+++ b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/transaction/GovernanceTransactionContexts.java
@@ -32,8 +32,11 @@ public final class GovernanceTransactionContexts implements TransactionContexts
private final TransactionContexts contexts;
- public GovernanceTransactionContexts(final TransactionContexts contexts) {
+ private final String transactionManagerType;
+
+ public GovernanceTransactionContexts(final TransactionContexts contexts, final String transactionManagerType) {
this.contexts = contexts;
+ this.transactionManagerType = transactionManagerType;
ShardingSphereEventBus.getInstance().register(this);
}
@@ -65,7 +68,7 @@ public final class GovernanceTransactionContexts implements TransactionContexts
oldEngine.close();
}
ShardingTransactionManagerEngine newEngine = new ShardingTransactionManagerEngine();
- newEngine.init(event.getDatabaseType(), event.getDataSources());
+ newEngine.init(event.getDatabaseType(), event.getDataSources(), transactionManagerType);
contexts.getEngines().put(event.getSchemaName(), newEngine);
}
}
diff --git a/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/transaction/GovernanceTransactionContextsTest.java b/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/transaction/GovernanceTransactionContextsTest.java
index 970264a..80b38dd 100644
--- a/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/transaction/GovernanceTransactionContextsTest.java
+++ b/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/transaction/GovernanceTransactionContextsTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.governance.context.transaction;
import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourceChangeCompletedEvent;
import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine;
import org.apache.shardingsphere.transaction.context.TransactionContexts;
+import org.apache.shardingsphere.transaction.core.TransactionManagerType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -54,14 +55,14 @@ public final class GovernanceTransactionContextsTest {
public void assertNewInstance() {
when(transactionContexts.getDefaultTransactionManagerEngine()).thenReturn(engine);
when(transactionContexts.getEngines()).thenReturn(Collections.singletonMap("name", engine));
- GovernanceTransactionContexts actual = new GovernanceTransactionContexts(transactionContexts);
+ GovernanceTransactionContexts actual = new GovernanceTransactionContexts(transactionContexts, TransactionManagerType.ATOMIKOS.getType());
assertThat(actual.getEngines(), is(Collections.singletonMap("name", engine)));
assertThat(actual.getDefaultTransactionManagerEngine(), is(engine));
}
@Test
public void assertClose() throws Exception {
- GovernanceTransactionContexts actual = new GovernanceTransactionContexts(transactionContexts);
+ GovernanceTransactionContexts actual = new GovernanceTransactionContexts(transactionContexts, TransactionManagerType.ATOMIKOS.getType());
actual.close();
verify(transactionContexts).close();
}
@@ -71,7 +72,7 @@ public final class GovernanceTransactionContextsTest {
when(event.getSchemaName()).thenReturn("name");
when(transactionContexts.getEngines()).thenReturn(engineMap);
when(engineMap.remove(eq("name"))).thenReturn(engine);
- GovernanceTransactionContexts actual = new GovernanceTransactionContexts(transactionContexts);
+ GovernanceTransactionContexts actual = new GovernanceTransactionContexts(transactionContexts, TransactionManagerType.ATOMIKOS.getType());
actual.renew(event);
verify(engine).close();
verify(engineMap).put(eq("name"), any(ShardingTransactionManagerEngine.class));
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
index 3055a15..66f746d 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
@@ -111,7 +111,7 @@ public enum ConfigurationPropertyKey implements TypedPropertyKey {
* ShardingSphere-Proxy will run with XA transaction with bitronix.
* </p>
*/
- PROXY_XA_TRANSACTION_MANAGER_TYPE("proxy-xa-transaction-manager-type", "atomikos", String.class),
+ TRANSACTION_MANAGER_TYPE("transaction-manager-type", "atomikos", String.class),
/**
* Whether enable opentracing for ShardingSphere-Proxy.
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 5034d80..6c79c83 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
@@ -22,6 +22,7 @@ import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContextsBuilder;
import org.apache.shardingsphere.infra.database.DefaultSchema;
@@ -52,12 +53,13 @@ public final class ShardingSphereDataSource extends AbstractUnsupportedOperation
public ShardingSphereDataSource(final Map<String, DataSource> dataSourceMap, final Collection<RuleConfiguration> configurations, final Properties props) throws SQLException {
metaDataContexts = new MetaDataContextsBuilder(
Collections.singletonMap(DefaultSchema.LOGIC_NAME, dataSourceMap), Collections.singletonMap(DefaultSchema.LOGIC_NAME, configurations), props).build();
- transactionContexts = createTransactionContexts(metaDataContexts.getDefaultMetaData().getResource().getDatabaseType(), dataSourceMap);
+ String transactionMangerType = metaDataContexts.getProps().getValue(ConfigurationPropertyKey.TRANSACTION_MANAGER_TYPE);
+ transactionContexts = createTransactionContexts(metaDataContexts.getDefaultMetaData().getResource().getDatabaseType(), dataSourceMap, transactionMangerType);
}
- private TransactionContexts createTransactionContexts(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
+ private TransactionContexts createTransactionContexts(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final String transactionMangerType) {
ShardingTransactionManagerEngine engine = new ShardingTransactionManagerEngine();
- engine.init(databaseType, dataSourceMap);
+ engine.init(databaseType, dataSourceMap, transactionMangerType);
return new StandardTransactionContexts(Collections.singletonMap(DefaultSchema.LOGIC_NAME, engine));
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/fixture/AbstractShardingTransactionManagerFixture.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/fixture/AbstractShardingTransactionManagerFixture.java
index f6e9a3d..1489dfb 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/fixture/AbstractShardingTransactionManagerFixture.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/fixture/AbstractShardingTransactionManagerFixture.java
@@ -46,7 +46,7 @@ public abstract class AbstractShardingTransactionManagerFixture implements Shard
}
@Override
- public final void init(final DatabaseType databaseType, final Collection<ResourceDataSource> resourceDataSources) {
+ public final void init(final DatabaseType databaseType, final Collection<ResourceDataSource> resourceDataSources, final String transactionMangerType) {
for (ResourceDataSource each : resourceDataSources) {
dataSourceMap.put(each.getOriginalName(), each.getDataSource());
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java
index 334d314..dd7e77b 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java
@@ -28,6 +28,7 @@ import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContextsBuilder;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
@@ -60,14 +61,18 @@ public final class GovernanceShardingSphereDataSource extends AbstractUnsupporte
public GovernanceShardingSphereDataSource(final GovernanceConfiguration governanceConfig) throws SQLException {
GovernanceFacade governanceFacade = createGovernanceFacade(governanceConfig);
metaDataContexts = new GovernanceMetaDataContexts(createMetaDataContexts(governanceFacade), governanceFacade);
- transactionContexts = createTransactionContexts(metaDataContexts.getDefaultMetaData().getResource().getDatabaseType(), metaDataContexts.getDefaultMetaData().getResource().getDataSources());
+ String transactionMangerType = metaDataContexts.getProps().getValue(ConfigurationPropertyKey.TRANSACTION_MANAGER_TYPE);
+ transactionContexts = createTransactionContexts(metaDataContexts.getDefaultMetaData().getResource().getDatabaseType(),
+ metaDataContexts.getDefaultMetaData().getResource().getDataSources(), transactionMangerType);
}
public GovernanceShardingSphereDataSource(final Map<String, DataSource> dataSourceMap, final Collection<RuleConfiguration> ruleConfigs,
final Properties props, final GovernanceConfiguration governanceConfig) throws SQLException {
GovernanceFacade governanceFacade = createGovernanceFacade(governanceConfig);
metaDataContexts = new GovernanceMetaDataContexts(createMetaDataContexts(dataSourceMap, ruleConfigs, props), governanceFacade);
- transactionContexts = createTransactionContexts(metaDataContexts.getDefaultMetaData().getResource().getDatabaseType(), metaDataContexts.getDefaultMetaData().getResource().getDataSources());
+ String transactionMangerType = metaDataContexts.getProps().getValue(ConfigurationPropertyKey.TRANSACTION_MANAGER_TYPE);
+ transactionContexts = createTransactionContexts(metaDataContexts.getDefaultMetaData().getResource().getDatabaseType(),
+ metaDataContexts.getDefaultMetaData().getResource().getDataSources(), transactionMangerType);
uploadLocalConfiguration(governanceFacade);
}
@@ -95,9 +100,9 @@ public final class GovernanceShardingSphereDataSource extends AbstractUnsupporte
return metaDataContextsBuilder.build();
}
- private TransactionContexts createTransactionContexts(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
+ private TransactionContexts createTransactionContexts(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final String transactionMangerType) {
ShardingTransactionManagerEngine engine = new ShardingTransactionManagerEngine();
- engine.init(databaseType, dataSourceMap);
+ engine.init(databaseType, dataSourceMap, transactionMangerType);
return new StandardTransactionContexts(Collections.singletonMap(DefaultSchema.LOGIC_NAME, engine));
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/pom.xml b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/pom.xml
index 2b51cfd..1811386 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/pom.xml
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/pom.xml
@@ -156,25 +156,5 @@
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
- <dependency>
- <groupId>org.jboss.narayana.jta</groupId>
- <artifactId>jta</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.jboss.narayana.jts</groupId>
- <artifactId>narayana-jts-integration</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.jboss</groupId>
- <artifactId>jboss-transaction-spi</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.jboss.logging</groupId>
- <artifactId>jboss-logging</artifactId>
- <scope>runtime</scope>
- </dependency>
</dependencies>
</project>
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
index 849ef93..89511af 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
@@ -35,8 +35,6 @@ import org.apache.shardingsphere.tracing.opentracing.OpenTracingTracer;
import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine;
import org.apache.shardingsphere.transaction.context.TransactionContexts;
import org.apache.shardingsphere.transaction.context.impl.StandardTransactionContexts;
-import org.apache.shardingsphere.transaction.core.XATransactionManagerType;
-import org.apache.shardingsphere.transaction.core.XATransactionManagerTypeHolder;
import javax.sql.DataSource;
import java.sql.SQLException;
@@ -59,7 +57,8 @@ public abstract class AbstractBootstrapInitializer implements BootstrapInitializ
public final void init(final YamlProxyConfiguration yamlConfig, final int port) throws SQLException {
ProxyConfiguration proxyConfig = getProxyConfiguration(yamlConfig);
MetaDataContexts metaDataContexts = decorateMetaDataContexts(createMetaDataContexts(proxyConfig));
- TransactionContexts transactionContexts = decorateTransactionContexts(createTransactionContexts(metaDataContexts));
+ String transactionMangerType = metaDataContexts.getProps().getValue(ConfigurationPropertyKey.TRANSACTION_MANAGER_TYPE);
+ TransactionContexts transactionContexts = decorateTransactionContexts(createTransactionContexts(metaDataContexts), transactionMangerType);
ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
initOpenTracing();
setDatabaseServerInfo();
@@ -87,12 +86,11 @@ public abstract class AbstractBootstrapInitializer implements BootstrapInitializ
private TransactionContexts createTransactionContexts(final MetaDataContexts metaDataContexts) {
Map<String, ShardingTransactionManagerEngine> transactionManagerEngines = new HashMap<>(metaDataContexts.getAllSchemaNames().size(), 1);
- XATransactionManagerType transactionType = XATransactionManagerType.valueFrom(metaDataContexts.getProps().getValue(ConfigurationPropertyKey.PROXY_XA_TRANSACTION_MANAGER_TYPE));
- XATransactionManagerTypeHolder.set(transactionType);
+ String transactionMangerType = metaDataContexts.getProps().getValue(ConfigurationPropertyKey.TRANSACTION_MANAGER_TYPE);
for (String each : metaDataContexts.getAllSchemaNames()) {
ShardingTransactionManagerEngine engine = new ShardingTransactionManagerEngine();
ShardingSphereResource resource = metaDataContexts.getMetaData(each).getResource();
- engine.init(resource.getDatabaseType(), resource.getDataSources());
+ engine.init(resource.getDatabaseType(), resource.getDataSources(), transactionMangerType);
transactionManagerEngines.put(each, engine);
}
return new StandardTransactionContexts(transactionManagerEngines);
@@ -117,7 +115,7 @@ public abstract class AbstractBootstrapInitializer implements BootstrapInitializ
protected abstract MetaDataContexts decorateMetaDataContexts(MetaDataContexts metaDataContexts);
- protected abstract TransactionContexts decorateTransactionContexts(TransactionContexts transactionContexts);
+ protected abstract TransactionContexts decorateTransactionContexts(TransactionContexts transactionContexts, String transactionManagerType);
protected abstract void initLockContext();
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java
index df3d0ce..f052c14 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java
@@ -121,8 +121,8 @@ public final class GovernanceBootstrapInitializer extends AbstractBootstrapIniti
}
@Override
- protected TransactionContexts decorateTransactionContexts(final TransactionContexts transactionContexts) {
- return new GovernanceTransactionContexts(transactionContexts);
+ protected TransactionContexts decorateTransactionContexts(final TransactionContexts transactionContexts, final String transactionManagerType) {
+ return new GovernanceTransactionContexts(transactionContexts, transactionManagerType);
}
@Override
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializer.java
index 64dc027..a6c3371 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializer.java
@@ -41,7 +41,7 @@ public final class StandardBootstrapInitializer extends AbstractBootstrapInitial
}
@Override
- protected TransactionContexts decorateTransactionContexts(final TransactionContexts transactionContexts) {
+ protected TransactionContexts decorateTransactionContexts(final TransactionContexts transactionContexts, final String transactionManagerType) {
return transactionContexts;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml
index 7e52f84..cc8ddec 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml
@@ -50,6 +50,7 @@
# # XA: Proxy will run with XA transaction.
# # BASE: Proxy will run with B.A.S.E transaction.
# proxy-transaction-type: LOCAL
+# transaction-manager-type: atomikos
# proxy-opentracing-enabled: false
# proxy-hint-enabled: false
# query-with-cipher-column: true
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializerTest.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializerTest.java
index 1140045..ef63677 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializerTest.java
@@ -78,11 +78,11 @@ public abstract class AbstractBootstrapInitializerTest {
doReturn(mock(ProxyConfiguration.class)).when(abstractBootstrapInitializer).getProxyConfiguration(any());
MetaDataContexts metaDataContexts = mock(MetaDataContexts.class);
ConfigurationProperties props = mock(ConfigurationProperties.class);
- when(props.getValue(ConfigurationPropertyKey.PROXY_XA_TRANSACTION_MANAGER_TYPE)).thenReturn("atomikos");
+ when(props.getValue(ConfigurationPropertyKey.TRANSACTION_MANAGER_TYPE)).thenReturn("atomikos");
when(props.getValue(ConfigurationPropertyKey.PROXY_OPENTRACING_ENABLED)).thenReturn(Boolean.FALSE);
when(metaDataContexts.getProps()).thenReturn(props);
doReturn(metaDataContexts).when(abstractBootstrapInitializer).decorateMetaDataContexts(any());
- doReturn(mock(TransactionContexts.class)).when(abstractBootstrapInitializer).decorateTransactionContexts(any());
+ doReturn(mock(TransactionContexts.class)).when(abstractBootstrapInitializer).decorateTransactionContexts(any(), any());
YamlProxyConfiguration yamlConfig = mock(YamlProxyConfiguration.class);
abstractBootstrapInitializer.init(yamlConfig, eq(anyInt()));
verify(shardingSphereProxy).start(anyInt());
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializerTest.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializerTest.java
index efc426b..853c605 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializerTest.java
@@ -37,6 +37,7 @@ import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfi
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.transaction.context.TransactionContexts;
+import org.apache.shardingsphere.transaction.core.TransactionManagerType;
import org.junit.Test;
import java.io.IOException;
@@ -220,7 +221,7 @@ public final class GovernanceBootstrapInitializerTest extends AbstractBootstrapI
@Test
public void assertDecorateTransactionContexts() {
TransactionContexts transactionContexts = mock(TransactionContexts.class);
- TransactionContexts actualTransactionContexts = getInitializer().decorateTransactionContexts(transactionContexts);
+ TransactionContexts actualTransactionContexts = getInitializer().decorateTransactionContexts(transactionContexts, TransactionManagerType.ATOMIKOS.getType());
assertNotNull(actualTransactionContexts);
assertThat(actualTransactionContexts, instanceOf(GovernanceTransactionContexts.class));
assertThat(actualTransactionContexts.getEngines(), is(transactionContexts.getEngines()));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializerTest.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializerTest.java
index 886250d..f198cf6 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializerTest.java
@@ -35,6 +35,7 @@ import org.apache.shardingsphere.proxy.config.yaml.YamlProxyServerConfiguration;
import org.apache.shardingsphere.proxy.fixture.FixtureRuleConfiguration;
import org.apache.shardingsphere.proxy.fixture.FixtureYamlRuleConfiguration;
import org.apache.shardingsphere.transaction.context.TransactionContexts;
+import org.apache.shardingsphere.transaction.core.TransactionManagerType;
import org.junit.Test;
import java.util.Collection;
@@ -193,7 +194,7 @@ public final class StandardBootstrapInitializerTest extends AbstractBootstrapIni
@Test
public void assertDecorateTransactionContexts() {
TransactionContexts transactionContexts = mock(TransactionContexts.class);
- assertThat(getInitializer().decorateTransactionContexts(transactionContexts), is(transactionContexts));
+ assertThat(getInitializer().decorateTransactionContexts(transactionContexts, TransactionManagerType.ATOMIKOS.getType()), is(transactionContexts));
}
protected void doEnvironmentPrepare() {
diff --git a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-atomikos/src/main/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManager.java b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-atomikos/src/main/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManager.java
index 738a28f..e0a4501 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-atomikos/src/main/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManager.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-atomikos/src/main/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManager.java
@@ -21,7 +21,7 @@ import com.atomikos.icatch.config.UserTransactionService;
import com.atomikos.icatch.config.UserTransactionServiceImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import lombok.SneakyThrows;
-import org.apache.shardingsphere.transaction.core.XATransactionManagerType;
+import org.apache.shardingsphere.transaction.core.TransactionManagerType;
import org.apache.shardingsphere.transaction.xa.spi.SingleXAResource;
import org.apache.shardingsphere.transaction.xa.spi.XATransactionManager;
@@ -35,12 +35,14 @@ import javax.transaction.TransactionManager;
*/
public final class AtomikosTransactionManager implements XATransactionManager {
- private final UserTransactionManager transactionManager = new UserTransactionManager();
+ private UserTransactionManager transactionManager;
- private final UserTransactionService userTransactionService = new UserTransactionServiceImp();
+ private UserTransactionService userTransactionService;
@Override
public void init() {
+ transactionManager = new UserTransactionManager();
+ userTransactionService = new UserTransactionServiceImp();
userTransactionService.init();
}
@@ -72,6 +74,6 @@ public final class AtomikosTransactionManager implements XATransactionManager {
@Override
public String getType() {
- return XATransactionManagerType.ATOMIKOS.getType();
+ return TransactionManagerType.ATOMIKOS.getType();
}
}
diff --git a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-atomikos/src/test/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManagerTest.java b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-atomikos/src/test/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManagerTest.java
index 6060a3b..16419a9 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-atomikos/src/test/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManagerTest.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-atomikos/src/test/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManagerTest.java
@@ -60,12 +60,6 @@ public final class AtomikosTransactionManagerTest {
}
@Test
- public void assertInit() {
- atomikosTransactionManager.init();
- verify(userTransactionService).init();
- }
-
- @Test
public void assertRegisterRecoveryResource() {
atomikosTransactionManager.registerRecoveryResource("ds1", xaDataSource);
verify(userTransactionService).registerResource(any(AtomikosXARecoverableResource.class));
diff --git a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-bitronix/src/main/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManager.java b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-bitronix/src/main/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManager.java
index dac3df9..68c812b 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-bitronix/src/main/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManager.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-bitronix/src/main/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManager.java
@@ -22,7 +22,7 @@ import bitronix.tm.TransactionManagerServices;
import bitronix.tm.recovery.RecoveryException;
import bitronix.tm.resource.ResourceRegistrar;
import lombok.SneakyThrows;
-import org.apache.shardingsphere.transaction.core.XATransactionManagerType;
+import org.apache.shardingsphere.transaction.core.TransactionManagerType;
import org.apache.shardingsphere.transaction.xa.spi.SingleXAResource;
import org.apache.shardingsphere.transaction.xa.spi.XATransactionManager;
@@ -36,10 +36,11 @@ import javax.transaction.TransactionManager;
*/
public final class BitronixXATransactionManager implements XATransactionManager {
- private final BitronixTransactionManager bitronixTransactionManager = TransactionManagerServices.getTransactionManager();
+ private BitronixTransactionManager bitronixTransactionManager;
@Override
public void init() {
+ bitronixTransactionManager = TransactionManagerServices.getTransactionManager();
}
@SneakyThrows(RecoveryException.class)
@@ -71,6 +72,6 @@ public final class BitronixXATransactionManager implements XATransactionManager
@Override
public String getType() {
- return XATransactionManagerType.BITRONIX.getType();
+ return TransactionManagerType.BITRONIX.getType();
}
}
diff --git a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-bitronix/src/test/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManagerTest.java b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-bitronix/src/test/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManagerTest.java
index af35fdb..1d64cc5 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-bitronix/src/test/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManagerTest.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-bitronix/src/test/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManagerTest.java
@@ -27,13 +27,10 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
-import javax.sql.XAConnection;
import javax.sql.XADataSource;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
-import javax.transaction.xa.XAResource;
-import java.sql.SQLException;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
@@ -55,12 +52,8 @@ public final class BitronixXATransactionManagerTest {
private XADataSource xaDataSource;
@Before
- public void setUp() throws SQLException {
+ public void setUp() {
ReflectiveUtil.setProperty(bitronixXATransactionManager, "bitronixTransactionManager", bitronixTransactionManager);
- XAConnection xaConnection = mock(XAConnection.class);
- XAResource xaResource = mock(XAResource.class);
- when(xaConnection.getXAResource()).thenReturn(xaResource);
- when(xaDataSource.getXAConnection()).thenReturn(xaConnection);
}
@Test
diff --git a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingTransactionManager.java b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingTransactionManager.java
index 492ffa0..d12153d 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingTransactionManager.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingTransactionManager.java
@@ -45,14 +45,15 @@ public final class XAShardingTransactionManager implements ShardingTransactionMa
private final Map<String, XATransactionDataSource> cachedDataSources = new HashMap<>();
- private final XATransactionManager xaTransactionManager = XATransactionManagerLoader.getInstance().getTransactionManager();
+ private XATransactionManager xaTransactionManager;
@Override
- public void init(final DatabaseType databaseType, final Collection<ResourceDataSource> resourceDataSources) {
+ public void init(final DatabaseType databaseType, final Collection<ResourceDataSource> resourceDataSources, final String transactionMangerType) {
+ xaTransactionManager = XATransactionManagerLoader.getInstance().getXATransactionManager(transactionMangerType);
+ xaTransactionManager.init();
for (ResourceDataSource each : resourceDataSources) {
cachedDataSources.put(each.getOriginalName(), new XATransactionDataSource(databaseType, each.getUniqueResourceName(), each.getDataSource(), xaTransactionManager));
}
- xaTransactionManager.init();
}
@Override
diff --git a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/manager/XATransactionManagerLoader.java b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/manager/XATransactionManagerLoader.java
index 6c81011..7461ac4 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/manager/XATransactionManagerLoader.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/manager/XATransactionManagerLoader.java
@@ -17,46 +17,50 @@
package org.apache.shardingsphere.transaction.xa.manager;
-import lombok.Getter;
+import java.util.Iterator;
+import java.util.ServiceLoader;
import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.transaction.core.XATransactionManagerType;
-import org.apache.shardingsphere.transaction.core.XATransactionManagerTypeHolder;
import org.apache.shardingsphere.transaction.xa.atomikos.manager.AtomikosTransactionManager;
import org.apache.shardingsphere.transaction.xa.spi.XATransactionManager;
-import java.util.ServiceLoader;
-
/**
* XA transaction manager loader.
*/
-@Getter
@Slf4j
public final class XATransactionManagerLoader {
private static final XATransactionManagerLoader INSTANCE = new XATransactionManagerLoader();
- private final XATransactionManager transactionManager;
-
private XATransactionManagerLoader() {
- transactionManager = load();
}
- private XATransactionManager load() {
- XATransactionManagerType xaTransactionManagerType = XATransactionManagerTypeHolder.get();
- for (XATransactionManager each : ServiceLoader.load(XATransactionManager.class)) {
- if (null != xaTransactionManagerType && each.getType().equalsIgnoreCase(xaTransactionManagerType.getType())) {
- return each;
- }
- }
- return new AtomikosTransactionManager();
- }
/**
* Get instance of XA transaction manager SPI loader.
- *
+ *
* @return instance of XA transaction manager SPI loader
*/
public static XATransactionManagerLoader getInstance() {
return INSTANCE;
}
+
+ /**
+ * Get xa transaction manager.
+ *
+ * @param type type
+ * @return xa transaction manager
+ */
+ public XATransactionManager getXATransactionManager(final String type) {
+ Iterator<XATransactionManager> xaTransactionManagers = ServiceLoader.load(XATransactionManager.class).iterator();
+ if (!xaTransactionManagers.hasNext()) {
+ return new AtomikosTransactionManager();
+ }
+ while (xaTransactionManagers.hasNext()) {
+ XATransactionManager result = xaTransactionManagers.next();
+ if (result.getType().equalsIgnoreCase(type)) {
+ return result;
+ }
+ }
+ return new AtomikosTransactionManager();
+ }
}
diff --git a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingTransactionManagerTest.java b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingTransactionManagerTest.java
index 9860e99..cb1116f 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingTransactionManagerTest.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingTransactionManagerTest.java
@@ -19,58 +19,44 @@ package org.apache.shardingsphere.transaction.xa;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.zaxxer.hikari.HikariDataSource;
+import java.lang.reflect.Field;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.transaction.Transaction;
import lombok.SneakyThrows;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.transaction.core.ResourceDataSource;
+import org.apache.shardingsphere.transaction.core.TransactionManagerType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.xa.fixture.DataSourceUtils;
-import org.apache.shardingsphere.transaction.xa.fixture.ReflectiveUtil;
import org.apache.shardingsphere.transaction.xa.jta.datasource.XATransactionDataSource;
-import org.apache.shardingsphere.transaction.xa.manager.XATransactionManagerLoader;
-import org.apache.shardingsphere.transaction.xa.spi.XATransactionManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
-import javax.sql.XADataSource;
-import javax.transaction.Transaction;
-import java.lang.reflect.Field;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public final class XAShardingTransactionManagerTest {
private final XAShardingTransactionManager xaShardingTransactionManager = new XAShardingTransactionManager();
- @Spy
- private final XATransactionManager xaTransactionManager = XATransactionManagerLoader.getInstance().getTransactionManager();
-
@Before
public void setUp() {
- ReflectiveUtil.setProperty(xaShardingTransactionManager, "xaTransactionManager", xaTransactionManager);
Collection<ResourceDataSource> resourceDataSources = createResourceDataSources(DatabaseTypeRegistry.getActualDatabaseType("H2"));
- xaShardingTransactionManager.init(DatabaseTypeRegistry.getActualDatabaseType("H2"), resourceDataSources);
- verify(xaTransactionManager).init();
+ xaShardingTransactionManager.init(DatabaseTypeRegistry.getActualDatabaseType("H2"), resourceDataSources, TransactionManagerType.ATOMIKOS.getType());
}
@After
@@ -134,7 +120,6 @@ public final class XAShardingTransactionManagerTest {
public void assertClose() throws Exception {
xaShardingTransactionManager.close();
Map<String, XATransactionDataSource> cachedSingleXADataSourceMap = getCachedDataSources();
- verify(xaTransactionManager, times(2)).removeRecoveryResource(anyString(), any(XADataSource.class));
assertThat(cachedSingleXADataSourceMap.size(), is(0));
}
diff --git a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/manager/XATransactionManagerLoaderTest.java b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/manager/XATransactionManagerLoaderTest.java
index a0270c2..5929a6b 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/manager/XATransactionManagerLoaderTest.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/manager/XATransactionManagerLoaderTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.transaction.xa.manager;
+import org.apache.shardingsphere.transaction.core.TransactionManagerType;
import org.apache.shardingsphere.transaction.xa.atomikos.manager.AtomikosTransactionManager;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -30,6 +31,6 @@ public final class XATransactionManagerLoaderTest {
@Test
public void assertGetTransactionManager() {
- assertThat(XATransactionManagerLoader.getInstance().getTransactionManager(), instanceOf(AtomikosTransactionManager.class));
+ assertThat(XATransactionManagerLoader.getInstance().getXATransactionManager(TransactionManagerType.ATOMIKOS.getType()), instanceOf(AtomikosTransactionManager.class));
}
}
diff --git a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManager.java b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManager.java
index c21b6ad..123de94 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManager.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManager.java
@@ -23,7 +23,7 @@ import com.arjuna.ats.jbossatx.jta.RecoveryManagerService;
import com.arjuna.ats.jta.common.jtaPropertyManager;
import java.util.Objects;
import lombok.SneakyThrows;
-import org.apache.shardingsphere.transaction.core.XATransactionManagerType;
+import org.apache.shardingsphere.transaction.core.TransactionManagerType;
import org.apache.shardingsphere.transaction.xa.spi.SingleXAResource;
import org.apache.shardingsphere.transaction.xa.spi.XATransactionManager;
@@ -37,14 +37,17 @@ import javax.transaction.TransactionManager;
*/
public final class NarayanaXATransactionManager implements XATransactionManager {
- private final TransactionManager transactionManager = jtaPropertyManager.getJTAEnvironmentBean().getTransactionManager();
+ private TransactionManager transactionManager;
- private final XARecoveryModule xaRecoveryModule = XARecoveryModule.getRegisteredXARecoveryModule();
+ private XARecoveryModule xaRecoveryModule;
- private final RecoveryManagerService recoveryManagerService = new RecoveryManagerService();
+ private RecoveryManagerService recoveryManagerService;
@Override
public void init() {
+ transactionManager = jtaPropertyManager.getJTAEnvironmentBean().getTransactionManager();
+ xaRecoveryModule = XARecoveryModule.getRegisteredXARecoveryModule();
+ recoveryManagerService = new RecoveryManagerService();
RecoveryManager.delayRecoveryManagerThread();
recoveryManagerService.create();
recoveryManagerService.start();
@@ -83,6 +86,6 @@ public final class NarayanaXATransactionManager implements XATransactionManager
@Override
public String getType() {
- return XATransactionManagerType.NARAYANA.getType();
+ return TransactionManagerType.NARAYANA.getType();
}
}
diff --git a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManagerTest.java b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManagerTest.java
index 94f1501..4f4d4c8 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManagerTest.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-2pc/shardingsphere-transaction-xa/shardingsphere-transaction-xa-narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManagerTest.java
@@ -65,13 +65,6 @@ public final class NarayanaXATransactionManagerTest {
}
@Test
- public void assertInit() {
- narayanaXATransactionManager.init();
- verify(recoveryManagerService).create();
- verify(recoveryManagerService).start();
- }
-
- @Test
public void assertRegisterRecoveryResource() {
narayanaXATransactionManager.registerRecoveryResource("ds1", xaDataSource);
verify(xaRecoveryModule).addXAResourceRecoveryHelper(any(DataSourceXAResourceRecoveryHelper.class));
diff --git a/shardingsphere-transaction/shardingsphere-transaction-base/shardingsphere-transaction-base-seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingTransactionManager.java b/shardingsphere-transaction/shardingsphere-transaction-base/shardingsphere-transaction-base-seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingTransactionManager.java
index ddec873..57b7b28 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-base/shardingsphere-transaction-base-seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingTransactionManager.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-base/shardingsphere-transaction-base-seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingTransactionManager.java
@@ -62,7 +62,7 @@ public final class SeataATShardingTransactionManager implements ShardingTransact
}
@Override
- public void init(final DatabaseType databaseType, final Collection<ResourceDataSource> resourceDataSources) {
+ public void init(final DatabaseType databaseType, final Collection<ResourceDataSource> resourceDataSources, final String transactionMangerType) {
if (enableSeataAT) {
initSeataRPCClient();
for (ResourceDataSource each : resourceDataSources) {
diff --git a/shardingsphere-transaction/shardingsphere-transaction-base/shardingsphere-transaction-base-seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingTransactionManagerTest.java b/shardingsphere-transaction/shardingsphere-transaction-base/shardingsphere-transaction-base-seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingTransactionManagerTest.java
index 825ca3b..04fc21b 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-base/shardingsphere-transaction-base-seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingTransactionManagerTest.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-base/shardingsphere-transaction-base-seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingTransactionManagerTest.java
@@ -90,7 +90,7 @@ public final class SeataATShardingTransactionManagerTest {
@Before
public void setUp() {
- seataATShardingTransactionManager.init(DatabaseTypeRegistry.getActualDatabaseType("MySQL"), getResourceDataSources());
+ seataATShardingTransactionManager.init(DatabaseTypeRegistry.getActualDatabaseType("MySQL"), getResourceDataSources(), "seata");
}
@After
diff --git a/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/ShardingTransactionManagerEngine.java b/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/ShardingTransactionManagerEngine.java
index 1b42855..73617ca 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/ShardingTransactionManagerEngine.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/ShardingTransactionManagerEngine.java
@@ -61,10 +61,11 @@ public final class ShardingTransactionManagerEngine {
*
* @param databaseType database type
* @param dataSourceMap data source map
+ * @param transactionMangerType transaction manger type
*/
- public void init(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
+ public void init(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final String transactionMangerType) {
for (Entry<TransactionType, ShardingTransactionManager> entry : transactionManagerMap.entrySet()) {
- entry.getValue().init(databaseType, getResourceDataSources(dataSourceMap));
+ entry.getValue().init(databaseType, getResourceDataSources(dataSourceMap), transactionMangerType);
}
}
diff --git a/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/XATransactionManagerType.java b/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/TransactionManagerType.java
similarity index 73%
rename from shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/XATransactionManagerType.java
rename to shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/TransactionManagerType.java
index f5c55b1..754f0e4 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/XATransactionManagerType.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/TransactionManagerType.java
@@ -22,27 +22,29 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
- * XA transaction manager type.
+ * Transaction manager type.
*/
@RequiredArgsConstructor
@Getter
-public enum XATransactionManagerType {
+public enum TransactionManagerType {
ATOMIKOS("atomikos"),
NARAYANA("narayana"),
- BITRONIX("bitronix");
-
+ BITRONIX("bitronix"),
+
+ SEATA("seata");
+
private final String type;
/**
- * Value from xa transaction manager type.
+ * Value from transaction manager type.
*
- * @param type value to be xa transaction manager type
- * @return value from xa transaction manager type
+ * @param type value to be transaction manager type
+ * @return value from transaction manager type
*/
- public static XATransactionManagerType valueFrom(final String type) {
- return Arrays.stream(values()).filter(each -> each.type.equalsIgnoreCase(type)).findFirst().orElse(XATransactionManagerType.ATOMIKOS);
+ public static TransactionManagerType valueFrom(final String type) {
+ return Arrays.stream(values()).filter(each -> each.type.equalsIgnoreCase(type)).findFirst().orElse(TransactionManagerType.ATOMIKOS);
}
}
diff --git a/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/XATransactionManagerTypeHolder.java b/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/XATransactionManagerTypeHolder.java
deleted file mode 100644
index c86ea26..0000000
--- a/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/core/XATransactionManagerTypeHolder.java
+++ /dev/null
@@ -1,55 +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.core;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-
-/**
- * Hold xa transaction manager type for current thread.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class XATransactionManagerTypeHolder {
-
- private static final ThreadLocal<XATransactionManagerType> CONTEXT = ThreadLocal.withInitial(() -> XATransactionManagerType.ATOMIKOS);
-
- /**
- * Get xa transaction manager type for current thread.
- *
- * @return xa transaction manager type
- */
- public static XATransactionManagerType get() {
- return CONTEXT.get();
- }
-
- /**
- * Set xa transaction manager type for current thread.
- *
- * @param xaTransactionManagerType xa transaction manager type
- */
- public static void set(final XATransactionManagerType xaTransactionManagerType) {
- CONTEXT.set(xaTransactionManagerType);
- }
-
- /**
- * Clear xa transaction manger type for current thread.
- */
- public static void clear() {
- CONTEXT.remove();
- }
-}
diff --git a/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/spi/ShardingTransactionManager.java b/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/spi/ShardingTransactionManager.java
index 1ce9498..97de37c 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/spi/ShardingTransactionManager.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/spi/ShardingTransactionManager.java
@@ -35,8 +35,9 @@ public interface ShardingTransactionManager extends AutoCloseable {
*
* @param databaseType database type
* @param resourceDataSources resource data sources
+ * @param transactionMangerType transaction manger type
*/
- void init(DatabaseType databaseType, Collection<ResourceDataSource> resourceDataSources);
+ void init(DatabaseType databaseType, Collection<ResourceDataSource> resourceDataSources, String transactionMangerType);
/**
* Get transaction type.
diff --git a/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/ShardingTransactionManagerEngineTest.java b/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/ShardingTransactionManagerEngineTest.java
index c2b2c68..1312cef 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/ShardingTransactionManagerEngineTest.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/ShardingTransactionManagerEngineTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.transaction;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
+import org.apache.shardingsphere.transaction.core.TransactionManagerType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.core.fixture.ShardingTransactionManagerFixture;
import org.junit.Test;
@@ -44,7 +45,7 @@ public final class ShardingTransactionManagerEngineTest {
Runnable caller = mock(Runnable.class);
ShardingTransactionManagerFixture shardingTransactionManager = (ShardingTransactionManagerFixture) shardingTransactionManagerEngine.getTransactionManager(TransactionType.XA);
shardingTransactionManager.setCaller(caller);
- shardingTransactionManagerEngine.init(DatabaseTypeRegistry.getActualDatabaseType("H2"), mock(Map.class));
+ shardingTransactionManagerEngine.init(DatabaseTypeRegistry.getActualDatabaseType("H2"), mock(Map.class), TransactionManagerType.ATOMIKOS.getType());
verify(caller).run();
}
}
diff --git a/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/OtherShardingTransactionManagerFixture.java b/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/OtherShardingTransactionManagerFixture.java
index 736310e..669e9d7 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/OtherShardingTransactionManagerFixture.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/OtherShardingTransactionManagerFixture.java
@@ -28,7 +28,7 @@ import java.util.Collection;
public final class OtherShardingTransactionManagerFixture implements ShardingTransactionManager {
@Override
- public void init(final DatabaseType databaseType, final Collection<ResourceDataSource> resourceDataSources) {
+ public void init(final DatabaseType databaseType, final Collection<ResourceDataSource> resourceDataSources, final String transactionMangerType) {
}
@Override
diff --git a/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/ShardingTransactionManagerFixture.java b/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/ShardingTransactionManagerFixture.java
index c77d7b8..6a267b6 100644
--- a/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/ShardingTransactionManagerFixture.java
+++ b/shardingsphere-transaction/shardingsphere-transaction-core/src/test/java/org/apache/shardingsphere/transaction/core/fixture/ShardingTransactionManagerFixture.java
@@ -32,7 +32,7 @@ public final class ShardingTransactionManagerFixture implements ShardingTransact
private Runnable caller;
@Override
- public void init(final DatabaseType databaseType, final Collection<ResourceDataSource> resourceDataSources) {
+ public void init(final DatabaseType databaseType, final Collection<ResourceDataSource> resourceDataSources, final String transactionMangerType) {
caller.run();
}