@@ -184,79 +184,69 @@ void testClosePropagation(SessionFactoryScope factoryScope) {
184
184
*/
185
185
@ Test
186
186
void testAutoFlushStatefulChild (SessionFactoryScope factoryScope ) {
187
- final var sqlCollector = factoryScope .getCollectingStatementInspector ();
188
- sqlCollector .clear ();
189
-
190
- factoryScope .inTransaction ( (parentSession ) -> {
191
- try (SessionImplementor childSession = (SessionImplementor ) parentSession
192
- .sessionWithOptions ()
193
- .connection ()
194
- .openSession ()) {
195
- // persist an entity through the child session -
196
- // should be auto flushed (technically as part of the try-with-resources close of the child session)
197
- childSession .persist ( new Something ( 1 , "first" ) );
198
- }
199
- } );
200
-
201
- // make sure the flush and insert happened
202
- assertThat ( sqlCollector .getSqlQueries () ).hasSize ( 1 );
203
- factoryScope .inTransaction ( (session ) -> {
204
- final Something created = session .find ( Something .class , 1 );
205
- assertThat ( created ).isNotNull ();
206
- } );
207
-
208
- }
187
+ final MutableObject <SharedSessionContractImplementor > parentSessionRef = new MutableObject <>();
188
+ final MutableObject <SharedSessionContractImplementor > childSessionRef = new MutableObject <>();
209
189
210
- /**
211
- * NOTE: builds on assertions from {@link #testConnectionAndTransactionSharing}
212
- * and {@linkplain #testClosePropagation}
213
- */
214
- @ Test
215
- void testAutoFlushStatefulChildNoClose (SessionFactoryScope factoryScope ) {
216
190
final var sqlCollector = factoryScope .getCollectingStatementInspector ();
217
191
sqlCollector .clear ();
218
192
219
193
factoryScope .inTransaction ( (parentSession ) -> {
220
- SessionImplementor childSession = (SessionImplementor ) parentSession
194
+ parentSessionRef .set ( parentSession );
195
+
196
+ // IMPORTANT: it is important that the child session not be closed here (e.g. try-with-resources).
197
+ final SessionImplementor childSession = (SessionImplementor ) parentSession
221
198
.sessionWithOptions ()
222
199
.connection ()
223
200
.openSession ();
201
+ childSessionRef .set ( childSession );
224
202
225
- // persist an entity through the shared/child session.
226
- // then make sure the auto-flush of the parent session
227
- // propagates to the shared/child
203
+ // persist an entity through the child session - should be auto flushed as part of the
204
+ // parent session's flush cycle
228
205
childSession .persist ( new Something ( 1 , "first" ) );
229
206
} );
230
207
208
+ assertThat ( parentSessionRef .get ().isClosed () ).isTrue ();
209
+ assertThat ( childSessionRef .get ().isClosed () ).isTrue ();
210
+
231
211
// make sure the flush and insert happened
232
212
assertThat ( sqlCollector .getSqlQueries () ).hasSize ( 1 );
233
213
factoryScope .inTransaction ( (session ) -> {
234
214
final Something created = session .find ( Something .class , 1 );
235
215
assertThat ( created ).isNotNull ();
236
216
} );
237
- }
238
217
218
+ }
239
219
240
220
241
221
/**
242
222
* NOTE: builds on assertions from {@link #testConnectionAndTransactionSharing}
243
223
*/
244
224
@ Test
245
225
void testAutoFlushStatelessChild (SessionFactoryScope factoryScope ) {
226
+ final MutableObject <SharedSessionContractImplementor > parentSessionRef = new MutableObject <>();
227
+ final MutableObject <SharedSessionContractImplementor > childSessionRef = new MutableObject <>();
228
+
246
229
final var sqlCollector = factoryScope .getCollectingStatementInspector ();
247
230
sqlCollector .clear ();
248
231
249
232
factoryScope .inStatelessTransaction ( (parentSession ) -> {
250
- try (SessionImplementor childSession = (SessionImplementor ) parentSession
233
+ parentSessionRef .set ( parentSession );
234
+
235
+ // IMPORTANT: it is important that the child session not be closed here (e.g. try-with-resources).
236
+ final var childSession = (SessionImplementor ) parentSession
251
237
.sessionWithOptions ()
252
238
.connection ()
253
- .openSession ()) {
254
- // persist an entity through the child session -
255
- // should be auto flushed (technically as part of the try-with-resources close of the child session)
256
- childSession .persist ( new Something ( 1 , "first" ) );
257
- }
239
+ .openSession ();
240
+ childSessionRef .set ( childSession );
241
+
242
+ // persist an entity through the child session - should be auto flushed as part of the
243
+ // parent session's flush cycle
244
+ childSession .persist ( new Something ( 1 , "first" ) );
258
245
} );
259
246
247
+ assertThat ( parentSessionRef .get ().isClosed () ).isTrue ();
248
+ assertThat ( childSessionRef .get ().isClosed () ).isTrue ();
249
+
260
250
// make sure the flush and insert happened
261
251
assertThat ( sqlCollector .getSqlQueries () ).hasSize ( 1 );
262
252
factoryScope .inTransaction ( (session ) -> {
0 commit comments