34
34
import java .util .Calendar ;
35
35
import java .util .Map ;
36
36
import java .util .Properties ;
37
- import java .util .concurrent .CompletionStage ;
38
37
import java .util .concurrent .Executor ;
39
38
40
39
import org .hibernate .boot .model .relational .SqlStringGenerationContext ;
41
40
import org .hibernate .engine .jdbc .env .spi .JdbcEnvironment ;
42
- import org .hibernate .reactive .pool .ReactiveConnection ;
43
41
import org .hibernate .reactive .pool .ReactiveConnectionPool ;
44
42
import org .hibernate .reactive .pool .impl .Parameters ;
45
43
import org .hibernate .resource .transaction .spi .DdlTransactionIsolator ;
48
46
import org .hibernate .tool .schema .internal .exec .JdbcContext ;
49
47
50
48
import static org .hibernate .reactive .util .impl .CompletionStages .logSqlException ;
51
- import static org .hibernate .reactive .util .impl .CompletionStages .voidFuture ;
52
49
53
50
public class ReactiveImprovedExtractionContextImpl extends ImprovedExtractionContextImpl {
54
51
55
- private final ReactiveConnectionPool service ;
52
+ private final ReactiveConnectionPool connectionPool ;
56
53
57
54
public ReactiveImprovedExtractionContextImpl (
58
55
ServiceRegistry registry ,
@@ -65,54 +62,42 @@ public ReactiveImprovedExtractionContextImpl(
65
62
NoopDdlTransactionIsolator .INSTANCE ,
66
63
databaseObjectAccess
67
64
);
68
- service = registry .getService ( ReactiveConnectionPool .class );
65
+ connectionPool = registry .getService ( ReactiveConnectionPool .class );
69
66
}
70
67
71
68
@ Override
72
69
public <T > T getQueryResults (
73
70
String queryString ,
74
71
Object [] positionalParameters ,
75
72
ResultSetProcessor <T > resultSetProcessor ) throws SQLException {
76
-
77
- final CompletionStage <ReactiveConnection > connectionStage = service .getConnection ();
78
-
79
- try (final ResultSet resultSet = getQueryResultSet ( queryString , positionalParameters , connectionStage )) {
73
+ try (final ResultSet resultSet = getQueryResultSet ( queryString , positionalParameters )) {
80
74
return resultSetProcessor .process ( resultSet );
81
75
}
82
- finally {
83
- // This method doesn't return a reactive type, so we start closing the connection and ignore the result
84
- connectionStage
85
- .handle ( ReactiveImprovedExtractionContextImpl ::ignoreException )
86
- .thenCompose ( ReactiveImprovedExtractionContextImpl ::closeConnection );
87
-
88
- }
89
- }
90
-
91
- private static ReactiveConnection ignoreException (ReactiveConnection reactiveConnection , Throwable throwable ) {
92
- return reactiveConnection ;
93
- }
94
-
95
- private static CompletionStage <Void > closeConnection (ReactiveConnection connection ) {
96
- // Avoid NullPointerException if we couldn't create a connection
97
- return connection != null ? connection .close () : voidFuture ();
98
76
}
99
77
100
78
private ResultSet getQueryResultSet (
101
79
String queryString ,
102
- Object [] positionalParameters ,
103
- CompletionStage <ReactiveConnection > connectionStage ) {
80
+ Object [] positionalParameters ) {
104
81
final Object [] parametersToUse = positionalParameters != null ? positionalParameters : new Object [0 ];
105
- final Parameters parametersDialectSpecific = Parameters .instance (
106
- getJdbcEnvironment ().getDialect ()
107
- );
82
+ final Parameters parametersDialectSpecific = Parameters .instance ( getJdbcEnvironment ().getDialect () );
108
83
final String queryToUse = parametersDialectSpecific .process ( queryString , parametersToUse .length );
109
- return connectionStage .thenCompose ( c -> c .selectJdbcOutsideTransaction ( queryToUse , parametersToUse ) )
84
+ return connectionPool
85
+ // DDL needs to run outside the current transaction. For example:
86
+ // - increment on a table-based id generator should happen outside the current tx.
87
+ // - not all databases support transactional DDL
88
+ .selectJdbcOutsideTransaction ( queryToUse , parametersToUse )
110
89
.whenComplete ( (resultSet , err ) -> logSqlException ( err , () -> "could not execute query " , queryToUse ) )
111
- .thenApply (ResultSetWorkaround ::new )
90
+ .thenApply ( ResultSetWorkaround ::new )
91
+ // During schema migration, errors are ignored
92
+ .handle ( ReactiveImprovedExtractionContextImpl ::ignoreException )
112
93
.toCompletableFuture ()
113
94
.join ();
114
95
}
115
96
97
+ private static <T > T ignoreException (T result , Throwable throwable ) {
98
+ return result ;
99
+ }
100
+
116
101
private static class NoopDdlTransactionIsolator implements DdlTransactionIsolator {
117
102
static final NoopDdlTransactionIsolator INSTANCE = new NoopDdlTransactionIsolator ();
118
103
0 commit comments