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:13 UTC

(grails-core) 01/01: Give Hibernate 5 templates the configured flush mode

This is an automated email from the ASF dual-hosted git repository.

matrei pushed a commit to branch fix/hibernate-template-flush-mode
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit da5527951a4e183a840ac847014f93cb6373e5a4
Author: Mattias Reichel <ma...@gmail.com>
AuthorDate: Sun Oct 4 08:14:52 2026 +0200

    Give Hibernate 5 templates the configured flush mode
    
    AbstractHibernateDatastore keeps its default flush mode as
    FlushMode.level (MANUAL 0, COMMIT 5, AUTO 10, ALWAYS 20) and passed
    that value to every GrailsHibernateTemplate created for it, whose flush
    mode is one of its FLUSH_* constants (0 to 4). Only MANUAL matched, so
    template operations ignored a configured COMMIT, AUTO or ALWAYS, and
    HibernateSession.getFlushMode() returned AUTO for COMMIT.
    
    The templates now get the matching FLUSH_* constant, as on Hibernate 7.
---
 .../orm/hibernate/AbstractHibernateDatastore.java  |  7 +-
 .../orm/hibernate/GrailsHibernateTemplate.java     | 17 ++++-
 .../GrailsHibernateTemplateFlushModeSpec.groovy    | 89 ++++++++++++++++++++++
 3 files changed, 110 insertions(+), 3 deletions(-)

diff --git a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java
index 0d62627c39..242c74c1e6 100644
--- a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java
+++ b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java
@@ -354,13 +354,16 @@ public abstract class AbstractHibernateDatastore extends AbstractDatastore imple
     /**
      * Obtains a hibernate template for the given flush mode
      *
-     * @param flushMode The flush mode
+     * @param flushMode The flush mode, one of the {@code FLUSH_*} constants of {@link GrailsHibernateTemplate}
      * @return The IHibernateTemplate
      */
     public abstract IHibernateTemplate getHibernateTemplate(int flushMode);
 
+    /**
+     * @return A hibernate template that uses the default flush mode of this datastore
+     */
     public IHibernateTemplate getHibernateTemplate() {
-        return getHibernateTemplate(defaultFlushMode);
+        return getHibernateTemplate(GrailsHibernateTemplate.flushModeNameToConstant(defaultFlushModeName));
     }
 
     /**
diff --git a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/GrailsHibernateTemplate.java b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/GrailsHibernateTemplate.java
index 84366ff921..aebbd9e2aa 100644
--- a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/GrailsHibernateTemplate.java
+++ b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/GrailsHibernateTemplate.java
@@ -115,7 +115,22 @@ public class GrailsHibernateTemplate implements IHibernateTemplate {
     }
 
     public GrailsHibernateTemplate(SessionFactory sessionFactory, HibernateDatastore datastore) {
-        this(sessionFactory, datastore, datastore.getDefaultFlushMode());
+        this(sessionFactory, datastore, flushModeNameToConstant(datastore.getDefaultFlushModeName()));
+    }
+
+    /**
+     * Maps the name of a Hibernate {@link FlushMode} to one of the {@code FLUSH_*} constants of this class.
+     *
+     * @param flushModeName the name of the flush mode, such as {@link AbstractHibernateDatastore#getDefaultFlushModeName()}
+     * @return the {@code FLUSH_*} constant
+     */
+    static int flushModeNameToConstant(String flushModeName) {
+        return switch (FlushMode.valueOf(flushModeName)) {
+            case MANUAL -> FLUSH_NEVER;
+            case COMMIT -> FLUSH_COMMIT;
+            case ALWAYS -> FLUSH_ALWAYS;
+            default -> FLUSH_AUTO;
+        };
     }
 
     public GrailsHibernateTemplate(SessionFactory sessionFactory, HibernateDatastore datastore, int defaultFlushMode) {
diff --git a/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/GrailsHibernateTemplateFlushModeSpec.groovy b/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/GrailsHibernateTemplateFlushModeSpec.groovy
new file mode 100644
index 0000000000..186dce1cc9
--- /dev/null
+++ b/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/GrailsHibernateTemplateFlushModeSpec.groovy
@@ -0,0 +1,89 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.orm.hibernate
+
+import jakarta.persistence.FlushModeType
+import org.hibernate.FlushMode
+import org.hibernate.Session
+import spock.lang.AutoCleanup
+import spock.lang.Specification
+
+import org.grails.orm.hibernate.cfg.Settings
+
+class GrailsHibernateTemplateFlushModeSpec extends Specification {
+
+    @AutoCleanup
+    HibernateDatastore datastore
+
+    void "templates use the configured flush mode #flushMode"() {
+        given:
+        datastore = createDatastore(flushMode)
+
+        expect:
+        datastore.hibernateTemplate.flushMode == expected
+        new GrailsHibernateTemplate(datastore.sessionFactory, datastore).flushMode == expected
+
+        where:
+        flushMode || expected
+        'MANUAL'  || GrailsHibernateTemplate.FLUSH_NEVER
+        'COMMIT'  || GrailsHibernateTemplate.FLUSH_COMMIT
+        'AUTO'    || GrailsHibernateTemplate.FLUSH_AUTO
+        'ALWAYS'  || GrailsHibernateTemplate.FLUSH_ALWAYS
+    }
+
+    void "a template operation in a transaction runs with the configured flush mode #flushMode"() {
+        given:
+        datastore = createDatastore(flushMode)
+
+        when: 'the session of the transaction uses AUTO when a template operation runs'
+        List<FlushMode> modes = Book.withTransaction {
+            Session session = datastore.sessionFactory.currentSession
+            session.hibernateFlushMode = FlushMode.AUTO
+            FlushMode during = datastore.hibernateTemplate.execute { Session s -> s.hibernateFlushMode }
+            [during, session.hibernateFlushMode]
+        }
+
+        then: 'the operation runs with the configured flush mode, and the session gets its flush mode back afterwards'
+        modes == [expected, FlushMode.AUTO]
+
+        where:
+        flushMode || expected
+        'MANUAL'  || FlushMode.MANUAL
+        'COMMIT'  || FlushMode.COMMIT
+        'AUTO'    || FlushMode.AUTO
+        'ALWAYS'  || FlushMode.ALWAYS
+    }
+
+    void "the session of the datastore reports the configured flush mode #flushMode"() {
+        given:
+        datastore = createDatastore(flushMode)
+
+        expect:
+        datastore.currentSession.flushMode == expected
+
+        where:
+        flushMode || expected
+        'COMMIT'  || FlushModeType.COMMIT
+        'AUTO'    || FlushModeType.AUTO
+    }
+
+    private static HibernateDatastore createDatastore(String flushMode) {
+        new HibernateDatastore([(Settings.SETTING_DB_CREATE): 'create-drop', 'hibernate.flush.mode': flushMode], Book)
+    }
+}