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

panjuan 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 4516e3ab5ac Add transaction exception codes (#23376)
4516e3ab5ac is described below

commit 4516e3ab5ac3f89b179454ad27f35f842407df1b
Author: ZhangCheng <[email protected]>
AuthorDate: Fri Jan 6 17:26:34 2023 +0800

    Add transaction exception codes (#23376)
---
 .../user-manual/error-code/sql-error-code.cn.md    | 16 +++++++----
 .../user-manual/error-code/sql-error-code.en.md    |  4 +++
 .../ShardingSphereTransactionManagerEngine.java    |  5 ++--
 .../TransactionManagerNotExistedException.java     | 33 ++++++++++++++++++++++
 .../exception/TransactionTimeoutException.java     | 32 +++++++++++++++++++++
 .../SeataATShardingSphereTransactionManager.java   | 25 +++++++++-------
 .../exception/SeataATConfigurationException.java   | 33 ++++++++++++++++++++++
 .../at/exception/SeataATDisabledException.java     | 33 ++++++++++++++++++++++
 .../xa/XAShardingSphereTransactionManager.java     |  6 ++--
 9 files changed, 166 insertions(+), 21 deletions(-)

diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md 
b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
index c228a61083e..2aca666f61c 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
@@ -59,13 +59,17 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 
 ### 事务
 
-| SQL State | Vendor Code | 错误信息 |
-| --------- | ----------- | ------ |
-| 25000     | 14000       | Switch transaction type failed, please terminate 
the current transaction. |
+| SQL State | Vendor Code | 错误信息                                               
                                |
+| --------- 
|-------------|------------------------------------------------------------------------------------|
+| 25000     | 14000       | Switch transaction type failed, please terminate 
the current transaction.          |
+| 25000     | 14001       | Can not find transaction manager of \`%s\`.        
                                 |
+| 25000     | 14002       | Transaction timeout should more than 0s.           
                              |
 | 25000     | 14100       | JDBC does not support operations across multiple 
logical databases in transaction. |
-| 25000     | 14200       | Can not start new XA transaction in a active 
transaction. |
-| 25000     | 14201       | Failed to create \`%s\` XA data source. |
-| 25000     | 14202       | Max length of xa unique resource name \`%s\` 
exceeded: should be less than 45. |
+| 25000     | 14200       | Can not start new XA transaction in a active 
transaction.                          |
+| 25000     | 14201       | Failed to create \`%s\` XA data source.            
                                |
+| 25000     | 14202       | Max length of xa unique resource name \`%s\` 
exceeded: should be less than 45.     |
+| 25000     | 14301       | ShardingSphere Seata-AT transaction has been 
disabled.     |
+| 25000     | 14302       | Please config application id within seata.conf 
file.     |
 
 ### 锁
 
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md 
b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index 97c871ce650..159564ae539 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -62,10 +62,14 @@ SQL error codes provide by standard `SQL State`, `Vendor 
Code` and `Reason`, whi
 | SQL State | Vendor Code | Reason |
 | --------- | ----------- | ------ |
 | 25000     | 14000       | Switch transaction type failed, please terminate 
the current transaction. |
+| 25000     | 14001       | Can not find transaction manager of \`%s\`.        
                                 |
+| 25000     | 14002       | Transaction timeout should more than 0s.           
                              |
 | 25000     | 14100       | JDBC does not support operations across multiple 
logical databases in transaction. |
 | 25000     | 14200       | Can not start new XA transaction in a active 
transaction. |
 | 25000     | 14201       | Failed to create \`%s\` XA data source. |
 | 25000     | 14202       | Max length of xa unique resource name \`%s\` 
exceeded: should be less than 45. |
+| 25000     | 14301       | ShardingSphere Seata-AT transaction has been 
disabled.     |
+| 25000     | 14302       | Please config application id within seata.conf 
file.     |
 
 ### Lock
 
diff --git 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngine.java
 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngine.java
index 1629a667fa8..7a5dd346adb 100644
--- 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngine.java
+++ 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ShardingSphereTransactionManagerEngine.java
@@ -17,11 +17,12 @@
 
 package org.apache.shardingsphere.transaction;
 
-import com.google.common.base.Preconditions;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.transaction.api.TransactionType;
+import 
org.apache.shardingsphere.transaction.exception.TransactionManagerNotExistedException;
 import 
org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager;
 
 import javax.sql.DataSource;
@@ -72,7 +73,7 @@ public final class ShardingSphereTransactionManagerEngine {
     public ShardingSphereTransactionManager getTransactionManager(final 
TransactionType transactionType) {
         ShardingSphereTransactionManager result = 
transactionManagers.get(transactionType);
         if (TransactionType.LOCAL != transactionType) {
-            Preconditions.checkNotNull(result, "Can not find transaction 
manager of `%s`", transactionType);
+            ShardingSpherePreconditions.checkNotNull(result, () -> new 
TransactionManagerNotExistedException(transactionType));
         }
         return result;
     }
diff --git 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/TransactionManagerNotExistedException.java
 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/TransactionManagerNotExistedException.java
new file mode 100644
index 00000000000..76fc9d92ea9
--- /dev/null
+++ 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/TransactionManagerNotExistedException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception;
+
+import 
org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.transaction.api.TransactionType;
+
+/**
+ * Transaction manager not existed exception.
+ */
+public final class TransactionManagerNotExistedException extends 
TransactionSQLException {
+    
+    private static final long serialVersionUID = 3831707403758598143L;
+    
+    public TransactionManagerNotExistedException(final TransactionType 
transactionType) {
+        super(XOpenSQLState.INVALID_TRANSACTION_STATE, 1, String.format("Can 
not find transaction manager of `%s`", transactionType));
+    }
+}
diff --git 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/TransactionTimeoutException.java
 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/TransactionTimeoutException.java
new file mode 100644
index 00000000000..a8404d6979e
--- /dev/null
+++ 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/TransactionTimeoutException.java
@@ -0,0 +1,32 @@
+/*
+ * 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.exception;
+
+import 
org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+
+/**
+ * Transaction timeout exception.
+ */
+public final class TransactionTimeoutException extends TransactionSQLException 
{
+    
+    private static final long serialVersionUID = -2976178098576465693L;
+    
+    public TransactionTimeoutException() {
+        super(XOpenSQLState.INVALID_TRANSACTION_STATE, 2, "Transaction timeout 
should more than 0s");
+    }
+}
diff --git 
a/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManager.java
 
b/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManager.java
index f746657ac9a..f5d033abf73 100644
--- 
a/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManager.java
+++ 
b/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManager.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.transaction.base.seata.at;
 
-import com.google.common.base.Preconditions;
 import io.seata.config.FileConfiguration;
 import io.seata.core.context.RootContext;
 import io.seata.core.exception.TransactionException;
@@ -30,7 +29,11 @@ import io.seata.tm.api.GlobalTransaction;
 import io.seata.tm.api.GlobalTransactionContext;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.transaction.api.TransactionType;
+import 
org.apache.shardingsphere.transaction.base.seata.at.exception.SeataATDisabledException;
+import 
org.apache.shardingsphere.transaction.base.seata.at.exception.SeataATConfigurationException;
+import 
org.apache.shardingsphere.transaction.exception.TransactionTimeoutException;
 import 
org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager;
 
 import javax.sql.DataSource;
@@ -71,7 +74,7 @@ public final class SeataATShardingSphereTransactionManager 
implements ShardingSp
     }
     
     private void initSeataRPCClient() {
-        Preconditions.checkNotNull(applicationId, "please config application 
id within seata.conf file.");
+        ShardingSpherePreconditions.checkNotNull(applicationId, () -> new 
SeataATConfigurationException("Please config application id within seata.conf 
file"));
         TMClient.init(applicationId, transactionServiceGroup);
         RMClient.init(applicationId, transactionServiceGroup);
     }
@@ -83,13 +86,13 @@ public final class SeataATShardingSphereTransactionManager 
implements ShardingSp
     
     @Override
     public boolean isInTransaction() {
-        Preconditions.checkState(enableSeataAT, "ShardingSphere seata-at 
transaction has been disabled.");
+        checkSeataATEnabled();
         return null != RootContext.getXID();
     }
     
     @Override
     public Connection getConnection(final String databaseName, final String 
dataSourceName) throws SQLException {
-        Preconditions.checkState(enableSeataAT, "ShardingSphere seata-at 
transaction has been disabled.");
+        checkSeataATEnabled();
         return dataSourceMap.get(databaseName + "." + 
dataSourceName).getConnection();
     }
     
@@ -101,10 +104,8 @@ public final class SeataATShardingSphereTransactionManager 
implements ShardingSp
     @Override
     @SneakyThrows(TransactionException.class)
     public void begin(final int timeout) {
-        if (timeout < 0) {
-            throw new TransactionException("timeout should more than 0s");
-        }
-        Preconditions.checkState(enableSeataAT, "ShardingSphere seata-at 
transaction has been disabled.");
+        ShardingSpherePreconditions.checkState(timeout >= 0, 
TransactionTimeoutException::new);
+        checkSeataATEnabled();
         GlobalTransaction globalTransaction = 
GlobalTransactionContext.getCurrentOrCreate();
         globalTransaction.begin(timeout * 1000);
         SeataTransactionHolder.set(globalTransaction);
@@ -113,7 +114,7 @@ public final class SeataATShardingSphereTransactionManager 
implements ShardingSp
     @Override
     @SneakyThrows(TransactionException.class)
     public void commit(final boolean rollbackOnly) {
-        Preconditions.checkState(enableSeataAT, "ShardingSphere seata-at 
transaction has been disabled.");
+        checkSeataATEnabled();
         try {
             SeataTransactionHolder.get().commit();
         } finally {
@@ -125,7 +126,7 @@ public final class SeataATShardingSphereTransactionManager 
implements ShardingSp
     @Override
     @SneakyThrows(TransactionException.class)
     public void rollback() {
-        Preconditions.checkState(enableSeataAT, "ShardingSphere seata-at 
transaction has been disabled.");
+        checkSeataATEnabled();
         try {
             SeataTransactionHolder.get().rollback();
         } finally {
@@ -134,6 +135,10 @@ public final class SeataATShardingSphereTransactionManager 
implements ShardingSp
         }
     }
     
+    private void checkSeataATEnabled() {
+        ShardingSpherePreconditions.checkState(enableSeataAT, 
SeataATDisabledException::new);
+    }
+    
     @Override
     public void close() {
         dataSourceMap.clear();
diff --git 
a/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/exception/SeataATConfigurationException.java
 
b/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/exception/SeataATConfigurationException.java
new file mode 100644
index 00000000000..b0c61bcb513
--- /dev/null
+++ 
b/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/exception/SeataATConfigurationException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.base.seata.at.exception;
+
+import 
org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.transaction.exception.TransactionSQLException;
+
+/**
+ * Seata AT configuration exception.
+ */
+public final class SeataATConfigurationException extends 
TransactionSQLException {
+    
+    private static final long serialVersionUID = 3742525073470768226L;
+    
+    public SeataATConfigurationException(final String errorMessage) {
+        super(XOpenSQLState.INVALID_TRANSACTION_STATE, 302, errorMessage);
+    }
+}
diff --git 
a/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/exception/SeataATDisabledException.java
 
b/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/exception/SeataATDisabledException.java
new file mode 100644
index 00000000000..3f4a59733bd
--- /dev/null
+++ 
b/kernel/transaction/type/base/seata-at/src/main/java/org/apache/shardingsphere/transaction/base/seata/at/exception/SeataATDisabledException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.base.seata.at.exception;
+
+import 
org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.transaction.exception.TransactionSQLException;
+
+/**
+ * Seata AT disabled exception.
+ */
+public final class SeataATDisabledException extends TransactionSQLException {
+    
+    private static final long serialVersionUID = 3742525073470768226L;
+    
+    public SeataATDisabledException() {
+        super(XOpenSQLState.INVALID_TRANSACTION_STATE, 301, "ShardingSphere 
Seata-AT transaction has been disabled");
+    }
+}
diff --git 
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
 
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
index 97dd3cb9f14..a934e7ea414 100644
--- 
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
+++ 
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
@@ -19,10 +19,12 @@ package org.apache.shardingsphere.transaction.xa;
 
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import 
org.apache.shardingsphere.infra.util.spi.type.required.RequiredSPIRegistry;
 import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPIRegistry;
 import org.apache.shardingsphere.transaction.api.TransactionType;
 import org.apache.shardingsphere.transaction.core.ResourceDataSource;
+import 
org.apache.shardingsphere.transaction.exception.TransactionTimeoutException;
 import 
org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager;
 import 
org.apache.shardingsphere.transaction.xa.jta.datasource.XATransactionDataSource;
 import 
org.apache.shardingsphere.transaction.xa.spi.XATransactionManagerProvider;
@@ -105,9 +107,7 @@ public final class XAShardingSphereTransactionManager 
implements ShardingSphereT
     @Override
     @SneakyThrows({SystemException.class, NotSupportedException.class})
     public void begin(final int timeout) {
-        if (timeout < 0) {
-            throw new NotSupportedException("timeout should more than 0s");
-        }
+        ShardingSpherePreconditions.checkState(timeout >= 0, 
TransactionTimeoutException::new);
         TransactionManager transactionManager = 
xaTransactionManagerProvider.getTransactionManager();
         transactionManager.setTransactionTimeout(timeout);
         transactionManager.begin();

Reply via email to