Skip to content

Commit

Permalink
HHH-19171 add SharedSessionContract.inTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Feb 21, 2025
1 parent 0bc1dff commit 626db6d
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import java.io.Serializable;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

import jakarta.persistence.EntityGraph;
import org.hibernate.graph.RootGraph;
Expand All @@ -15,6 +17,8 @@
import org.hibernate.query.QueryProducer;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;

import static org.hibernate.internal.TransactionManagement.manageTransaction;

/**
* Declares operations that are common between {@link Session} and {@link StatelessSession}.
*
Expand Down Expand Up @@ -432,4 +436,31 @@ public interface SharedSessionContract extends QueryProducer, AutoCloseable, Ser
* The factory which created this session.
*/
SessionFactory getFactory();

/**
* Perform an action within the bounds of a {@linkplain Transaction
* transaction} associated with this session.
*
* @param action a void function which accepts the {@link Transaction}
*
* @since 7.0
*/
default void inTransaction(Consumer<? super Transaction> action) {
final Transaction transaction = beginTransaction();
manageTransaction( transaction, transaction, action );
}

/**
* Obtain a value within the bounds of a {@linkplain Transaction
* transaction} associated with this session.
*
* @param action a function which accepts the {@link Transaction} and
* returns the value
*
* @since 7.0
*/
default <R> R fromTransaction(Function<? super Transaction,R> action) {
final Transaction transaction = beginTransaction();
return manageTransaction( transaction, transaction, action );
}
}

0 comments on commit 626db6d

Please sign in to comment.