Skip to content

Commit 626db6d

Browse files
committed
HHH-19171 add SharedSessionContract.inTransaction
1 parent 0bc1dff commit 626db6d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

hibernate-core/src/main/java/org/hibernate/SharedSessionContract.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import java.io.Serializable;
88
import java.util.List;
9+
import java.util.function.Consumer;
10+
import java.util.function.Function;
911

1012
import jakarta.persistence.EntityGraph;
1113
import org.hibernate.graph.RootGraph;
@@ -15,6 +17,8 @@
1517
import org.hibernate.query.QueryProducer;
1618
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
1719

20+
import static org.hibernate.internal.TransactionManagement.manageTransaction;
21+
1822
/**
1923
* Declares operations that are common between {@link Session} and {@link StatelessSession}.
2024
*
@@ -432,4 +436,31 @@ public interface SharedSessionContract extends QueryProducer, AutoCloseable, Ser
432436
* The factory which created this session.
433437
*/
434438
SessionFactory getFactory();
439+
440+
/**
441+
* Perform an action within the bounds of a {@linkplain Transaction
442+
* transaction} associated with this session.
443+
*
444+
* @param action a void function which accepts the {@link Transaction}
445+
*
446+
* @since 7.0
447+
*/
448+
default void inTransaction(Consumer<? super Transaction> action) {
449+
final Transaction transaction = beginTransaction();
450+
manageTransaction( transaction, transaction, action );
451+
}
452+
453+
/**
454+
* Obtain a value within the bounds of a {@linkplain Transaction
455+
* transaction} associated with this session.
456+
*
457+
* @param action a function which accepts the {@link Transaction} and
458+
* returns the value
459+
*
460+
* @since 7.0
461+
*/
462+
default <R> R fromTransaction(Function<? super Transaction,R> action) {
463+
final Transaction transaction = beginTransaction();
464+
return manageTransaction( transaction, transaction, action );
465+
}
435466
}

0 commit comments

Comments
 (0)