Skip to content

Commit

Permalink
[hibernate#929] filled out H2 client pool
Browse files Browse the repository at this point in the history
  • Loading branch information
blafond committed Mar 18, 2022
1 parent bd9aab9 commit 279d537
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Void> closeFuture = Future.succeededFuture();
Expand All @@ -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<Void> 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() );
}

Expand All @@ -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<Void> getCloseFuture() {
return closeFuture.toCompletionStage();
protected SqlStatementLogger getSqlStatementLogger() {
return sqlStatementLogger;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -50,12 +49,12 @@ public String createJdbcUrl(String host, int port, String database, Map<String,

@Override
public String jdbcStartQuery() {
throw new UnsupportedOperationException();
return ";";
}

@Override
public String jdbcParamDelimiter() {
throw new UnsupportedOperationException();
return ";";
}

private H2Database() {
Expand Down

0 comments on commit 279d537

Please sign in to comment.