Skip to content

warn people in javadoc that inSession() and friends don't flush! #10219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions hibernate-core/src/main/java/org/hibernate/SessionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,19 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser
StatelessSession openStatelessSession(Connection connection);

/**
* Open a {@link Session} and use it to perform an action.
* Open a {@link Session} and use it to perform the given action.
*
* @apiNote This method does not begin a transaction, and so
* the session is not automatically flushed before the method
* returns unless either:
* <ul>
* <li>the given action calls {@link Session#flush() flush()}
* explicitly, or
* <li>a transaction is initiated by the given action, using
* {@link Session#inTransaction}, for example.
* </ul>
*
* @see #inTransaction(Consumer)
*/
default void inSession(Consumer<? super Session> action) {
try ( Session session = openSession() ) {
Expand All @@ -236,7 +248,20 @@ default void inSession(Consumer<? super Session> action) {
}

/**
* Open a {@link StatelessSession} and use it to perform an action.
* Open a {@link StatelessSession} and use it to perform the
* given action.
*
* @apiNote This method does not begin a transaction, and so
* the session is not automatically flushed before the method
* returns unless either:
* <ul>
* <li>the given action calls {@link Session#flush() flush()}
* explicitly, or
* <li>a transaction is initiated by the given action, using
* {@link Session#inTransaction}, for example.
* </ul>
*
* @see #inStatelessTransaction(Consumer)
*
* @since 6.3
*/
Expand All @@ -247,7 +272,7 @@ default void inStatelessSession(Consumer<? super StatelessSession> action) {
}

/**
* Open a {@link Session} and use it to perform an action
* Open a {@link Session} and use it to perform the given action
* within the bounds of a transaction.
*
* @apiNote This method competes with the JPA-defined method
Expand All @@ -269,6 +294,18 @@ default void inStatelessTransaction(Consumer<? super StatelessSession> action) {

/**
* Open a {@link Session} and use it to obtain a value.
*
* @apiNote This method does not begin a transaction, and so
* the session is not automatically flushed before the method
* returns unless either:
* <ul>
* <li>the given action calls {@link Session#flush() flush()}
* explicitly, or
* <li>a transaction is initiated by the given action, using
* {@link Session#inTransaction}, for example.
* </ul>
*
* @see #fromTransaction(Function)
*/
default <R> R fromSession(Function<? super Session,R> action) {
try ( Session session = openSession() ) {
Expand All @@ -279,6 +316,18 @@ default <R> R fromSession(Function<? super Session,R> action) {
/**
* Open a {@link StatelessSession} and use it to obtain a value.
*
* @apiNote This method does not begin a transaction, and so
* the session is not automatically flushed before the method
* returns unless either:
* <ul>
* <li>the given action calls {@link Session#flush() flush()}
* explicitly, or
* <li>a transaction is initiated by the given action, using
* {@link Session#inTransaction}, for example.
* </ul>
*
* @see #fromStatelessTransaction(Function)
*
* @since 6.3
*/
default <R> R fromStatelessSession(Function<? super StatelessSession,R> action) {
Expand Down