|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive.jdbc.pool.impl; |
| 7 | + |
| 8 | + |
| 9 | +import java.lang.invoke.MethodHandles; |
| 10 | +import java.net.URI; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.concurrent.CompletionStage; |
| 13 | + |
| 14 | +import org.hibernate.engine.jdbc.spi.JdbcServices; |
| 15 | +import org.hibernate.engine.jdbc.spi.SqlStatementLogger; |
| 16 | +import org.hibernate.internal.util.config.ConfigurationHelper; |
| 17 | +import org.hibernate.reactive.logging.impl.Log; |
| 18 | +import org.hibernate.reactive.logging.impl.LoggerFactory; |
| 19 | +import org.hibernate.reactive.pool.impl.JdbcClientPoolConfiguration; |
| 20 | +import org.hibernate.reactive.pool.impl.SqlClientPool; |
| 21 | +import org.hibernate.reactive.provider.Settings; |
| 22 | +import org.hibernate.reactive.vertx.VertxInstance; |
| 23 | +import org.hibernate.service.spi.Configurable; |
| 24 | +import org.hibernate.service.spi.ServiceRegistryAwareService; |
| 25 | +import org.hibernate.service.spi.ServiceRegistryImplementor; |
| 26 | +import org.hibernate.service.spi.Startable; |
| 27 | +import org.hibernate.service.spi.Stoppable; |
| 28 | + |
| 29 | +import io.vertx.core.Future; |
| 30 | +import io.vertx.core.json.JsonObject; |
| 31 | +import io.vertx.jdbcclient.JDBCPool; |
| 32 | +import io.vertx.sqlclient.Pool; |
| 33 | + |
| 34 | +public class H2SqlClientPool extends SqlClientPool |
| 35 | + implements ServiceRegistryAwareService, Configurable, Stoppable, Startable { |
| 36 | + |
| 37 | + private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() ); |
| 38 | + |
| 39 | + public static final String DEFAULT_URL = "jdbc:h2:mem:hreact"; |
| 40 | + |
| 41 | + //Asynchronous shutdown promise: we can't return it from #close as we implement a |
| 42 | + //blocking interface. |
| 43 | + private volatile Future<Void> closeFuture = Future.succeededFuture(); |
| 44 | + |
| 45 | + private Pool pools; |
| 46 | + private URI uri; |
| 47 | + private SqlStatementLogger sqlStatementLogger; |
| 48 | + private ServiceRegistryImplementor serviceRegistry; |
| 49 | + |
| 50 | + public H2SqlClientPool() { |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void injectServices(ServiceRegistryImplementor serviceRegistry) { |
| 55 | + this.serviceRegistry = serviceRegistry; |
| 56 | + sqlStatementLogger = serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void configure(Map configuration) { |
| 61 | + uri = jdbcUrl( configuration ); |
| 62 | + } |
| 63 | + |
| 64 | + protected URI jdbcUrl(Map<?,?> configurationValues) { |
| 65 | + String url = ConfigurationHelper.getString( Settings.URL, configurationValues, DEFAULT_URL ); |
| 66 | + LOG.sqlClientUrl( url); |
| 67 | + return URI.create( url ); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void start() { |
| 72 | + if ( pools == null ) { |
| 73 | + pools = createPool( uri ); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public CompletionStage<Void> getCloseFuture() { |
| 79 | + return closeFuture.toCompletionStage(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + protected Pool getPool() { |
| 84 | + return pools; |
| 85 | + } |
| 86 | + |
| 87 | + private Pool createPool(URI uri) { |
| 88 | + JdbcClientPoolConfiguration configuration = serviceRegistry.getService( JdbcClientPoolConfiguration.class ); |
| 89 | + VertxInstance vertx = serviceRegistry.getService( VertxInstance.class ); |
| 90 | + JsonObject poolOptions = configuration.poolOptions().toJson(); |
| 91 | + JsonObject connectOptions = configuration.jdbcConnectOptions( uri ); |
| 92 | + JsonObject config = poolOptions.mergeIn( connectOptions ); |
| 93 | + return JDBCPool.pool( vertx.getVertx(), config ); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void stop() { |
| 98 | + if ( pools != null ) { |
| 99 | + this.closeFuture = pools.close(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected SqlStatementLogger getSqlStatementLogger() { |
| 105 | + return sqlStatementLogger; |
| 106 | + } |
| 107 | +} |
0 commit comments