You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by ma...@apache.org on 2026/10/04 06:16:18 UTC
(grails-core) 01/01: Apply the GORM session flush mode to the bound Hibernate 7 session
This is an automated email from the ASF dual-hosted git repository.
matrei pushed a commit to branch fix/hibernate7-session-flush-mode
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 36f00398bf6569fbfe846ebdaa30ca5e854d8b84
Author: Mattias Reichel <ma...@gmail.com>
AuthorDate: Sun Oct 4 08:11:14 2026 +0200
Apply the GORM session flush mode to the bound Hibernate 7 session
HibernateSession.setFlushMode changed the flush mode of the
GrailsHibernateTemplate that the HibernateDatastore shares, so a flush
mode set through one GORM session applied to template operations of
every session and thread, and stayed in place. getFlushMode reported
any mode but COMMIT as AUTO.
Both methods now use the Hibernate session bound to the current thread.
Without a bound session, getFlushMode returns the default flush mode of
the datastore and setFlushMode throws an IllegalStateException.
---
.../org/grails/orm/hibernate/HibernateSession.java | 38 +++++++++---
.../orm/hibernate/HibernateSessionSpec.groovy | 67 +++++++++++++++++++++-
2 files changed, 96 insertions(+), 9 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java
index c566c7392b..ac481a5180 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java
@@ -32,6 +32,8 @@ import java.util.Set;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType;
+import org.hibernate.FlushMode;
+import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
@@ -480,20 +482,42 @@ public class HibernateSession extends AbstractAttributeStoringSession implements
return (GrailsHibernateTemplate) getNativeInterface();
}
+ /**
+ * Returns the flush mode of the Hibernate session bound to the current thread, or the default flush mode of
+ * the datastore when no session is bound. {@link FlushMode#MANUAL} is returned as {@link FlushModeType#COMMIT}
+ * and {@link FlushMode#ALWAYS} as {@link FlushModeType#AUTO}.
+ */
@Override
public FlushModeType getFlushMode() {
- if (hibernateTemplate.getFlushMode() == GrailsHibernateTemplate.FLUSH_COMMIT) {
- return FlushModeType.COMMIT;
+ Session session = getCurrentNativeSession();
+ if (session != null) {
+ return session.getFlushMode();
}
- return FlushModeType.AUTO;
+ FlushMode defaultFlushMode = ((HibernateDatastore) getDatastore()).getDefaultFlushMode();
+ return defaultFlushMode == FlushMode.COMMIT || defaultFlushMode == FlushMode.MANUAL ?
+ FlushModeType.COMMIT : FlushModeType.AUTO;
}
+ /**
+ * Sets the flush mode of the Hibernate session bound to the current thread. Other sessions and the default
+ * flush mode of the datastore are not changed.
+ *
+ * @throws IllegalStateException if no Hibernate session is bound to the current thread
+ */
@Override
public void setFlushMode(FlushModeType flushMode) {
- if (flushMode == FlushModeType.AUTO) {
- hibernateTemplate.setFlushMode(GrailsHibernateTemplate.FLUSH_AUTO);
- } else if (flushMode == FlushModeType.COMMIT) {
- hibernateTemplate.setFlushMode(GrailsHibernateTemplate.FLUSH_COMMIT);
+ Session session = getCurrentNativeSession();
+ if (session == null) {
+ throw new IllegalStateException("No Hibernate session is bound to the current thread");
+ }
+ session.setFlushMode(flushMode);
+ }
+
+ private Session getCurrentNativeSession() {
+ try {
+ return hibernateTemplate.getSessionFactory().getCurrentSession();
+ } catch (HibernateException e) {
+ return null;
}
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/HibernateSessionSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/HibernateSessionSpec.groovy
index 9f696c5d30..369f28ad86 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/HibernateSessionSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/HibernateSessionSpec.groovy
@@ -23,6 +23,7 @@ import grails.gorm.annotation.Entity
import grails.gorm.hibernate.HibernateEntity
import grails.gorm.tests.HibernateGormDatastoreSpec
import jakarta.persistence.FlushModeType
+import org.hibernate.FlushMode
import org.grails.orm.hibernate.query.HibernateQuery
class HibernateSessionSpec extends HibernateGormDatastoreSpec {
@@ -113,21 +114,83 @@ class HibernateSessionSpec extends HibernateGormDatastoreSpec {
// Flush mode
// -------------------------------------------------------------------------
- void "getFlushMode and setFlushMode round-trip correctly"() {
+ void "setFlushMode sets the flush mode of the Hibernate session bound to the current thread"() {
given:
- def session = getSession()
+ var session = getSession()
+ var nativeSession = sessionFactory.currentSession
+ var originalMode = nativeSession.hibernateFlushMode
when:
session.setFlushMode(FlushModeType.AUTO)
then:
+ nativeSession.hibernateFlushMode == FlushMode.AUTO
session.getFlushMode() == FlushModeType.AUTO
when:
session.setFlushMode(FlushModeType.COMMIT)
then:
+ nativeSession.hibernateFlushMode == FlushMode.COMMIT
session.getFlushMode() == FlushModeType.COMMIT
+
+ cleanup:
+ nativeSession.hibernateFlushMode = originalMode
+ }
+
+ void "setFlushMode does not change the flush mode of the template the datastore shares"() {
+ given:
+ var nativeSession = sessionFactory.currentSession
+ var originalMode = nativeSession.hibernateFlushMode
+ var templateMode = datastore.hibernateTemplate.flushMode
+
+ when:
+ getSession().setFlushMode(FlushModeType.AUTO)
+
+ then:
+ datastore.hibernateTemplate.flushMode == templateMode
+
+ cleanup:
+ nativeSession.hibernateFlushMode = originalMode
+ }
+
+ void "getFlushMode returns #expected when the bound Hibernate session uses #nativeMode"() {
+ given:
+ var nativeSession = sessionFactory.currentSession
+ var originalMode = nativeSession.hibernateFlushMode
+ nativeSession.hibernateFlushMode = nativeMode
+
+ expect:
+ getSession().getFlushMode() == expected
+
+ cleanup:
+ nativeSession.hibernateFlushMode = originalMode
+
+ where:
+ nativeMode || expected
+ FlushMode.AUTO || FlushModeType.AUTO
+ FlushMode.ALWAYS || FlushModeType.AUTO
+ FlushMode.COMMIT || FlushModeType.COMMIT
+ FlushMode.MANUAL || FlushModeType.COMMIT
+ }
+
+ void "without a bound Hibernate session getFlushMode returns the datastore default and setFlushMode fails"() {
+ when: 'the session is used on a thread without a bound Hibernate session'
+ FlushModeType mode = null
+ Throwable failure = null
+ Thread.start {
+ var session = datastore.currentSession
+ mode = session.flushMode
+ try {
+ session.flushMode = FlushModeType.AUTO
+ } catch (Throwable e) {
+ failure = e
+ }
+ }.join()
+
+ then: 'the default flush mode of the datastore is COMMIT'
+ mode == FlushModeType.COMMIT
+ failure instanceof IllegalStateException
}
// -------------------------------------------------------------------------