This is an automated email from the ASF dual-hosted git repository. jshao pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push: new 38ffa8588e [#6506] fix(core): fix SQLExceptionConverterFactory of entity storage not close (#6624) 38ffa8588e is described below commit 38ffa8588e86bbaeac7ff45dbf8532427e97a178 Author: mchades <liminghu...@datastrato.com> AuthorDate: Fri Mar 7 10:08:08 2025 +0800 [#6506] fix(core): fix SQLExceptionConverterFactory of entity storage not close (#6624) ### What changes were proposed in this pull request? fix SQLExceptionConverterFactory of entity storage not close ### Why are the changes needed? Fix: #6506 ### Does this PR introduce _any_ user-facing change? no ### How was this patch tested? CI pass --- .../org/apache/gravitino/storage/relational/JDBCBackend.java | 1 + .../storage/relational/converters/H2ExceptionConverter.java | 4 +++- .../relational/converters/MySQLExceptionConverter.java | 2 +- .../relational/converters/SQLExceptionConverterFactory.java | 12 +++++++++++- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/JDBCBackend.java b/core/src/main/java/org/apache/gravitino/storage/relational/JDBCBackend.java index 8e3cf5c871..a089251297 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/JDBCBackend.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/JDBCBackend.java @@ -377,6 +377,7 @@ public class JDBCBackend implements RelationalBackend { @Override public void close() throws IOException { SqlSessionFactoryHelper.getInstance().close(); + SQLExceptionConverterFactory.close(); if (jdbcDatabase != null) { jdbcDatabase.close(); diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java b/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java index a64db131a1..ff297d8d27 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java @@ -37,10 +37,12 @@ public class H2ExceptionConverter implements SQLExceptionConverter { throws IOException { switch (se.getErrorCode()) { case DUPLICATED_ENTRY_ERROR_CODE: + // compatible with H2 in MySQL mode + case MySQLExceptionConverter.DUPLICATED_ENTRY_ERROR_CODE: throw new EntityAlreadyExistsException( se, "The %s entity: %s already exists.", type.name(), name); default: - throw new IOException(se); + throw new IOException("error code: " + se.getErrorCode(), se); } } } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/converters/MySQLExceptionConverter.java b/core/src/main/java/org/apache/gravitino/storage/relational/converters/MySQLExceptionConverter.java index 1190f9ce4b..6b5035f23a 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/converters/MySQLExceptionConverter.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/converters/MySQLExceptionConverter.java @@ -30,7 +30,7 @@ import org.apache.gravitino.EntityAlreadyExistsException; */ public class MySQLExceptionConverter implements SQLExceptionConverter { /** It means found a duplicated primary key or unique key entry in MySQL. */ - private static final int DUPLICATED_ENTRY_ERROR_CODE = 1062; + static final int DUPLICATED_ENTRY_ERROR_CODE = 1062; @SuppressWarnings("FormatStringAnnotation") @Override diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/converters/SQLExceptionConverterFactory.java b/core/src/main/java/org/apache/gravitino/storage/relational/converters/SQLExceptionConverterFactory.java index feb0fb0d7d..66b4213047 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/converters/SQLExceptionConverterFactory.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/converters/SQLExceptionConverterFactory.java @@ -26,7 +26,7 @@ import org.apache.gravitino.Configs; public class SQLExceptionConverterFactory { private static final Pattern TYPE_PATTERN = Pattern.compile("jdbc:(\\w+):"); - private static SQLExceptionConverter converter; + private static volatile SQLExceptionConverter converter; private SQLExceptionConverterFactory() {} @@ -56,4 +56,14 @@ public class SQLExceptionConverterFactory { Preconditions.checkState(converter != null, "Exception converter is not initialized."); return converter; } + + public static void close() { + if (converter != null) { + synchronized (SQLExceptionConverterFactory.class) { + if (converter != null) { + converter = null; + } + } + } + } }