TeslaCN commented on issue #22544:
URL: 
https://github.com/apache/shardingsphere/issues/22544#issuecomment-1499873179

   I've confirmed this issue.
   
   I wrote a test. Exclude PostgreSQL JDBC Driver from classpath.
   ```java
   import org.apache.shardingsphere.dialect.SQLExceptionTransformEngine;
   import 
org.apache.shardingsphere.dialect.postgresql.exception.authority.InvalidPasswordException;
   import org.junit.jupiter.api.Test;
   
   class SQLExceptionTransformEngineTest {
       
       @Test
       void assertMySQLToSQLException() {
           SQLExceptionTransformEngine.toSQLException(new 
InvalidPasswordException("user"), "MySQL");
       }
   }
   ```
   
   ```
   java.util.ServiceConfigurationError: 
org.apache.shardingsphere.dialect.mapper.SQLDialectExceptionMapper: 
org.apache.shardingsphere.dialect.postgresql.mapper.PostgreSQLDialectExceptionMapper
 Unable to get public no-arg constructor
   
        at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:586)
        at 
java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:679)
        at 
java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1240)
        at 
java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1273)
        at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1309)
        at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1393)
        at 
org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader.load(ShardingSphereServiceLoader.java:58)
        at 
org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader.<init>(ShardingSphereServiceLoader.java:48)
        at 
java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708)
        at 
org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader.getServiceInstances(ShardingSphereServiceLoader.java:75)
        at 
org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader.findService(TypedSPILoader.java:72)
        at 
org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader.findService(TypedSPILoader.java:56)
        at 
org.apache.shardingsphere.dialect.SQLExceptionTransformEngine.toSQLException(SQLExceptionTransformEngine.java:57)
        at 
org.apache.shardingsphere.proxy.SQLExceptionTransformEngineTest.assertMySQLToSQLException(SQLExceptionTransformEngineTest.java:28)
   
   Omit junit stacks...
   
   Caused by: java.lang.NoClassDefFoundError: org/postgresql/util/PSQLException
        at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
        at 
java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3373)
        at java.base/java.lang.Class.getConstructor0(Class.java:3578)
        at java.base/java.lang.Class.getConstructor(Class.java:2271)
        at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:666)
        at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:663)
        at 
java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
        at 
java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:674)
        ... 82 more
   Caused by: java.lang.ClassNotFoundException: 
org.postgresql.util.PSQLException
        at 
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at 
java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
        ... 90 more
   ```
   
   I changed the convert method to the following:
   ```java
   public final class PostgreSQLDialectExceptionMapper implements 
SQLDialectExceptionMapper {
       
       @Override
       public SQLException convert(final SQLDialectException 
sqlDialectException) {
           PSQLException throwables = new 
PSQLException(sqlDialectException.getMessage(), PSQLState.UNEXPECTED_ERROR);
           return throwables;
   //        return null;
       }
   // ClassNotFoundException occurred in test.
   ```
   
   
   ```java
   public final class PostgreSQLDialectExceptionMapper implements 
SQLDialectExceptionMapper {
       
       @Override
       public SQLException convert(final SQLDialectException 
sqlDialectException) {
           PSQLException throwables = new 
PSQLException(sqlDialectException.getMessage(), PSQLState.UNEXPECTED_ERROR);
   //        return throwables;
           return null;
       }
   // No error occurred in test.
   ```
   
   **So this issue is caused by returning an instance of a class not in 
classpath.**
   
   Adding `-Xverify:none` or `-noverify` could avoid `ClassNotFoundException`, 
but options `-Xverify:none` and `-noverify` were deprecated in JDK 13 and will 
likely be removed in a future release.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to