From e43f1f743113a1117d7c0fa16404d9f76d316c1c Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 9 Sep 2025 16:46:03 +0200 Subject: [PATCH 1/2] [#2478] Terrible error reporting when JDBC scheme is not recognized --- .../DefaultSqlClientPoolConfiguration.java | 22 ++++++++++++++----- .../service/NoJdbcEnvironmentInitiator.java | 5 +++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPoolConfiguration.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPoolConfiguration.java index e0613b78c..6867051ef 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPoolConfiguration.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPoolConfiguration.java @@ -90,12 +90,17 @@ public PoolOptions poolOptions() { @Override public SqlConnectOptions connectOptions(URI uri) { - String scheme = uri.getScheme(); - String path = scheme.equals( "oracle" ) + final String scheme = uri.getScheme(); + if ( !isSchemeSupported( scheme ) ) { + throw new IllegalArgumentException( "Unsupported URI scheme: " + scheme + " for uri:" + uri + + "; supported schemes are 'postgresql', 'postgres', 'mariadb', 'mysql', 'db2', 'cockroachdb', 'sqlserver', 'oracle' "); + } + + final String path = scheme.equals( "oracle" ) ? oraclePath( uri ) : uri.getPath(); - String database = path.length() > 0 + String database = path != null && !path.isEmpty() ? path.substring( 1 ) : ""; @@ -105,8 +110,8 @@ public SqlConnectOptions connectOptions(URI uri) { database = database.substring( 0, database.indexOf( ':' ) ); } - String host = findHost( uri, scheme ); - int port = findPort( uri, scheme ); + final String host = findHost( uri, scheme ); + final int port = findPort( uri, scheme ); //see if the credentials were specified via properties String username = user; @@ -385,4 +390,11 @@ private int defaultPort(String scheme) { } } + private boolean isSchemeSupported(String scheme) { + return switch ( scheme ) { + case "postgresql", "postgres", "mariadb", "mysql", "db2", "cockroachdb", "sqlserver", "oracle" -> true; + default -> false; + }; + } + } diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/provider/service/NoJdbcEnvironmentInitiator.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/provider/service/NoJdbcEnvironmentInitiator.java index 0cf59c413..e641b9b50 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/provider/service/NoJdbcEnvironmentInitiator.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/provider/service/NoJdbcEnvironmentInitiator.java @@ -29,6 +29,7 @@ import static java.lang.Integer.parseInt; import static java.util.Objects.requireNonNullElse; import static java.util.function.Function.identity; +import static org.hibernate.cfg.JdbcSettings.ALLOW_METADATA_ON_BOOT; import static org.hibernate.reactive.util.impl.CompletionStages.completedFuture; /** @@ -84,7 +85,7 @@ protected JdbcEnvironmentImpl getJdbcEnvironmentUsingJdbcMetadata( Integer explicitDatabaseMajorVersion, Integer explicitDatabaseMinorVersion, String explicitDatabaseVersion) { - try { + if ( configurationValues.containsKey( ALLOW_METADATA_ON_BOOT ) ) { final Dialect dialect = new DialectBuilder( configurationValues, registry ) .build( dialectFactory, @@ -97,7 +98,7 @@ protected JdbcEnvironmentImpl getJdbcEnvironmentUsingJdbcMetadata( ); return new JdbcEnvironmentImpl( registry, dialect ); } - catch (RuntimeException e) { + else { return getJdbcEnvironmentWithDefaults( configurationValues, registry, dialectFactory ); } } From ee8e2ef7d33f8450203611f3cacc12102c915c0f Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 9 Sep 2025 16:46:47 +0200 Subject: [PATCH 2/2] [#2478] Add test for terrible error reporting when JDBC scheme is not recognized --- .../reactive/WrongJdbcUrlShemeTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 hibernate-reactive-core/src/test/java/org/hibernate/reactive/WrongJdbcUrlShemeTest.java diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/WrongJdbcUrlShemeTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/WrongJdbcUrlShemeTest.java new file mode 100644 index 000000000..cb2f149c2 --- /dev/null +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/WrongJdbcUrlShemeTest.java @@ -0,0 +1,51 @@ +/* Hibernate, Relational Persistence for Idiomatic Java + * + * SPDX-License-Identifier: Apache-2.0 + * Copyright: Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.reactive; + +import org.hibernate.cfg.Configuration; +import org.hibernate.reactive.provider.Settings; +import org.hibernate.service.spi.ServiceException; + +import org.junit.jupiter.api.Test; + +import io.vertx.junit5.Timeout; +import io.vertx.junit5.VertxTestContext; + +import static java.util.concurrent.TimeUnit.MINUTES; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.reactive.testing.ReactiveAssertions.assertThrown; + +/** + * Check that the right exception is thrown when there is an error when the JDBC URI scheme is not recognized. + */ +@Timeout(value = 10, timeUnit = MINUTES) +public class WrongJdbcUrlShemeTest extends BaseReactiveTest { + + @Override + protected void setProperties(Configuration configuration) { + configuration.setProperty( Settings.JAKARTA_JDBC_URL, "jdbc:h2:~/h2temp" ); + configuration.setProperty( Settings.JAKARTA_JDBC_USER, "abc" ); + setSqlLoggingProperties( configuration ); + } + + @Override + public void before(VertxTestContext context) { + // We need to postpone the creation of the factory so that we can check the exception + } + + @Test + public void testWithTransaction(VertxTestContext context) { + test( context, assertThrown( ServiceException.class, setupSessionFactory( this::constructConfiguration ) ) + .thenAccept( WrongJdbcUrlShemeTest::assertException ) + ); + } + + private static void assertException(Throwable throwable) { + assertThat( throwable.getMessage() ) + .contains( "Unsupported URI scheme:" ); + } + +}