Skip to content

Commit d15360b

Browse files
blafondDavideD
authored andcommitted
[hibernate#929] H2 client pool and build dependencies
1 parent 6e790e4 commit d15360b

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

Diff for: hibernate-reactive-core/build.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ dependencies {
2626

2727
//Specific implementation details of Hibernate Reactive:
2828
implementation "io.vertx:vertx-sql-client:${vertxVersion}"
29+
implementation 'io.agroal:agroal-api:1.12'
30+
implementation 'io.agroal:agroal-pool:1.12'
31+
implementation "io.vertx:vertx-jdbc-client:${vertxVersion}"
2932

3033
// Testing
3134
testImplementation 'org.assertj:assertj-core:3.20.2'
@@ -65,6 +68,8 @@ dependencies {
6568
testImplementation "org.testcontainers:cockroachdb:${testcontainersVersion}"
6669
testImplementation "org.testcontainers:mssqlserver:${testcontainersVersion}"
6770
testImplementation "org.testcontainers:oracle-xe:${testcontainersVersion}"
71+
72+
testImplementation "com.h2database:h2:2.1.210"
6873
}
6974

7075
// Print a summary of the results of the tests (number of failures, successes and skipped)

Diff for: hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPoolConfiguration.java

+7
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ public PoolOptions poolOptions() {
9292
@Override
9393
public SqlConnectOptions connectOptions(URI uri) {
9494
String scheme = uri.getScheme();
95+
if( scheme.equalsIgnoreCase( "h2" )) {
96+
return new SqlConnectOptions()
97+
// username
98+
.setUser("sa")
99+
// password
100+
.setPassword("");
101+
}
95102
String path = scheme.equals( "oracle" )
96103
? oraclePath( uri )
97104
: uri.getPath();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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.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.HibernateError;
15+
import org.hibernate.engine.jdbc.spi.JdbcServices;
16+
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
17+
import org.hibernate.internal.util.config.ConfigurationHelper;
18+
import org.hibernate.reactive.logging.impl.Log;
19+
import org.hibernate.reactive.logging.impl.LoggerFactory;
20+
import org.hibernate.reactive.provider.Settings;
21+
import org.hibernate.reactive.vertx.VertxInstance;
22+
import org.hibernate.service.spi.Configurable;
23+
import org.hibernate.service.spi.ServiceRegistryAwareService;
24+
import org.hibernate.service.spi.ServiceRegistryImplementor;
25+
import org.hibernate.service.spi.Startable;
26+
import org.hibernate.service.spi.Stoppable;
27+
28+
import io.vertx.core.Future;
29+
import io.vertx.core.Vertx;
30+
import io.vertx.jdbcclient.JDBCConnectOptions;
31+
import io.vertx.jdbcclient.JDBCPool;
32+
import io.vertx.sqlclient.Pool;
33+
import io.vertx.sqlclient.PoolOptions;
34+
import io.vertx.sqlclient.SqlConnectOptions;
35+
36+
public class H2SqlClientPool extends SqlClientPool
37+
implements ServiceRegistryAwareService, Configurable, Stoppable, Startable {
38+
39+
private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() );
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+
@Override
65+
public void start() {
66+
if ( pools == null ) {
67+
pools = createPool( uri );
68+
}
69+
}
70+
71+
@Override
72+
public CompletionStage<Void> getCloseFuture() {
73+
return closeFuture.toCompletionStage();
74+
}
75+
76+
@Override
77+
protected Pool getPool() {
78+
return pools;
79+
}
80+
81+
private Pool createPool(URI uri) {
82+
SqlClientPoolConfiguration configuration = serviceRegistry.getService( SqlClientPoolConfiguration.class );
83+
VertxInstance vertx = serviceRegistry.getService( VertxInstance.class );
84+
return createPool( uri, configuration.connectOptions( uri ), configuration.poolOptions(), vertx.getVertx() );
85+
}
86+
87+
private Pool createPool(URI uri, SqlConnectOptions connectOptions, PoolOptions poolOptions, Vertx vertx) {
88+
JDBCConnectOptions jdbcOptions = new JDBCConnectOptions();
89+
jdbcOptions.setUser( connectOptions.getUser() );
90+
jdbcOptions.setJdbcUrl( "jdbc:" + uri.toString() );
91+
JDBCPool pool = JDBCPool.pool( vertx, jdbcOptions, poolOptions );
92+
return pool;
93+
}
94+
95+
private URI jdbcUrl(Map<?, ?> configurationValues) {
96+
String url = ConfigurationHelper.getString( Settings.URL, configurationValues );
97+
LOG.sqlClientUrl( url );
98+
return parse( url );
99+
}
100+
101+
@Override
102+
public void stop() {
103+
if ( pools != null ) {
104+
this.closeFuture = pools.close();
105+
}
106+
}
107+
108+
public static URI parse(String url) {
109+
110+
if ( url == null || url.trim().isEmpty() ) {
111+
throw new HibernateError(
112+
"The configuration property '" + Settings.URL + "' was not provided, or is in invalid format. This is required when using the default DefaultSqlClientPool: " +
113+
"either provide the configuration setting or integrate with a different SqlClientPool implementation" );
114+
}
115+
116+
if ( url.startsWith( "jdbc:" ) ) {
117+
return URI.create( url.substring( 5 ) );
118+
}
119+
120+
return URI.create( url );
121+
}
122+
123+
@Override
124+
protected SqlStatementLogger getSqlStatementLogger() {
125+
return sqlStatementLogger;
126+
}
127+
}

Diff for: hibernate-reactive-core/src/main/java/org/hibernate/reactive/provider/service/NoJdbcEnvironmentInitiator.java

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.hibernate.dialect.CockroachDB201Dialect;
1010
import org.hibernate.dialect.DB297Dialect;
1111
import org.hibernate.dialect.Dialect;
12+
import org.hibernate.dialect.H2Dialect;
1213
import org.hibernate.dialect.MariaDB103Dialect;
1314
import org.hibernate.dialect.MySQL8Dialect;
1415
import org.hibernate.dialect.Oracle12cDialect;
@@ -147,6 +148,9 @@ else if ( url.startsWith( "sqlserver:" ) ) {
147148
else if ( url.startsWith( "oracle:" ) ) {
148149
return Oracle12cDialect.class;
149150
}
151+
else if ( url.startsWith( "h2:" ) ) {
152+
return H2Dialect.class;
153+
}
150154
else {
151155
return null;
152156
}

0 commit comments

Comments
 (0)