From 279d5378c89bdaa41a7a765483541a5d80d2f8ba Mon Sep 17 00:00:00 2001 From: blafond Date: Fri, 18 Mar 2022 10:23:05 -0500 Subject: [PATCH] [#929] filled out H2 client pool --- gradle.properties | 2 +- .../reactive/pool/impl/H2SqlClientPool.java | 59 +++++++++++++++---- .../reactive/containers/H2Database.java | 11 ++-- 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/gradle.properties b/gradle.properties index 123a3e584..048d3317c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -26,7 +26,7 @@ org.gradle.java.installations.auto-download=false # The database type to use (key insensitive and support aliases): # Db2, MySql, PostgreSQL, CockroachDB, SqlServer -#db = MSSQL +db = H2 # Enable the SonatypeOS maven repository (mainly for Vert.x snapshots) when present (value ignored) #enableSonatypeOpenSourceSnapshotsRep = true 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 aba2c4cc4..1825096a8 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 @@ -6,12 +6,19 @@ package org.hibernate.reactive.pool.impl; +import java.lang.invoke.MethodHandles; import java.net.URI; +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; +import org.hibernate.reactive.logging.impl.Log; +import org.hibernate.reactive.logging.impl.LoggerFactory; import org.hibernate.reactive.pool.ReactiveConnection; +import org.hibernate.reactive.provider.Settings; import org.hibernate.reactive.vertx.VertxInstance; import org.hibernate.service.spi.ServiceRegistryAwareService; import org.hibernate.service.spi.ServiceRegistryImplementor; @@ -27,6 +34,8 @@ public class H2SqlClientPool extends SqlClientPool implements ServiceRegistryAwareService { + private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() ); + //Asynchronous shutdown promise: we can't return it from #close as we implement a //blocking interface. private volatile Future closeFuture = Future.succeededFuture(); @@ -45,22 +54,29 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) { sqlStatementLogger = serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger(); } + public void configure(Map configuration) { + uri = jdbcUrl( configuration ); + } + public void start() { if ( pools == null ) { pools = createPool( uri ); } } - public void stop() { - if ( pools != null ) { - this.closeFuture = pools.close(); - } + @Override + public CompletionStage getCloseFuture() { + return closeFuture.toCompletionStage(); + } + + @Override + protected Pool getPool() { + return pools; } private Pool createPool(URI uri) { SqlClientPoolConfiguration configuration = serviceRegistry.getService( SqlClientPoolConfiguration.class ); VertxInstance vertx = serviceRegistry.getService( VertxInstance.class ); - return createPool( uri, configuration.connectOptions( uri ), configuration.poolOptions(), vertx.getVertx() ); } @@ -73,19 +89,36 @@ private Pool createPool(URI uri, SqlConnectOptions connectOptions, PoolOptions p return pool; } - @Override - protected Pool getPool() { - return pools; + private URI jdbcUrl(Map configurationValues) { + String url = ConfigurationHelper.getString( Settings.URL, configurationValues ); + LOG.sqlClientUrl( url ); + return parse( url ); } - @Override - protected SqlStatementLogger getSqlStatementLogger() { - return sqlStatementLogger; + public void stop() { + if ( pools != null ) { + this.closeFuture = pools.close(); + } + } + + 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" ); + } + + if ( url.startsWith( "jdbc:" ) ) { + return URI.create( url.substring( 5 ) ); + } + + return URI.create( url ); } @Override - public CompletionStage getCloseFuture() { - return closeFuture.toCompletionStage(); + protected SqlStatementLogger getSqlStatementLogger() { + return sqlStatementLogger; } @Override 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 a9b15a8bf..f3ecbc6a7 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 @@ -18,16 +18,15 @@ private String getRegularJdbcUrl() { @Override public String getJdbcUrl() { - return getRegularJdbcUrl(); + return "jdbc:h2:~/test"; } @Override public String getUri() { - { - return "h2:~/test"; - } + return "h2:~/test"; } + @Override public String getScheme() { return "h2"; @@ -50,12 +49,12 @@ public String createJdbcUrl(String host, int port, String database, Map