Skip to content

Don't open multiple connections on schema update #1924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Properties;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;

import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
Expand All @@ -53,6 +54,8 @@
public class ReactiveImprovedExtractionContextImpl extends ImprovedExtractionContextImpl {

private final ReactiveConnectionPool service;
private final AtomicInteger level = new AtomicInteger( 0 );
private CompletionStage<ReactiveConnection> connectionStage;

public ReactiveImprovedExtractionContextImpl(
ServiceRegistry registry,
Expand All @@ -68,23 +71,35 @@ public ReactiveImprovedExtractionContextImpl(
service = registry.getService( ReactiveConnectionPool.class );
}

@Override
public <T> T getQueryResults(
String queryString,
Object[] positionalParameters,
ResultSetProcessor<T> resultSetProcessor) throws SQLException {
public ReactiveImprovedExtractionContextImpl push() {
int currentLevel = level.getAndIncrement();
if ( currentLevel == 0 ) {
connectionStage = service.getConnection();
}
return this;
}

final CompletionStage<ReactiveConnection> connectionStage = service.getConnection();
public ReactiveImprovedExtractionContextImpl pop() {
int currentLevel = level.decrementAndGet();
if ( currentLevel == 0 && connectionStage != null ) {
// This method doesn't return a reactive type, so we start closing the connection and ignore the result
connectionStage
.handle( ReactiveImprovedExtractionContextImpl::ignoreException )
.thenCompose( ReactiveImprovedExtractionContextImpl::closeConnection )
.toCompletableFuture()
.join();
}
return this;
}

@Override
public <T> T getQueryResults( String queryString, Object[] positionalParameters, ResultSetProcessor<T> resultSetProcessor) throws SQLException {
push();
try (final ResultSet resultSet = getQueryResultSet( queryString, positionalParameters, connectionStage )) {
return resultSetProcessor.process( resultSet );
}
finally {
// This method doesn't return a reactive type, so we start closing the connection and ignore the result
connectionStage
.handle( ReactiveImprovedExtractionContextImpl::ignoreException )
.thenCompose( ReactiveImprovedExtractionContextImpl::closeConnection );

pop();
}
}

Expand All @@ -102,13 +117,12 @@ private ResultSet getQueryResultSet(
Object[] positionalParameters,
CompletionStage<ReactiveConnection> connectionStage) {
final Object[] parametersToUse = positionalParameters != null ? positionalParameters : new Object[0];
final Parameters parametersDialectSpecific = Parameters.instance(
getJdbcEnvironment().getDialect()
);
final Parameters parametersDialectSpecific = Parameters.instance( getJdbcEnvironment().getDialect() );
final String queryToUse = parametersDialectSpecific.process( queryString, parametersToUse.length );
return connectionStage.thenCompose( c -> c.selectJdbcOutsideTransaction( queryToUse, parametersToUse ) )
return connectionStage
.thenCompose( c -> c.selectJdbcOutsideTransaction( queryToUse, parametersToUse ) )
.whenComplete( (resultSet, err) -> logSqlException( err, () -> "could not execute query ", queryToUse ) )
.thenApply(ResultSetWorkaround::new)
.thenApply( ResultSetWorkaround::new )
.toCompletableFuture()
.join();
}
Expand Down
4 changes: 4 additions & 0 deletions hibernate-reactive-core/src/test/resources/log4j2.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ logger.hibernate-dialect.level = debug
logger.hibernate.name = org.hibernate.SQL
logger.hibernate.level = info

# We want to log when a connection is opened/closed
logger.sql-connection.name = org.hibernate.reactive.pool.impl
logger.sql-connection.level = trace

# Setting level to TRACE will show parameters values
logger.sql-parameters-values.name = org.hibernate.type
logger.sql-parameters-values.level = info
Expand Down
Loading