This is an automated email from the ASF dual-hosted git repository.
jimin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.x by this push:
new 486f582845 optimize: compatible with tm module and rm-datasource
module (#6343)
486f582845 is described below
commit 486f582845d4af15805fdca2ced7597071644577
Author: xingfudeshi <[email protected]>
AuthorDate: Wed Feb 14 00:11:01 2024 +0800
optimize: compatible with tm module and rm-datasource module (#6343)
---
changes/en-us/2.x.md | 1 +
changes/zh-cn/2.x.md | 2 +
compatible/pom.xml | 6 +
.../seata/core/exception/TransactionException.java | 56 ++++++
.../core/exception/TransactionExceptionCode.java | 172 +++++++++++++++++
.../java/io/seata/core/model/GlobalStatus.java | 215 +++++++++++++++++++++
compatible/src/main/java/io/seata/rm/RMClient.java | 25 +++
.../io/seata/rm/datasource/DataSourceProxy.java | 34 ++++
.../seata/rm/datasource/xa/DataSourceProxyXA.java | 34 ++++
compatible/src/main/java/io/seata/tm/TMClient.java | 24 +++
.../io/seata/tm/api/DefaultGlobalTransaction.java | 170 ++++++++++++++++
.../java/io/seata/tm/api/GlobalTransaction.java | 148 ++++++++++++++
.../io/seata/tm/api/GlobalTransactionContext.java | 82 ++++++++
.../io/seata/tm/api/GlobalTransactionRole.java | 35 ++++
.../io/seata/tm/api/TransactionalTemplate.java | 23 +++
.../api/transaction/SuspendedResourcesHolder.java | 27 +++
.../seata/tm/api/DefaultGlobalTransaction.java | 2 +-
17 files changed, 1055 insertions(+), 1 deletion(-)
diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 898880d63d..991ed849cc 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -85,6 +85,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6330](https://github.com/apache/incubator-seata/pull/6330)] remove
mariadb API
- [[#6329](https://github.com/apache/incubator-seata/pull/6312)] add saga
subcomponent-level io.seata compatible api
- [[#6254](https://github.com/apache/incubator-seata/pull/6254)] optimize
Hessian Serialize
+- [[#6343](https://github.com/apache/incubator-seata/pull/6343)] compatible
with tm module and rm-datasource module
### security:
- [[#6069](https://github.com/apache/incubator-seata/pull/6069)] Upgrade Guava
dependencies to fix security vulnerabilities
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index e8795ee267..88d595ca0f 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -83,6 +83,8 @@
- [[#6330](https://github.com/apache/incubator-seata/pull/6330)] 去除 mariadb API
- [[#6329](https://github.com/apache/incubator-seata/pull/6312)]
添加saga子组件的io.seata兼容性API
- [[#6254](https://github.com/apache/incubator-seata/pull/6254)] 优化Hessian 序列化
+- [[#6343](https://github.com/apache/incubator-seata/pull/6343)] 兼容tm
模块和rm-datasource模块
+
### security:
diff --git a/compatible/pom.xml b/compatible/pom.xml
index 8e2a25c09f..fd4b4d76a6 100644
--- a/compatible/pom.xml
+++ b/compatible/pom.xml
@@ -49,5 +49,11 @@
<version>2.1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.seata</groupId>
+ <artifactId>seata-rm-datasource</artifactId>
+ <version>2.1.0-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
</project>
diff --git
a/compatible/src/main/java/io/seata/core/exception/TransactionException.java
b/compatible/src/main/java/io/seata/core/exception/TransactionException.java
new file mode 100644
index 0000000000..59b888737e
--- /dev/null
+++ b/compatible/src/main/java/io/seata/core/exception/TransactionException.java
@@ -0,0 +1,56 @@
+/*
+ * 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 io.seata.core.exception;
+
+
+/**
+ * The type Transaction exception.
+ */
+public class TransactionException extends
org.apache.seata.core.exception.TransactionException {
+
+ private static org.apache.seata.core.exception.TransactionExceptionCode
convertApacheSeataTransactionExceptionCode(TransactionExceptionCode
transactionExceptionCode) {
+ return
org.apache.seata.core.exception.TransactionExceptionCode.get(transactionExceptionCode.ordinal());
+ }
+
+ public TransactionException(TransactionExceptionCode code) {
+ super(convertApacheSeataTransactionExceptionCode(code));
+ }
+
+ public TransactionException(TransactionExceptionCode code, Throwable
cause) {
+ super(convertApacheSeataTransactionExceptionCode(code), cause);
+ }
+
+ public TransactionException(String message) {
+ super(message);
+ }
+
+ public TransactionException(TransactionExceptionCode code, String message)
{
+ super(convertApacheSeataTransactionExceptionCode(code), message);
+ }
+
+ public TransactionException(Throwable cause) {
+ super(cause);
+ }
+
+ public TransactionException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public TransactionException(TransactionExceptionCode code, String message,
Throwable cause) {
+ super(convertApacheSeataTransactionExceptionCode(code), message,
cause);
+ }
+}
diff --git
a/compatible/src/main/java/io/seata/core/exception/TransactionExceptionCode.java
b/compatible/src/main/java/io/seata/core/exception/TransactionExceptionCode.java
new file mode 100644
index 0000000000..637c2db031
--- /dev/null
+++
b/compatible/src/main/java/io/seata/core/exception/TransactionExceptionCode.java
@@ -0,0 +1,172 @@
+/*
+ * 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 io.seata.core.exception;
+
+/**
+ * The enum Transaction exception code.
+ *
+ */
+public enum TransactionExceptionCode {
+
+ /**
+ * Unknown transaction exception code.
+ */
+ Unknown,
+
+ /**
+ * BeginFailed
+ */
+ BeginFailed,
+
+ /**
+ * Lock key conflict transaction exception code.
+ */
+ LockKeyConflict,
+
+ /**
+ * Io transaction exception code.
+ */
+ IO,
+
+ /**
+ * Branch rollback failed retriable transaction exception code.
+ */
+ BranchRollbackFailed_Retriable,
+
+ /**
+ * Branch rollback failed unretriable transaction exception code.
+ */
+ BranchRollbackFailed_Unretriable,
+
+ /**
+ * Branch register failed transaction exception code.
+ */
+ BranchRegisterFailed,
+
+ /**
+ * Branch report failed transaction exception code.
+ */
+ BranchReportFailed,
+
+ /**
+ * Lockable check failed transaction exception code.
+ */
+ LockableCheckFailed,
+
+ /**
+ * Branch transaction not exist transaction exception code.
+ */
+ BranchTransactionNotExist,
+
+ /**
+ * Global transaction not exist transaction exception code.
+ */
+ GlobalTransactionNotExist,
+
+ /**
+ * Global transaction not active transaction exception code.
+ */
+ GlobalTransactionNotActive,
+
+ /**
+ * Global transaction status invalid transaction exception code.
+ */
+ GlobalTransactionStatusInvalid,
+
+ /**
+ * Failed to send branch commit request transaction exception code.
+ */
+ FailedToSendBranchCommitRequest,
+
+ /**
+ * Failed to send branch rollback request transaction exception code.
+ */
+ FailedToSendBranchRollbackRequest,
+
+ /**
+ * Failed to add branch transaction exception code.
+ */
+ FailedToAddBranch,
+
+ /**
+ * Failed to lock global transaction exception code.
+ */
+ FailedLockGlobalTranscation,
+
+ /**
+ * FailedWriteSession
+ */
+ FailedWriteSession,
+
+ /**
+ * Failed to store exception code
+ */
+ FailedStore,
+
+ /**
+ * not raft leader exception code
+ */
+ NotRaftLeader,
+
+ /**
+ * Lock key conflict fail fast transaction exception code.
+ */
+ LockKeyConflictFailFast,
+
+ /**
+ * transaction already timeout
+ */
+ TransactionTimeout,
+
+ /**
+ * Commit heuristic transaction exception code.
+ */
+ CommitHeuristic,
+
+ /**
+ * Broken transaction exception code.
+ */
+ Broken;
+
+
+ /**
+ * Get transaction exception code.
+ *
+ * @param ordinal the ordinal
+ * @return the transaction exception code
+ */
+ public static TransactionExceptionCode get(byte ordinal) {
+ return get((int)ordinal);
+ }
+
+ /**
+ * Get transaction exception code.
+ *
+ * @param ordinal the ordinal
+ * @return the transaction exception code
+ */
+ public static TransactionExceptionCode get(int ordinal) {
+ TransactionExceptionCode value = null;
+ try {
+ value = TransactionExceptionCode.values()[ordinal];
+ } catch (Exception e) {
+ throw new IllegalArgumentException("Unknown
TransactionExceptionCode[" + ordinal + "]");
+ }
+ return value;
+ }
+
+}
diff --git a/compatible/src/main/java/io/seata/core/model/GlobalStatus.java
b/compatible/src/main/java/io/seata/core/model/GlobalStatus.java
new file mode 100644
index 0000000000..034f69d63c
--- /dev/null
+++ b/compatible/src/main/java/io/seata/core/model/GlobalStatus.java
@@ -0,0 +1,215 @@
+/*
+ * 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 io.seata.core.model;
+
+/**
+ * Status of global transaction.
+ *
+ */
+public enum GlobalStatus {
+
+ /**
+ * Un known global status.
+ */
+ // Unknown
+ UnKnown(0, "an ambiguous transaction state, usually use before begin"),
+
+ /**
+ * The Begin.
+ */
+ // PHASE 1: can accept new branch registering.
+ Begin(1, "global transaction start"),
+
+ /**
+ * PHASE 2: Running Status: may be changed any time.
+ */
+ // Committing.
+ Committing(2, "2Phase committing"),
+
+ /**
+ * The Commit retrying.
+ */
+ // Retrying commit after a recoverable failure.
+ CommitRetrying(3, "2Phase committing failure retry"),
+
+ /**
+ * Rollbacking global status.
+ */
+ // Rollbacking
+ Rollbacking(4, "2Phase rollbacking"),
+
+ /**
+ * The Rollback retrying.
+ */
+ // Retrying rollback after a recoverable failure.
+ RollbackRetrying(5, "2Phase rollbacking failure retry"),
+
+ /**
+ * The Timeout rollbacking.
+ */
+ // Rollbacking since timeout
+ TimeoutRollbacking(6, "after global transaction timeout rollbacking"),
+
+ /**
+ * The Timeout rollback retrying.
+ */
+ // Retrying rollback (since timeout) after a recoverable failure.
+ TimeoutRollbackRetrying(7, "after global transaction timeout rollback
retrying"),
+
+ /**
+ * All branches can be async committed. The committing is NOT done yet,
but it can be seen as committed for TM/RM
+ * client.
+ */
+ AsyncCommitting(8, "2Phase committing, used for AT mode"),
+
+ /**
+ * PHASE 2: Final Status: will NOT change any more.
+ */
+ // Finally: global transaction is successfully committed.
+ Committed(9, "global transaction completed with status committed"),
+
+ /**
+ * The Commit failed.
+ */
+ // Finally: failed to commit
+ CommitFailed(10, "2Phase commit failed"),
+
+ /**
+ * The Rollbacked.
+ */
+ // Finally: global transaction is successfully rollbacked.
+ Rollbacked(11, "global transaction completed with status rollbacked"),
+
+ /**
+ * The Rollback failed.
+ */
+ // Finally: failed to rollback
+ RollbackFailed(12, "global transaction completed but rollback failed"),
+
+ /**
+ * The Timeout rollbacked.
+ */
+ // Finally: global transaction is successfully rollbacked since timeout.
+ TimeoutRollbacked(13, "global transaction completed with rollback due to
timeout"),
+
+ /**
+ * The Timeout rollback failed.
+ */
+ // Finally: failed to rollback since timeout
+ TimeoutRollbackFailed(14, "global transaction was rollbacking due to
timeout, but failed"),
+
+ /**
+ * The Finished.
+ */
+ // Not managed in session MAP any more
+ Finished(15, "ambiguous transaction status for non-exist transaction and
global report for Saga"),
+
+ /**
+ * The commit retry Timeout .
+ */
+ // Finally: failed to commit since retry timeout
+ CommitRetryTimeout(16, "global transaction still failed after commit
failure and retries for some time"),
+
+ /**
+ * The rollback retry Timeout .
+ */
+ // Finally: failed to rollback since retry timeout
+ RollbackRetryTimeout(17, "global transaction still failed after commit
failure and retries for some time");
+
+ private final int code;
+ private final String desc;
+
+ GlobalStatus(int code, String desc) {
+ this.code = code;
+ this.desc = desc;
+ }
+
+ /**
+ * Gets code.
+ *
+ * @return the code
+ */
+ public int getCode() {
+ return code;
+ }
+
+ /**
+ * Get global status.
+ *
+ * @param code the code
+ * @return the global status
+ */
+ public static GlobalStatus get(byte code) {
+ return get((int)code);
+ }
+
+ /**
+ * Get global status.
+ *
+ * @param code the code
+ * @return the global status
+ */
+ public static GlobalStatus get(int code) {
+ GlobalStatus value = null;
+ try {
+ value = GlobalStatus.values()[code];
+ } catch (Exception e) {
+ throw new IllegalArgumentException("Unknown GlobalStatus[" + code
+ "]");
+ }
+ return value;
+ }
+
+ /**
+ * Is one phase timeout boolean.
+ *
+ * @param status the status
+ * @return the boolean
+ */
+ public static boolean isOnePhaseTimeout(GlobalStatus status) {
+ if (status == TimeoutRollbacking || status == TimeoutRollbackRetrying
|| status == TimeoutRollbacked || status == TimeoutRollbackFailed) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Is two phase success boolean.
+ *
+ * @param status the status
+ * @return the boolean
+ */
+ public static boolean isTwoPhaseSuccess(GlobalStatus status) {
+ if (status == GlobalStatus.Committed || status ==
GlobalStatus.Rollbacked
+ || status == GlobalStatus.TimeoutRollbacked) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Is two phase heuristic boolean.
+ *
+ * @param status the status
+ * @return the boolean
+ */
+ public static boolean isTwoPhaseHeuristic(GlobalStatus status) {
+ if (status == GlobalStatus.Finished) {
+ return true;
+ }
+ return false;
+ }
+}
diff --git a/compatible/src/main/java/io/seata/rm/RMClient.java
b/compatible/src/main/java/io/seata/rm/RMClient.java
new file mode 100644
index 0000000000..c5b2df0067
--- /dev/null
+++ b/compatible/src/main/java/io/seata/rm/RMClient.java
@@ -0,0 +1,25 @@
+/*
+ * 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 io.seata.rm;
+
+/**
+ * The Rm client Initiator.
+ *
+ */
+public class RMClient extends org.apache.seata.rm.RMClient {
+
+}
diff --git
a/compatible/src/main/java/io/seata/rm/datasource/DataSourceProxy.java
b/compatible/src/main/java/io/seata/rm/datasource/DataSourceProxy.java
new file mode 100644
index 0000000000..ad6188e06f
--- /dev/null
+++ b/compatible/src/main/java/io/seata/rm/datasource/DataSourceProxy.java
@@ -0,0 +1,34 @@
+/*
+ * 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 io.seata.rm.datasource;
+
+import javax.sql.DataSource;
+
+/**
+ * The type Data source proxy.
+ *
+ */
+public class DataSourceProxy extends
org.apache.seata.rm.datasource.DataSourceProxy {
+
+ public DataSourceProxy(DataSource targetDataSource) {
+ super(targetDataSource);
+ }
+
+ public DataSourceProxy(DataSource targetDataSource, String
resourceGroupId) {
+ super(targetDataSource, resourceGroupId);
+ }
+}
diff --git
a/compatible/src/main/java/io/seata/rm/datasource/xa/DataSourceProxyXA.java
b/compatible/src/main/java/io/seata/rm/datasource/xa/DataSourceProxyXA.java
new file mode 100644
index 0000000000..c90c515e9f
--- /dev/null
+++ b/compatible/src/main/java/io/seata/rm/datasource/xa/DataSourceProxyXA.java
@@ -0,0 +1,34 @@
+/*
+ * 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 io.seata.rm.datasource.xa;
+
+import javax.sql.DataSource;
+
+/**
+ * DataSource proxy for XA mode.
+ *
+ */
+public class DataSourceProxyXA extends
org.apache.seata.rm.datasource.xa.DataSourceProxyXA {
+
+ public DataSourceProxyXA(DataSource dataSource) {
+ super(dataSource);
+ }
+
+ public DataSourceProxyXA(DataSource dataSource, String resourceGroupId) {
+ super(dataSource, resourceGroupId);
+ }
+}
diff --git a/compatible/src/main/java/io/seata/tm/TMClient.java
b/compatible/src/main/java/io/seata/tm/TMClient.java
new file mode 100644
index 0000000000..4753aea0fe
--- /dev/null
+++ b/compatible/src/main/java/io/seata/tm/TMClient.java
@@ -0,0 +1,24 @@
+/*
+ * 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 io.seata.tm;
+
+/**
+ * The type Tm client.
+ *
+ */
+public class TMClient extends org.apache.seata.tm.TMClient {
+}
diff --git
a/compatible/src/main/java/io/seata/tm/api/DefaultGlobalTransaction.java
b/compatible/src/main/java/io/seata/tm/api/DefaultGlobalTransaction.java
new file mode 100644
index 0000000000..31a72c91b4
--- /dev/null
+++ b/compatible/src/main/java/io/seata/tm/api/DefaultGlobalTransaction.java
@@ -0,0 +1,170 @@
+/*
+ * 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 io.seata.tm.api;
+
+import io.seata.core.exception.TransactionException;
+import io.seata.core.exception.TransactionExceptionCode;
+import io.seata.core.model.GlobalStatus;
+import io.seata.tm.api.transaction.SuspendedResourcesHolder;
+
+/**
+ * The type Default global transaction.
+ */
+public class DefaultGlobalTransaction implements GlobalTransaction {
+ private final org.apache.seata.tm.api.DefaultGlobalTransaction instance;
+
+ DefaultGlobalTransaction() {
+ this(null, GlobalStatus.UnKnown, GlobalTransactionRole.Launcher);
+ }
+
+ /**
+ * Instantiates a new Default global transaction.
+ *
+ * @param xid the xid
+ * @param status the status
+ * @param role the role
+ */
+ DefaultGlobalTransaction(String xid, GlobalStatus status,
GlobalTransactionRole role) {
+ this.instance = new
org.apache.seata.tm.api.DefaultGlobalTransaction(xid,
convertApacheSeataGlobalStatus(status),
convertApacheSeataGlobalTransactionRole(role));
+ }
+
+ private static org.apache.seata.core.model.GlobalStatus
convertApacheSeataGlobalStatus(GlobalStatus globalStatus) {
+ return
org.apache.seata.core.model.GlobalStatus.get(globalStatus.getCode());
+ }
+
+ private static GlobalStatus
convertIoSeataGlobalStatus(org.apache.seata.core.model.GlobalStatus
globalStatus) {
+ return GlobalStatus.get(globalStatus.getCode());
+ }
+
+ private static org.apache.seata.tm.api.GlobalTransactionRole
convertApacheSeataGlobalTransactionRole(GlobalTransactionRole
globalTransactionRole) {
+ return
org.apache.seata.tm.api.GlobalTransactionRole.valueOf(globalTransactionRole.name());
+ }
+
+ private static GlobalTransactionRole
convertIoSeataGlobalTransactionRole(org.apache.seata.tm.api.GlobalTransactionRole
globalTransactionRole) {
+ return GlobalTransactionRole.valueOf(globalTransactionRole.name());
+ }
+
+ @Override
+ public void begin() throws TransactionException {
+ try {
+ this.instance.begin();
+ } catch (org.apache.seata.core.exception.TransactionException e) {
+ throw new
TransactionException(TransactionExceptionCode.valueOf(e.getCode().name()),
e.getMessage(), e.getCause());
+ }
+ }
+
+ @Override
+ public void begin(int timeout) throws TransactionException {
+ try {
+ this.instance.begin(timeout);
+ } catch (org.apache.seata.core.exception.TransactionException e) {
+ throw new
TransactionException(TransactionExceptionCode.valueOf(e.getCode().name()),
e.getMessage(), e.getCause());
+ }
+ }
+
+ @Override
+ public void begin(int timeout, String name) throws TransactionException {
+ try {
+ this.instance.begin(timeout, name);
+ } catch (org.apache.seata.core.exception.TransactionException e) {
+ throw new
TransactionException(TransactionExceptionCode.valueOf(e.getCode().name()),
e.getMessage(), e.getCause());
+ }
+ }
+
+ @Override
+ public void commit() throws TransactionException {
+ try {
+ this.instance.commit();
+ } catch (org.apache.seata.core.exception.TransactionException e) {
+ throw new
TransactionException(TransactionExceptionCode.valueOf(e.getCode().name()),
e.getMessage(), e.getCause());
+ }
+ }
+
+ @Override
+ public void rollback() throws TransactionException {
+ try {
+ this.instance.rollback();
+ } catch (org.apache.seata.core.exception.TransactionException e) {
+ throw new
TransactionException(TransactionExceptionCode.valueOf(e.getCode().name()),
e.getMessage(), e.getCause());
+ }
+ }
+
+ @Override
+ public SuspendedResourcesHolder suspend() throws TransactionException {
+ try {
+ return new
SuspendedResourcesHolder(this.instance.suspend().getXid());
+ } catch (org.apache.seata.core.exception.TransactionException e) {
+ throw new
TransactionException(TransactionExceptionCode.valueOf(e.getCode().name()),
e.getMessage(), e.getCause());
+ }
+ }
+
+ @Override
+ public SuspendedResourcesHolder suspend(boolean clean) throws
TransactionException {
+ try {
+ return new
SuspendedResourcesHolder(this.instance.suspend(clean).getXid());
+ } catch (org.apache.seata.core.exception.TransactionException e) {
+ throw new
TransactionException(TransactionExceptionCode.valueOf(e.getCode().name()),
e.getMessage(), e.getCause());
+ }
+ }
+
+ @Override
+ public void resume(SuspendedResourcesHolder suspendedResourcesHolder)
throws TransactionException {
+ try {
+ this.instance.resume(suspendedResourcesHolder);
+ } catch (org.apache.seata.core.exception.TransactionException e) {
+ throw new
TransactionException(TransactionExceptionCode.valueOf(e.getCode().name()),
e.getMessage(), e.getCause());
+ }
+ }
+
+ @Override
+ public GlobalStatus getStatus() throws TransactionException {
+ try {
+ return convertIoSeataGlobalStatus(this.instance.getStatus());
+ } catch (org.apache.seata.core.exception.TransactionException e) {
+ throw new
TransactionException(TransactionExceptionCode.valueOf(e.getCode().name()),
e.getMessage(), e.getCause());
+ }
+ }
+
+ @Override
+ public String getXid() {
+ return this.instance.getXid();
+ }
+
+ @Override
+ public void globalReport(GlobalStatus globalStatus) throws
TransactionException {
+ try {
+
this.instance.globalReport(convertApacheSeataGlobalStatus(globalStatus));
+ } catch (org.apache.seata.core.exception.TransactionException e) {
+ throw new
TransactionException(TransactionExceptionCode.valueOf(e.getCode().name()),
e.getMessage(), e.getCause());
+ }
+ }
+
+ @Override
+ public GlobalStatus getLocalStatus() {
+ return convertIoSeataGlobalStatus(this.instance.getLocalStatus());
+ }
+
+ @Override
+ public GlobalTransactionRole getGlobalTransactionRole() {
+ return
convertIoSeataGlobalTransactionRole(this.instance.getGlobalTransactionRole());
+ }
+
+ @Override
+ public long getCreateTime() {
+ return this.instance.getCreateTime();
+ }
+}
diff --git a/compatible/src/main/java/io/seata/tm/api/GlobalTransaction.java
b/compatible/src/main/java/io/seata/tm/api/GlobalTransaction.java
new file mode 100644
index 0000000000..2184f02b71
--- /dev/null
+++ b/compatible/src/main/java/io/seata/tm/api/GlobalTransaction.java
@@ -0,0 +1,148 @@
+/*
+ * 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 io.seata.tm.api;
+
+import io.seata.core.exception.TransactionException;
+import io.seata.core.model.GlobalStatus;
+import io.seata.tm.api.transaction.SuspendedResourcesHolder;
+
+/**
+ * Global transaction.
+ */
+public interface GlobalTransaction {
+
+ /**
+ * Begin a new global transaction with default timeout and name.
+ *
+ * @throws TransactionException Any exception that fails this will be
wrapped with TransactionException and thrown
+ * out.
+ */
+ void begin() throws TransactionException;
+
+ /**
+ * Begin a new global transaction with given timeout and default name.
+ *
+ * @param timeout Global transaction timeout in MILLISECONDS
+ * @throws TransactionException Any exception that fails this will be
wrapped with TransactionException and thrown
+ * out.
+ */
+ void begin(int timeout) throws TransactionException;
+
+ /**
+ * Begin a new global transaction with given timeout and given name.
+ *
+ * @param timeout Given timeout in MILLISECONDS.
+ * @param name Given name.
+ * @throws TransactionException Any exception that fails this will be
wrapped with TransactionException and thrown
+ * out.
+ */
+ void begin(int timeout, String name) throws TransactionException;
+
+ /**
+ * Commit the global transaction.
+ *
+ * @throws TransactionException Any exception that fails this will be
wrapped with TransactionException and thrown
+ * out.
+ */
+ void commit() throws TransactionException;
+
+ /**
+ * Rollback the global transaction.
+ *
+ * @throws TransactionException Any exception that fails this will be
wrapped with TransactionException and thrown
+ * out.
+ */
+ void rollback() throws TransactionException;
+
+ /**
+ * Suspend the global transaction.
+ *
+ * @return the SuspendedResourcesHolder which holds the suspend resources
+ * @throws TransactionException Any exception that fails this will be
wrapped with TransactionException and thrown
+ * @see SuspendedResourcesHolder
+ */
+ SuspendedResourcesHolder suspend() throws TransactionException;
+
+ /**
+ * Suspend the global transaction.
+ *
+ * @param clean the clean if true, clean the transaction context.
otherwise,supend only
+ * @return the SuspendedResourcesHolder which holds the suspend resources
+ * @throws TransactionException Any exception that fails this will be
wrapped with TransactionException and thrown
+ * @see SuspendedResourcesHolder
+ */
+ SuspendedResourcesHolder suspend(boolean clean) throws
TransactionException;
+
+ /**
+ * Resume the global transaction.
+ *
+ * @param suspendedResourcesHolder the suspended resources to resume
+ * @throws TransactionException Any exception that fails this will be
wrapped with TransactionException and thrown
+ * out.
+ * @see SuspendedResourcesHolder
+ */
+ void resume(SuspendedResourcesHolder suspendedResourcesHolder) throws
TransactionException;
+
+ /**
+ * Ask TC for current status of the corresponding global transaction.
+ *
+ * @return Status of the corresponding global transaction.
+ * @throws TransactionException Any exception that fails this will be
wrapped with TransactionException and thrown
+ * out.
+ * @see GlobalStatus
+ */
+ GlobalStatus getStatus() throws TransactionException;
+
+ /**
+ * Get XID.
+ *
+ * @return XID. xid
+ */
+ String getXid();
+
+ /**
+ * report the global transaction status.
+ *
+ * @param globalStatus global status.
+ * @throws TransactionException Any exception that fails this will be
wrapped with TransactionException and thrown
+ * out.
+ */
+ void globalReport(GlobalStatus globalStatus) throws TransactionException;
+
+ /**
+ * local status of the global transaction.
+ *
+ * @return Status of the corresponding global transaction.
+ * @see GlobalStatus
+ */
+ GlobalStatus getLocalStatus();
+
+ /**
+ * get global transaction role.
+ *
+ * @return global transaction Role.
+ * @see GlobalTransactionRole
+ */
+ GlobalTransactionRole getGlobalTransactionRole();
+
+ /**
+ * get create time
+ *
+ * @return create time
+ */
+ long getCreateTime();
+}
diff --git
a/compatible/src/main/java/io/seata/tm/api/GlobalTransactionContext.java
b/compatible/src/main/java/io/seata/tm/api/GlobalTransactionContext.java
new file mode 100644
index 0000000000..e7dc71ed30
--- /dev/null
+++ b/compatible/src/main/java/io/seata/tm/api/GlobalTransactionContext.java
@@ -0,0 +1,82 @@
+/*
+ * 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 io.seata.tm.api;
+
+
+import io.seata.core.context.RootContext;
+import io.seata.core.exception.TransactionException;
+import io.seata.core.model.GlobalStatus;
+
+/**
+ * GlobalTransaction API
+ */
+public class GlobalTransactionContext {
+
+ private GlobalTransactionContext() {
+ }
+
+ /**
+ * Try to create a new GlobalTransaction.
+ *
+ * @return the new global transaction
+ */
+ public static GlobalTransaction createNew() {
+ return new DefaultGlobalTransaction();
+ }
+
+ /**
+ * Get GlobalTransaction instance bind on current thread.
+ *
+ * @return null if no transaction context there.
+ */
+ public static GlobalTransaction getCurrent() {
+ String xid = RootContext.getXID();
+ if (xid == null) {
+ return null;
+ }
+ return new DefaultGlobalTransaction(xid, GlobalStatus.Begin,
GlobalTransactionRole.Participant);
+ }
+
+ /**
+ * Get GlobalTransaction instance bind on current thread. Create a new on
if no existing there.
+ *
+ * @return new context if no existing there.
+ */
+ public static GlobalTransaction getCurrentOrCreate() {
+ GlobalTransaction tx = getCurrent();
+ if (tx == null) {
+ return createNew();
+ }
+ return tx;
+ }
+
+ /**
+ * Reload GlobalTransaction instance according to the given XID
+ *
+ * @param xid the xid
+ * @return reloaded transaction instance.
+ * @throws TransactionException the transaction exception
+ */
+ public static GlobalTransaction reload(String xid) throws
TransactionException {
+ return new DefaultGlobalTransaction(xid, GlobalStatus.UnKnown,
GlobalTransactionRole.Launcher) {
+ @Override
+ public void begin(int timeout, String name) throws
TransactionException {
+ throw new IllegalStateException("Never BEGIN on a RELOADED
GlobalTransaction. ");
+ }
+ };
+ }
+}
diff --git
a/compatible/src/main/java/io/seata/tm/api/GlobalTransactionRole.java
b/compatible/src/main/java/io/seata/tm/api/GlobalTransactionRole.java
new file mode 100644
index 0000000000..7dd926eff9
--- /dev/null
+++ b/compatible/src/main/java/io/seata/tm/api/GlobalTransactionRole.java
@@ -0,0 +1,35 @@
+/*
+ * 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 io.seata.tm.api;
+
+/**
+ * Role of current thread involve in a global transaction.
+ */
+public enum GlobalTransactionRole {
+
+ /**
+ * The Launcher.
+ */
+ // The one begins the current global transaction.
+ Launcher,
+
+ /**
+ * The Participant.
+ */
+ // The one just joins into a existing global transaction.
+ Participant
+}
diff --git
a/compatible/src/main/java/io/seata/tm/api/TransactionalTemplate.java
b/compatible/src/main/java/io/seata/tm/api/TransactionalTemplate.java
new file mode 100644
index 0000000000..46dab0393d
--- /dev/null
+++ b/compatible/src/main/java/io/seata/tm/api/TransactionalTemplate.java
@@ -0,0 +1,23 @@
+/*
+ * 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 io.seata.tm.api;
+
+/**
+ * Template of executing business logic with a global transaction.
+ */
+public class TransactionalTemplate extends
org.apache.seata.tm.api.TransactionalTemplate {
+}
diff --git
a/compatible/src/main/java/io/seata/tm/api/transaction/SuspendedResourcesHolder.java
b/compatible/src/main/java/io/seata/tm/api/transaction/SuspendedResourcesHolder.java
new file mode 100644
index 0000000000..38bf15bc74
--- /dev/null
+++
b/compatible/src/main/java/io/seata/tm/api/transaction/SuspendedResourcesHolder.java
@@ -0,0 +1,27 @@
+/*
+ * 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 io.seata.tm.api.transaction;
+
+/**
+ * Holder for suspended resources to support propagation or nested logic.
+ * Used by {@code suspend} and {@code resume}
+ */
+public class SuspendedResourcesHolder extends
org.apache.seata.tm.api.transaction.SuspendedResourcesHolder {
+ public SuspendedResourcesHolder(String xid) {
+ super(xid);
+ }
+}
diff --git
a/tm/src/main/java/org/apache/seata/tm/api/DefaultGlobalTransaction.java
b/tm/src/main/java/org/apache/seata/tm/api/DefaultGlobalTransaction.java
index ec3de3ecdc..76b58c547e 100644
--- a/tm/src/main/java/org/apache/seata/tm/api/DefaultGlobalTransaction.java
+++ b/tm/src/main/java/org/apache/seata/tm/api/DefaultGlobalTransaction.java
@@ -77,7 +77,7 @@ public class DefaultGlobalTransaction implements
GlobalTransaction {
* @param status the status
* @param role the role
*/
- DefaultGlobalTransaction(String xid, GlobalStatus status,
GlobalTransactionRole role) {
+ public DefaultGlobalTransaction(String xid, GlobalStatus status,
GlobalTransactionRole role) {
this.transactionManager = TransactionManagerHolder.get();
this.xid = xid;
this.status = status;
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]