Skip to content

Commit e43f1f7

Browse files
committed
[#2478] Terrible error reporting when JDBC scheme is not recognized
1 parent f2e7ec0 commit e43f1f7

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPoolConfiguration.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,17 @@ public PoolOptions poolOptions() {
9090

9191
@Override
9292
public SqlConnectOptions connectOptions(URI uri) {
93-
String scheme = uri.getScheme();
94-
String path = scheme.equals( "oracle" )
93+
final String scheme = uri.getScheme();
94+
if ( !isSchemeSupported( scheme ) ) {
95+
throw new IllegalArgumentException( "Unsupported URI scheme: " + scheme + " for uri:" + uri
96+
+ "; supported schemes are 'postgresql', 'postgres', 'mariadb', 'mysql', 'db2', 'cockroachdb', 'sqlserver', 'oracle' ");
97+
}
98+
99+
final String path = scheme.equals( "oracle" )
95100
? oraclePath( uri )
96101
: uri.getPath();
97102

98-
String database = path.length() > 0
103+
String database = path != null && !path.isEmpty()
99104
? path.substring( 1 )
100105
: "";
101106

@@ -105,8 +110,8 @@ public SqlConnectOptions connectOptions(URI uri) {
105110
database = database.substring( 0, database.indexOf( ':' ) );
106111
}
107112

108-
String host = findHost( uri, scheme );
109-
int port = findPort( uri, scheme );
113+
final String host = findHost( uri, scheme );
114+
final int port = findPort( uri, scheme );
110115

111116
//see if the credentials were specified via properties
112117
String username = user;
@@ -385,4 +390,11 @@ private int defaultPort(String scheme) {
385390
}
386391
}
387392

393+
private boolean isSchemeSupported(String scheme) {
394+
return switch ( scheme ) {
395+
case "postgresql", "postgres", "mariadb", "mysql", "db2", "cockroachdb", "sqlserver", "oracle" -> true;
396+
default -> false;
397+
};
398+
}
399+
388400
}

hibernate-reactive-core/src/main/java/org/hibernate/reactive/provider/service/NoJdbcEnvironmentInitiator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static java.lang.Integer.parseInt;
3030
import static java.util.Objects.requireNonNullElse;
3131
import static java.util.function.Function.identity;
32+
import static org.hibernate.cfg.JdbcSettings.ALLOW_METADATA_ON_BOOT;
3233
import static org.hibernate.reactive.util.impl.CompletionStages.completedFuture;
3334

3435
/**
@@ -84,7 +85,7 @@ protected JdbcEnvironmentImpl getJdbcEnvironmentUsingJdbcMetadata(
8485
Integer explicitDatabaseMajorVersion,
8586
Integer explicitDatabaseMinorVersion,
8687
String explicitDatabaseVersion) {
87-
try {
88+
if ( configurationValues.containsKey( ALLOW_METADATA_ON_BOOT ) ) {
8889
final Dialect dialect = new DialectBuilder( configurationValues, registry )
8990
.build(
9091
dialectFactory,
@@ -97,7 +98,7 @@ protected JdbcEnvironmentImpl getJdbcEnvironmentUsingJdbcMetadata(
9798
);
9899
return new JdbcEnvironmentImpl( registry, dialect );
99100
}
100-
catch (RuntimeException e) {
101+
else {
101102
return getJdbcEnvironmentWithDefaults( configurationValues, registry, dialectFactory );
102103
}
103104
}

0 commit comments

Comments
 (0)