24
24
import jakarta .persistence .EntityGraph ;
25
25
import jakarta .persistence .EntityManagerFactory ;
26
26
27
+ import static org .hibernate .internal .TransactionManagement .manageTransaction ;
28
+
27
29
/**
28
30
* A {@code SessionFactory} represents an "instance" of Hibernate: it maintains
29
31
* the runtime metamodel representing persistent entities, their attributes,
@@ -193,7 +195,16 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser
193
195
* Open a {@link Session} and use it to perform an action.
194
196
*/
195
197
default void inSession (Consumer <Session > action ) {
196
- try (Session session = openSession ()) {
198
+ try ( Session session = openSession () ) {
199
+ action .accept ( session );
200
+ }
201
+ }
202
+
203
+ /**
204
+ * Open a {@link StatelessSession} and use it to perform an action.
205
+ */
206
+ default void inStatelessSession (Consumer <StatelessSession > action ) {
207
+ try ( StatelessSession session = openStatelessSession () ) {
197
208
action .accept ( session );
198
209
}
199
210
}
@@ -203,86 +214,49 @@ default void inSession(Consumer<Session> action) {
203
214
* within the bounds of a transaction.
204
215
*/
205
216
default void inTransaction (Consumer <Session > action ) {
206
- inSession (
207
- session -> {
208
- final Transaction transaction = session .beginTransaction ();
209
- try {
210
- action .accept ( session );
211
- if ( !transaction .isActive () ) {
212
- throw new TransactionManagementException (
213
- "Execution of action caused managed transaction to be completed" );
214
- }
215
- }
216
- catch (RuntimeException exception ) {
217
- // an error happened in the action
218
- if ( transaction .isActive () ) {
219
- try {
220
- transaction .rollback ();
221
- }
222
- catch (Exception ignore ) {
223
- }
224
- }
225
-
226
- throw exception ;
227
- }
228
- // The action completed without throwing an exception,
229
- // so we attempt to commit the transaction, allowing
230
- // any RollbackException to propagate. Note that when
231
- // we get here we know that the transaction is active
232
- transaction .commit ();
233
- }
234
- );
217
+ inSession ( session -> manageTransaction ( session , session .beginTransaction (), action ) );
235
218
}
236
219
237
- class TransactionManagementException extends RuntimeException {
238
- TransactionManagementException (String message ) {
239
- super ( message );
240
- }
220
+ /**
221
+ * Open a {@link StatelessSession} and use it to perform an action
222
+ * within the bounds of a transaction.
223
+ */
224
+ default void inStatelessTransaction (Consumer <StatelessSession > action ) {
225
+ inStatelessSession ( session -> manageTransaction ( session , session .beginTransaction (), action ) );
241
226
}
242
227
243
228
/**
244
229
* Open a {@link Session} and use it to obtain a value.
245
230
*/
246
231
default <R > R fromSession (Function <Session ,R > action ) {
247
- try (Session session = openSession ()) {
232
+ try ( Session session = openSession () ) {
248
233
return action .apply ( session );
249
234
}
250
235
}
251
236
252
237
/**
253
- * Open a {@link Session} and use it to perform an action
238
+ * Open a {@link StatelessSession} and use it to obtain a value.
239
+ */
240
+ default <R > R fromStatelessSession (Function <StatelessSession ,R > action ) {
241
+ try ( StatelessSession session = openStatelessSession () ) {
242
+ return action .apply ( session );
243
+ }
244
+ }
245
+
246
+ /**
247
+ * Open a {@link Session} and use it to obtain a value
254
248
* within the bounds of a transaction.
255
249
*/
256
250
default <R > R fromTransaction (Function <Session ,R > action ) {
257
- return fromSession (
258
- session -> {
259
- final Transaction transaction = session .beginTransaction ();
260
- try {
261
- R result = action .apply ( session );
262
- if ( !transaction .isActive () ) {
263
- throw new TransactionManagementException (
264
- "Execution of action caused managed transaction to be completed" );
265
- }
266
- // The action completed without throwing an exception,
267
- // so we attempt to commit the transaction, allowing
268
- // any RollbackException to propagate. Note that when
269
- // we get here we know that the transaction is active
270
- transaction .commit ();
271
- return result ;
272
- }
273
- catch (RuntimeException exception ) {
274
- // an error happened in the action or during commit()
275
- if ( transaction .isActive () ) {
276
- try {
277
- transaction .rollback ();
278
- }
279
- catch (Exception ignore ) {
280
- }
281
- }
282
- throw exception ;
283
- }
284
- }
285
- );
251
+ return fromSession ( session -> manageTransaction ( session , session .beginTransaction (), action ) );
252
+ }
253
+
254
+ /**
255
+ * Open a {@link StatelessSession} and use it to obtain a value
256
+ * within the bounds of a transaction.
257
+ */
258
+ default <R > R fromStatelessTransaction (Function <StatelessSession ,R > action ) {
259
+ return fromStatelessSession ( session -> manageTransaction ( session , session .beginTransaction (), action ) );
286
260
}
287
261
288
262
/**
0 commit comments