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

tuichenchuxin 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 086c4739034 Add xa resource exceeds length check(#22768) (#22772)
086c4739034 is described below

commit 086c47390346d4c0695c12228597ca01412d9ef0
Author: ZhangCheng <[email protected]>
AuthorDate: Mon Dec 12 09:50:18 2022 +0800

    Add xa resource exceeds length check(#22768) (#22772)
    
    * Add xa resource exceeds length check(#22768)
    
    * Fix test
---
 .../user-manual/error-code/sql-error-code.cn.md        |  1 +
 .../user-manual/error-code/sql-error-code.en.md        |  1 +
 .../transaction/core/ResourceDataSource.java           |  7 ++++++-
 .../transaction/core/ResourceIDGenerator.java          |  2 +-
 .../XAResourceNameLengthExceededException.java}        | 18 ++++++++++--------
 .../transaction/core/ResourceDataSourceTest.java       |  9 ++++++++-
 .../transaction/core/ResourceIDGeneratorTest.java      |  9 ++++++++-
 7 files changed, 35 insertions(+), 12 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 1d36f0e4944..fec376ae06f 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
@@ -65,6 +65,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | 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. |
 
 ### 锁
 
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 398f76e770e..0c65a07ae9d 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
@@ -65,6 +65,7 @@ SQL error codes provide by standard `SQL State`, `Vendor 
Code` and `Reason`, whi
 | 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. |
 
 ### Lock
 
diff --git 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/core/ResourceDataSource.java
 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/core/ResourceDataSource.java
index d85fb9f7a8a..0aa18b5d921 100644
--- 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/core/ResourceDataSource.java
+++ 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/core/ResourceDataSource.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.transaction.core;
 
 import com.google.common.base.Preconditions;
 import lombok.Getter;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
+import 
org.apache.shardingsphere.transaction.exception.XAResourceNameLengthExceededException;
 
 import javax.sql.DataSource;
 
@@ -28,6 +30,8 @@ import javax.sql.DataSource;
 @Getter
 public final class ResourceDataSource {
     
+    private static final int MAX_RESOURCE_NAME_LENGTH = 45;
+    
     private final String originalName;
     
     private final String uniqueResourceName;
@@ -39,6 +43,7 @@ public final class ResourceDataSource {
         Preconditions.checkState(2 == databaseAndDataSourceName.length, 
String.format("Database and data source name must be provided,`%s`.", 
originalName));
         this.originalName = originalName;
         this.dataSource = dataSource;
-        this.uniqueResourceName = ResourceIDGenerator.getInstance().nextId() + 
databaseAndDataSourceName[1];
+        uniqueResourceName = ResourceIDGenerator.getInstance().nextId() + 
databaseAndDataSourceName[1];
+        
ShardingSpherePreconditions.checkState(uniqueResourceName.getBytes().length <= 
MAX_RESOURCE_NAME_LENGTH, () -> new 
XAResourceNameLengthExceededException(uniqueResourceName));
     }
 }
diff --git 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/core/ResourceIDGenerator.java
 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/core/ResourceIDGenerator.java
index 724172c24cd..251f96151d3 100644
--- 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/core/ResourceIDGenerator.java
+++ 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/core/ResourceIDGenerator.java
@@ -47,6 +47,6 @@ public final class ResourceIDGenerator {
      * @return next ID
      */
     String nextId() {
-        return String.format("resource-%d-", count.incrementAndGet());
+        return String.format("%d-", count.incrementAndGet());
     }
 }
diff --git 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/ResourceIDGeneratorTest.java
 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/XAResourceNameLengthExceededException.java
similarity index 56%
copy from 
kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/ResourceIDGeneratorTest.java
copy to 
kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/XAResourceNameLengthExceededException.java
index fcd0816131d..d0d0f131ee5 100644
--- 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/ResourceIDGeneratorTest.java
+++ 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/XAResourceNameLengthExceededException.java
@@ -15,16 +15,18 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.transaction.core;
+package org.apache.shardingsphere.transaction.exception;
 
-import org.junit.Test;
+import 
org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
 
-import static org.junit.Assert.assertTrue;
-
-public final class ResourceIDGeneratorTest {
+/**
+ * XA unique resource name length exceeded exception.
+ */
+public final class XAResourceNameLengthExceededException extends 
TransactionSQLException {
+    
+    private static final long serialVersionUID = 6190231034576044165L;
     
-    @Test
-    public void assertNextIdProperly() {
-        
assertTrue(ResourceIDGenerator.getInstance().nextId().contains("resource"));
+    public XAResourceNameLengthExceededException(final String 
uniqueResourceName) {
+        super(XOpenSQLState.INVALID_TRANSACTION_STATE, 202, String.format("Max 
length of xa unique resource name `%s` exceeded: should be less than 45.", 
uniqueResourceName));
     }
 }
diff --git 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/ResourceDataSourceTest.java
 
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/ResourceDataSourceTest.java
index ca51fb9fced..2f3871e6b96 100644
--- 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/ResourceDataSourceTest.java
+++ 
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/ResourceDataSourceTest.java
@@ -20,6 +20,8 @@ package org.apache.shardingsphere.transaction.core;
 import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
 import org.junit.Test;
 
+import java.util.regex.Pattern;
+
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -37,10 +39,15 @@ public final class ResourceDataSourceTest {
         ResourceDataSource actual = new ResourceDataSource(originalName, new 
MockedDataSource());
         assertThat(actual.getOriginalName(), is(originalName));
         assertThat(actual.getDataSource(), instanceOf(MockedDataSource.class));
-        assertTrue(actual.getUniqueResourceName().startsWith("resource"));
+        assertTrue(isStartWithNumber(actual.getUniqueResourceName()));
         assertTrue(actual.getUniqueResourceName().endsWith(DATA_SOURCE_NAME));
     }
     
+    private boolean isStartWithNumber(final String resourceId) {
+        Pattern pattern = Pattern.compile("[0-9]+-.*");
+        return pattern.matcher(resourceId).matches();
+    }
+    
     @Test(expected = IllegalStateException.class)
     public void assertDataSourceNameOnlyFailure() {
         new ResourceDataSource(DATA_SOURCE_NAME, new MockedDataSource());
diff --git 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/ResourceIDGeneratorTest.java
 
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/ResourceIDGeneratorTest.java
index fcd0816131d..adbb1ba9c79 100644
--- 
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/ResourceIDGeneratorTest.java
+++ 
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/core/ResourceIDGeneratorTest.java
@@ -19,12 +19,19 @@ package org.apache.shardingsphere.transaction.core;
 
 import org.junit.Test;
 
+import java.util.regex.Pattern;
+
 import static org.junit.Assert.assertTrue;
 
 public final class ResourceIDGeneratorTest {
     
     @Test
     public void assertNextIdProperly() {
-        
assertTrue(ResourceIDGenerator.getInstance().nextId().contains("resource"));
+        
assertTrue(isStartWithNumber(ResourceIDGenerator.getInstance().nextId()));
+    }
+    
+    private boolean isStartWithNumber(final String resourceId) {
+        Pattern pattern = Pattern.compile("[0-9]+-.*");
+        return pattern.matcher(resourceId).matches();
     }
 }

Reply via email to