Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
: "";

Expand All @@ -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;
Expand Down Expand Up @@ -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;
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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,
Expand All @@ -97,7 +98,7 @@ protected JdbcEnvironmentImpl getJdbcEnvironmentUsingJdbcMetadata(
);
return new JdbcEnvironmentImpl( registry, dialect );
}
catch (RuntimeException e) {
else {
return getJdbcEnvironmentWithDefaults( configurationValues, registry, dialectFactory );
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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:" );
}

}
Loading