Skip to content

Commit

Permalink
[hibernate#929] minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
blafond committed Mar 25, 2022
1 parent 9bc6b57 commit 4c11a7f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,28 @@ public PoolOptions poolOptions() {
public SqlConnectOptions connectOptions(URI uri) {
String scheme = uri.getScheme();
if( scheme.equalsIgnoreCase( "h2" )) {
return new SqlConnectOptions()
// H2 separates parameters in the url with a semicolon (';')
// "jdbc:h2:~/test;DATABASE_TO_UPPER=FALSE";
SqlConnectOptions options = new SqlConnectOptions()
// username
.setUser("sa")
// password
.setPassword("");
int index = uri.toString().indexOf( ';' );
if( index > -1 ) {
String query = uri.toString().substring( index+1 );
String[] params = query.split( ";" );
for(String param : params) {
final int position = param.indexOf( "=" );
if ( position != -1 ) {
// We assume the first '=' is the one separating key and value
String key = param.substring( 0, position );
String value = param.substring( position + 1 );
options.addProperty( key, value );
}
}
}
return options;
}
String path = scheme.equals( "oracle" )
? oraclePath( uri )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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;
Expand All @@ -38,6 +37,8 @@ public class H2SqlClientPool extends SqlClientPool

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

private static final String DEFAULT_URL = "jdbc:h2:~/test;DATABASE_TO_UPPER=FALSE";

//Asynchronous shutdown promise: we can't return it from #close as we implement a
//blocking interface.
private volatile Future<Void> closeFuture = Future.succeededFuture();
Expand Down Expand Up @@ -108,9 +109,7 @@ public void stop() {
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" );
return URI.create( DEFAULT_URL );
}

if ( url.startsWith( "jdbc:" ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public String getUri() {

@Override
public String getScheme() {
return "h2";
return "h2:";
}

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

@Override
public String createJdbcUrl(String host, int port, String database, Map<String, String> params) {
return getRegularJdbcUrl();
// Primary mode for H2 is embedded which uses the URL format: "jdbc:h2:~/test"
// H2 can also be configured as a remote server.
// EXAMPLE 1: jdbc:h2:tcp://localhost/D:/myproject/data/project-name
// EXAMPLE 2: jdbc:h2:tcp://localhost/~/test
// EXAMpLE 3: jdbc:h2:tcp://localhost:9081/~/test
final StringBuilder paramsBuilder = new StringBuilder();
if ( params != null && !params.isEmpty() ) {
params.forEach( (key, value) -> {
paramsBuilder.append( jdbcParamDelimiter() );
paramsBuilder.append( key );
paramsBuilder.append( "=" );
paramsBuilder.append( value );
} );
}
String url = "jdbc:" + getScheme() + "//" + host;
if ( port > -1 ) {
url += ":" + port;
}
if ( paramsBuilder.length() > 0 ) {
url += jdbcStartQuery() + paramsBuilder.substring( 1 );
}
if ( database != null ) {
return url + ";database=" + database;
}
return url;
}

@Override
Expand Down

0 comments on commit 4c11a7f

Please sign in to comment.