Skip to content

Commit c874b0b

Browse files
committed
[#2478] Terrible error reporting when JDBC scheme is not recognized
1 parent 82340d1 commit c874b0b

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/logging/impl/Log.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,8 @@ public interface Log extends BasicLogger {
159159
@Message(id = 47, value = "Generated identifier smaller or equal to 0: %1$d")
160160
HibernateException generatedIdentifierSmallerOrEqualThanZero(Long id);
161161

162-
@Message(id = 48, value = "Could not determine Dialect from JDBC driver metadata (specify a connection URI with scheme 'postgresql:', 'mysql:', 'cockroachdb', or 'db2:')")
163-
HibernateException couldNotDetermineDialectFromJdbcDriverMetadata();
164-
165-
@Message(id = 49, value = "Could not determine Dialect from connection URI: '%1$s' (specify a connection URI with scheme 'postgresql:', 'mysql:', 'cockroachdb', or 'db2:')")
166-
HibernateException couldNotDetermineDialectFromConnectionURI(String url);
162+
@Message(id = 49, value = "Could not determine Dialect from connection URI: '%1$s' (specify a connection URI with scheme 'postgresql:', 'postgres:', 'mariadb:', 'mysql:', 'db2:', 'cockroachdb:', 'sqlserver:' or 'oracle:' )")
163+
IllegalArgumentException couldNotDetermineDialectFromConnectionURI(String url);
167164

168165
@Message(id = 50, value = "SelectGenerator is not supported in Hibernate Reactive")
169166
HibernateException selectGeneratorIsNotSupportedInHibernateReactive();

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,16 @@ 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 LOG.couldNotDetermineDialectFromConnectionURI( uri.toString() );
96+
}
97+
98+
final String path = scheme.equals( "oracle" )
9599
? oraclePath( uri )
96100
: uri.getPath();
97101

98-
String database = path.length() > 0
102+
String database = path != null && !path.isEmpty()
99103
? path.substring( 1 )
100104
: "";
101105

@@ -105,8 +109,8 @@ public SqlConnectOptions connectOptions(URI uri) {
105109
database = database.substring( 0, database.indexOf( ':' ) );
106110
}
107111

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

111115
//see if the credentials were specified via properties
112116
String username = user;
@@ -385,4 +389,11 @@ private int defaultPort(String scheme) {
385389
}
386390
}
387391

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

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
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;
33+
import static org.hibernate.internal.util.config.ConfigurationHelper.getBooleanWrapper;
3234
import static org.hibernate.reactive.util.impl.CompletionStages.completedFuture;
3335

3436
/**
@@ -84,7 +86,8 @@ protected JdbcEnvironmentImpl getJdbcEnvironmentUsingJdbcMetadata(
8486
Integer explicitDatabaseMajorVersion,
8587
Integer explicitDatabaseMinorVersion,
8688
String explicitDatabaseVersion) {
87-
try {
89+
if ( getBooleanWrapper( ALLOW_METADATA_ON_BOOT, configurationValues, true ) ) {
90+
// We query the database for the metadata to build the Dialect
8891
final Dialect dialect = new DialectBuilder( configurationValues, registry )
8992
.build(
9093
dialectFactory,
@@ -97,7 +100,8 @@ protected JdbcEnvironmentImpl getJdbcEnvironmentUsingJdbcMetadata(
97100
);
98101
return new JdbcEnvironmentImpl( registry, dialect );
99102
}
100-
catch (RuntimeException e) {
103+
else {
104+
// We don't query the database but use default config values
101105
return getJdbcEnvironmentWithDefaults( configurationValues, registry, dialectFactory );
102106
}
103107
}

0 commit comments

Comments
 (0)