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 412ead56c..2a95cc716 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 @@ -93,11 +93,28 @@ public PoolOptions poolOptions() { public SqlConnectOptions connectOptions(URI uri) { String scheme = uri.getScheme(); if( scheme.equalsIgnoreCase( "h2" )) { - return new SqlConnectOptions() + // H2 separates parameters in the url with a semicolon (';') + // "jdbc:h2:~/test;DATABASE_TO_UPPER=FALSE"; + SqlConnectOptions options = new SqlConnectOptions() // username .setUser("sa") // password .setPassword(""); + int index = uri.toString().indexOf( ';' ); + if( index > -1 ) { + String query = uri.toString().substring( index+1 ); + String[] params = query.split( ";" ); + for(String param : params) { + final int position = param.indexOf( "=" ); + if ( position != -1 ) { + // We assume the first '=' is the one separating key and value + String key = param.substring( 0, position ); + String value = param.substring( position + 1 ); + options.addProperty( key, value ); + } + } + } + return options; } String path = scheme.equals( "oracle" ) ? oraclePath( uri ) diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/H2SqlClientPool.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/H2SqlClientPool.java index b49ab06c6..56ea174c2 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/H2SqlClientPool.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/H2SqlClientPool.java @@ -11,7 +11,6 @@ import java.util.Map; import java.util.concurrent.CompletionStage; -import org.hibernate.HibernateError; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.SqlStatementLogger; import org.hibernate.internal.util.config.ConfigurationHelper; @@ -38,6 +37,8 @@ public class H2SqlClientPool extends SqlClientPool private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() ); + private static final String DEFAULT_URL = "jdbc:h2:~/test;DATABASE_TO_UPPER=FALSE"; + //Asynchronous shutdown promise: we can't return it from #close as we implement a //blocking interface. private volatile Future closeFuture = Future.succeededFuture(); @@ -108,9 +109,7 @@ public void stop() { public static URI parse(String url) { if ( url == null || url.trim().isEmpty() ) { - throw new HibernateError( - "The configuration property '" + Settings.URL + "' was not provided, or is in invalid format. This is required when using the default DefaultSqlClientPool: " + - "either provide the configuration setting or integrate with a different SqlClientPool implementation" ); + return URI.create( DEFAULT_URL ); } if ( url.startsWith( "jdbc:" ) ) { diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers/H2Database.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers/H2Database.java index 08c23d261..ad6a0b355 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers/H2Database.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers/H2Database.java @@ -87,7 +87,7 @@ public String getUri() { @Override public String getScheme() { - return "h2"; + return "h2:"; } @Override @@ -104,7 +104,31 @@ public String getExpectedNativeDatatype(Class dataType) { @Override public String createJdbcUrl(String host, int port, String database, Map params) { - return getRegularJdbcUrl(); + // Primary mode for H2 is embedded which uses the URL format: "jdbc:h2:~/test" + // H2 can also be configured as a remote server. + // EXAMPLE 1: jdbc:h2:tcp://localhost/D:/myproject/data/project-name + // EXAMPLE 2: jdbc:h2:tcp://localhost/~/test + // EXAMpLE 3: jdbc:h2:tcp://localhost:9081/~/test + final StringBuilder paramsBuilder = new StringBuilder(); + if ( params != null && !params.isEmpty() ) { + params.forEach( (key, value) -> { + paramsBuilder.append( jdbcParamDelimiter() ); + paramsBuilder.append( key ); + paramsBuilder.append( "=" ); + paramsBuilder.append( value ); + } ); + } + String url = "jdbc:" + getScheme() + "//" + host; + if ( port > -1 ) { + url += ":" + port; + } + if ( paramsBuilder.length() > 0 ) { + url += jdbcStartQuery() + paramsBuilder.substring( 1 ); + } + if ( database != null ) { + return url + ";database=" + database; + } + return url; } @Override