Skip to content

Commit 279d537

Browse files
committed
[hibernate#929] filled out H2 client pool
1 parent bd9aab9 commit 279d537

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ org.gradle.java.installations.auto-download=false
2626

2727
# The database type to use (key insensitive and support aliases):
2828
# Db2, MySql, PostgreSQL, CockroachDB, SqlServer
29-
#db = MSSQL
29+
db = H2
3030

3131
# Enable the SonatypeOS maven repository (mainly for Vert.x snapshots) when present (value ignored)
3232
#enableSonatypeOpenSourceSnapshotsRep = true

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/H2SqlClientPool.java

+46-13
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
package org.hibernate.reactive.pool.impl;
77

88

9+
import java.lang.invoke.MethodHandles;
910
import java.net.URI;
11+
import java.util.Map;
1012
import java.util.concurrent.CompletionStage;
1113

14+
import org.hibernate.HibernateError;
1215
import org.hibernate.engine.jdbc.spi.JdbcServices;
1316
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;
1420
import org.hibernate.reactive.pool.ReactiveConnection;
21+
import org.hibernate.reactive.provider.Settings;
1522
import org.hibernate.reactive.vertx.VertxInstance;
1623
import org.hibernate.service.spi.ServiceRegistryAwareService;
1724
import org.hibernate.service.spi.ServiceRegistryImplementor;
@@ -27,6 +34,8 @@
2734

2835
public class H2SqlClientPool extends SqlClientPool implements ServiceRegistryAwareService {
2936

37+
private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() );
38+
3039
//Asynchronous shutdown promise: we can't return it from #close as we implement a
3140
//blocking interface.
3241
private volatile Future<Void> closeFuture = Future.succeededFuture();
@@ -45,22 +54,29 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
4554
sqlStatementLogger = serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger();
4655
}
4756

57+
public void configure(Map configuration) {
58+
uri = jdbcUrl( configuration );
59+
}
60+
4861
public void start() {
4962
if ( pools == null ) {
5063
pools = createPool( uri );
5164
}
5265
}
5366

54-
public void stop() {
55-
if ( pools != null ) {
56-
this.closeFuture = pools.close();
57-
}
67+
@Override
68+
public CompletionStage<Void> getCloseFuture() {
69+
return closeFuture.toCompletionStage();
70+
}
71+
72+
@Override
73+
protected Pool getPool() {
74+
return pools;
5875
}
5976

6077
private Pool createPool(URI uri) {
6178
SqlClientPoolConfiguration configuration = serviceRegistry.getService( SqlClientPoolConfiguration.class );
6279
VertxInstance vertx = serviceRegistry.getService( VertxInstance.class );
63-
6480
return createPool( uri, configuration.connectOptions( uri ), configuration.poolOptions(), vertx.getVertx() );
6581
}
6682

@@ -73,19 +89,36 @@ private Pool createPool(URI uri, SqlConnectOptions connectOptions, PoolOptions p
7389
return pool;
7490
}
7591

76-
@Override
77-
protected Pool getPool() {
78-
return pools;
92+
private URI jdbcUrl(Map<?, ?> configurationValues) {
93+
String url = ConfigurationHelper.getString( Settings.URL, configurationValues );
94+
LOG.sqlClientUrl( url );
95+
return parse( url );
7996
}
8097

81-
@Override
82-
protected SqlStatementLogger getSqlStatementLogger() {
83-
return sqlStatementLogger;
98+
public void stop() {
99+
if ( pools != null ) {
100+
this.closeFuture = pools.close();
101+
}
102+
}
103+
104+
public static URI parse(String url) {
105+
106+
if ( url == null || url.trim().isEmpty() ) {
107+
throw new HibernateError(
108+
"The configuration property '" + Settings.URL + "' was not provided, or is in invalid format. This is required when using the default DefaultSqlClientPool: " +
109+
"either provide the configuration setting or integrate with a different SqlClientPool implementation" );
110+
}
111+
112+
if ( url.startsWith( "jdbc:" ) ) {
113+
return URI.create( url.substring( 5 ) );
114+
}
115+
116+
return URI.create( url );
84117
}
85118

86119
@Override
87-
public CompletionStage<Void> getCloseFuture() {
88-
return closeFuture.toCompletionStage();
120+
protected SqlStatementLogger getSqlStatementLogger() {
121+
return sqlStatementLogger;
89122
}
90123

91124
@Override

hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers/H2Database.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ private String getRegularJdbcUrl() {
1818

1919
@Override
2020
public String getJdbcUrl() {
21-
return getRegularJdbcUrl();
21+
return "jdbc:h2:~/test";
2222
}
2323

2424
@Override
2525
public String getUri() {
26-
{
27-
return "h2:~/test";
28-
}
26+
return "h2:~/test";
2927
}
3028

29+
3130
@Override
3231
public String getScheme() {
3332
return "h2";
@@ -50,12 +49,12 @@ public String createJdbcUrl(String host, int port, String database, Map<String,
5049

5150
@Override
5251
public String jdbcStartQuery() {
53-
throw new UnsupportedOperationException();
52+
return ";";
5453
}
5554

5655
@Override
5756
public String jdbcParamDelimiter() {
58-
throw new UnsupportedOperationException();
57+
return ";";
5958
}
6059

6160
private H2Database() {

0 commit comments

Comments
 (0)