Skip to content

Commit 4c11a7f

Browse files
committed
[hibernate#929] minor fixes
1 parent 9bc6b57 commit 4c11a7f

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,28 @@ public PoolOptions poolOptions() {
9393
public SqlConnectOptions connectOptions(URI uri) {
9494
String scheme = uri.getScheme();
9595
if( scheme.equalsIgnoreCase( "h2" )) {
96-
return new SqlConnectOptions()
96+
// H2 separates parameters in the url with a semicolon (';')
97+
// "jdbc:h2:~/test;DATABASE_TO_UPPER=FALSE";
98+
SqlConnectOptions options = new SqlConnectOptions()
9799
// username
98100
.setUser("sa")
99101
// password
100102
.setPassword("");
103+
int index = uri.toString().indexOf( ';' );
104+
if( index > -1 ) {
105+
String query = uri.toString().substring( index+1 );
106+
String[] params = query.split( ";" );
107+
for(String param : params) {
108+
final int position = param.indexOf( "=" );
109+
if ( position != -1 ) {
110+
// We assume the first '=' is the one separating key and value
111+
String key = param.substring( 0, position );
112+
String value = param.substring( position + 1 );
113+
options.addProperty( key, value );
114+
}
115+
}
116+
}
117+
return options;
101118
}
102119
String path = scheme.equals( "oracle" )
103120
? oraclePath( uri )

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.Map;
1212
import java.util.concurrent.CompletionStage;
1313

14-
import org.hibernate.HibernateError;
1514
import org.hibernate.engine.jdbc.spi.JdbcServices;
1615
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
1716
import org.hibernate.internal.util.config.ConfigurationHelper;
@@ -38,6 +37,8 @@ public class H2SqlClientPool extends SqlClientPool
3837

3938
private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() );
4039

40+
private static final String DEFAULT_URL = "jdbc:h2:~/test;DATABASE_TO_UPPER=FALSE";
41+
4142
//Asynchronous shutdown promise: we can't return it from #close as we implement a
4243
//blocking interface.
4344
private volatile Future<Void> closeFuture = Future.succeededFuture();
@@ -108,9 +109,7 @@ public void stop() {
108109
public static URI parse(String url) {
109110

110111
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" );
112+
return URI.create( DEFAULT_URL );
114113
}
115114

116115
if ( url.startsWith( "jdbc:" ) ) {

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public String getUri() {
8787

8888
@Override
8989
public String getScheme() {
90-
return "h2";
90+
return "h2:";
9191
}
9292

9393
@Override
@@ -104,7 +104,31 @@ public String getExpectedNativeDatatype(Class<?> dataType) {
104104

105105
@Override
106106
public String createJdbcUrl(String host, int port, String database, Map<String, String> params) {
107-
return getRegularJdbcUrl();
107+
// Primary mode for H2 is embedded which uses the URL format: "jdbc:h2:~/test"
108+
// H2 can also be configured as a remote server.
109+
// EXAMPLE 1: jdbc:h2:tcp://localhost/D:/myproject/data/project-name
110+
// EXAMPLE 2: jdbc:h2:tcp://localhost/~/test
111+
// EXAMpLE 3: jdbc:h2:tcp://localhost:9081/~/test
112+
final StringBuilder paramsBuilder = new StringBuilder();
113+
if ( params != null && !params.isEmpty() ) {
114+
params.forEach( (key, value) -> {
115+
paramsBuilder.append( jdbcParamDelimiter() );
116+
paramsBuilder.append( key );
117+
paramsBuilder.append( "=" );
118+
paramsBuilder.append( value );
119+
} );
120+
}
121+
String url = "jdbc:" + getScheme() + "//" + host;
122+
if ( port > -1 ) {
123+
url += ":" + port;
124+
}
125+
if ( paramsBuilder.length() > 0 ) {
126+
url += jdbcStartQuery() + paramsBuilder.substring( 1 );
127+
}
128+
if ( database != null ) {
129+
return url + ";database=" + database;
130+
}
131+
return url;
108132
}
109133

110134
@Override

0 commit comments

Comments
 (0)