forked from hibernate/hibernate-reactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hibernate#929] Draft - Initial changes for H2 db support
- Loading branch information
Showing
7 changed files
with
210 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/H2SqlClientPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.pool.impl; | ||
|
||
|
||
import java.net.URI; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import org.hibernate.engine.jdbc.spi.JdbcServices; | ||
import org.hibernate.engine.jdbc.spi.SqlStatementLogger; | ||
import org.hibernate.reactive.pool.ReactiveConnection; | ||
import org.hibernate.reactive.vertx.VertxInstance; | ||
import org.hibernate.service.spi.ServiceRegistryAwareService; | ||
import org.hibernate.service.spi.ServiceRegistryImplementor; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.jdbcclient.JDBCConnectOptions; | ||
import io.vertx.jdbcclient.JDBCPool; | ||
import io.vertx.sqlclient.Pool; | ||
import io.vertx.sqlclient.PoolOptions; | ||
import io.vertx.sqlclient.SqlConnectOptions; | ||
import io.vertx.sqlclient.SqlConnection; | ||
|
||
public class H2SqlClientPool extends SqlClientPool implements ServiceRegistryAwareService { | ||
|
||
//Asynchronous shutdown promise: we can't return it from #close as we implement a | ||
//blocking interface. | ||
private volatile Future<Void> closeFuture = Future.succeededFuture(); | ||
|
||
private Pool pools; | ||
private URI uri; | ||
private SqlStatementLogger sqlStatementLogger; | ||
private ServiceRegistryImplementor serviceRegistry; | ||
|
||
public H2SqlClientPool() { | ||
} | ||
|
||
@Override | ||
public void injectServices(ServiceRegistryImplementor serviceRegistry) { | ||
this.serviceRegistry = serviceRegistry; | ||
sqlStatementLogger = serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger(); | ||
} | ||
|
||
public void start() { | ||
if ( pools == null ) { | ||
pools = createPool( uri ); | ||
} | ||
} | ||
|
||
public void stop() { | ||
if ( pools != null ) { | ||
this.closeFuture = pools.close(); | ||
} | ||
} | ||
|
||
private Pool createPool(URI uri) { | ||
SqlClientPoolConfiguration configuration = serviceRegistry.getService( SqlClientPoolConfiguration.class ); | ||
VertxInstance vertx = serviceRegistry.getService( VertxInstance.class ); | ||
|
||
return createPool( uri, configuration.connectOptions( uri ), configuration.poolOptions(), vertx.getVertx() ); | ||
} | ||
|
||
private Pool createPool(URI uri, SqlConnectOptions connectOptions, PoolOptions poolOptions, Vertx vertx) { | ||
JDBCConnectOptions jdbcOptions = new JDBCConnectOptions(); | ||
jdbcOptions.setUser( connectOptions.getUser() ); | ||
jdbcOptions.setJdbcUrl( "jdbc:" + uri.toString() ); | ||
JDBCPool pool = JDBCPool.pool( vertx, jdbcOptions, poolOptions ); | ||
|
||
return pool; | ||
} | ||
|
||
@Override | ||
protected Pool getPool() { | ||
return pools; | ||
} | ||
|
||
@Override | ||
protected SqlStatementLogger getSqlStatementLogger() { | ||
return sqlStatementLogger; | ||
} | ||
|
||
@Override | ||
public CompletionStage<Void> getCloseFuture() { | ||
return closeFuture.toCompletionStage(); | ||
} | ||
|
||
@Override | ||
public CompletionStage<ReactiveConnection> getConnection() { | ||
return getConnectionFromPool( getPool() ); | ||
} | ||
|
||
@Override | ||
public CompletionStage<ReactiveConnection> getConnection(String tenantId) { | ||
return getConnectionFromPool( getTenantPool( tenantId ) ); | ||
} | ||
|
||
private CompletionStage<ReactiveConnection> getConnectionFromPool(Pool pool) { | ||
start(); | ||
return pool.getConnection().toCompletionStage().thenApply( this::newConnection ); | ||
} | ||
|
||
private SqlClientConnection newConnection(SqlConnection connection) { | ||
return new SqlClientConnection( connection, getPool(), getSqlStatementLogger() ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers/H2Database.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.containers; | ||
|
||
|
||
import java.util.Map; | ||
|
||
|
||
public class H2Database implements TestableDatabase { | ||
public static H2Database INSTANCE = new H2Database(); | ||
|
||
private String getRegularJdbcUrl() { | ||
return "jdbc:h2:~/test"; | ||
} | ||
|
||
@Override | ||
public String getJdbcUrl() { | ||
return getRegularJdbcUrl(); | ||
} | ||
|
||
@Override | ||
public String getUri() { | ||
{ | ||
return "h2:~/test"; | ||
} | ||
} | ||
|
||
@Override | ||
public String getScheme() { | ||
return "h2"; | ||
} | ||
|
||
@Override | ||
public String getNativeDatatypeQuery(String tableName, String columnName) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public String getExpectedNativeDatatype(Class<?> dataType) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String createJdbcUrl(String host, int port, String database, Map<String, String> params) { | ||
return getRegularJdbcUrl(); | ||
} | ||
|
||
@Override | ||
public String jdbcStartQuery() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public String jdbcParamDelimiter() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
private H2Database() { | ||
} | ||
} |
1e04a1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes get us close and it's currently failing in registering services. The
H2SqlClientPool
is being added as a service, but something is still wrong/missing.