Skip to content

Commit 4545013

Browse files
SanneDavideD
authored andcommitted
Ensure MutationExecutor triggers autoflushing before running the mutation query
1 parent 84ded19 commit 4545013

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/sql/exec/internal/StandardReactiveJdbcMutationExecutor.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1818
import org.hibernate.query.spi.QueryOptions;
1919
import org.hibernate.reactive.adaptor.impl.PreparedStatementAdaptor;
20+
import org.hibernate.reactive.engine.spi.ReactiveSharedSessionContractImplementor;
2021
import org.hibernate.reactive.logging.impl.Log;
2122
import org.hibernate.reactive.logging.impl.LoggerFactory;
2223
import org.hibernate.reactive.pool.ReactiveConnection;
@@ -48,30 +49,34 @@ public CompletionStage<Integer> executeReactive(
4849
Function<String, PreparedStatement> statementCreator,
4950
BiConsumer<Integer, PreparedStatement> expectationCheck,
5051
ExecutionContext executionContext) {
51-
final SharedSessionContractImplementor session = executionContext.getSession();
52-
session.autoFlushIfRequired( jdbcMutation.getAffectedTableNames() );
53-
54-
final LogicalConnectionImplementor logicalConnection = session
55-
.getJdbcCoordinator()
56-
.getLogicalConnection();
57-
58-
final JdbcServices jdbcServices = session.getJdbcServices();
59-
final QueryOptions queryOptions = executionContext.getQueryOptions();
60-
final String finalSql = finalSql( jdbcMutation, executionContext, jdbcServices, queryOptions );
61-
62-
Object[] parameters = PreparedStatementAdaptor
63-
.bind( statement -> prepareStatement( jdbcMutation, statement, jdbcParameterBindings, executionContext ) );
64-
65-
session.getEventListenerManager().jdbcExecuteStatementStart();
66-
return connection( executionContext )
67-
.update( finalSql, parameters )
68-
.thenApply( result -> {
69-
// FIXME: I don't have a preparedStatement
70-
// expectationCheck.accept( result, preparedStatement );
71-
return result;
72-
} )
73-
.whenComplete( (result, t) -> session.getEventListenerManager().jdbcExecuteStatementEnd() )
74-
.whenComplete( (result, t) -> executionContext.afterStatement( logicalConnection ) );
52+
SharedSessionContractImplementor session = executionContext.getSession();
53+
ReactiveSharedSessionContractImplementor reactiveSession = (ReactiveSharedSessionContractImplementor) session;
54+
55+
return reactiveSession.reactiveAutoFlushIfRequired( jdbcMutation.getAffectedTableNames() )
56+
.thenCompose( v -> {
57+
58+
final LogicalConnectionImplementor logicalConnection = session
59+
.getJdbcCoordinator()
60+
.getLogicalConnection();
61+
62+
final JdbcServices jdbcServices = session.getJdbcServices();
63+
final QueryOptions queryOptions = executionContext.getQueryOptions();
64+
final String finalSql = finalSql( jdbcMutation, executionContext, jdbcServices, queryOptions );
65+
66+
Object[] parameters = PreparedStatementAdaptor
67+
.bind( statement -> prepareStatement( jdbcMutation, statement, jdbcParameterBindings, executionContext ) );
68+
69+
session.getEventListenerManager().jdbcExecuteStatementStart();
70+
return connection( executionContext )
71+
.update( finalSql, parameters )
72+
.thenApply( result -> {
73+
// FIXME: I don't have a preparedStatement
74+
// expectationCheck.accept( result, preparedStatement );
75+
return result;
76+
} )
77+
.whenComplete( (result, t) -> session.getEventListenerManager().jdbcExecuteStatementEnd() )
78+
.whenComplete( (result, t) -> executionContext.afterStatement( logicalConnection ) );
79+
} );
7580
}
7681

7782
private void prepareStatement(

0 commit comments

Comments
 (0)