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

borinquenkid pushed a commit to branch 8.0.x-hibernate7
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit 2edded5c82d9327e747d92bffef3370b605470fd
Author: Walter Duque de Estrada <[email protected]>
AuthorDate: Tue Feb 24 04:12:39 2026 -0600

    GrailsSessionContext tested
---
 .../grails/orm/hibernate/GrailsSessionContext.java |  9 ++-
 .../sessioncontext/GrailsSessionContextSpec.groovy | 87 ++++++++++++++++++++++
 2 files changed, 93 insertions(+), 3 deletions(-)

diff --git 
a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsSessionContext.java
 
b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsSessionContext.java
index a89a66ab6d..77e2f10784 100644
--- 
a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsSessionContext.java
+++ 
b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsSessionContext.java
@@ -18,6 +18,8 @@
  */
 package org.grails.orm.hibernate;
 
+import java.io.Serial;
+
 import jakarta.transaction.Status;
 import jakarta.transaction.Transaction;
 import jakarta.transaction.TransactionManager;
@@ -48,6 +50,7 @@ import 
org.springframework.transaction.support.TransactionSynchronizationManager
  */
 public class GrailsSessionContext implements CurrentSessionContext {
 
+  @Serial
   private static final long serialVersionUID = 1;
 
   private static final Logger LOG = 
LoggerFactory.getLogger(GrailsSessionContext.class);
@@ -194,7 +197,7 @@ public class GrailsSessionContext implements 
CurrentSessionContext {
       }
       jtaTx.registerSynchronization(
           new SpringJtaSynchronizationAdapter(
-              createSpringSessionSynchronization(holderToUse), jtaTm));
+              createSpringSessionSynchronization(holderToUse)));
       holderToUse.setSynchronizedWithTransaction(true);
       if (holderToUse != sessionHolder) {
         TransactionSynchronizationManager.bindResource(sessionFactory, 
holderToUse);
@@ -207,8 +210,8 @@ public class GrailsSessionContext implements 
CurrentSessionContext {
 
   protected TransactionManager getJtaTransactionManager(Session session) {
     SessionFactoryImplementor sessionFactoryImpl = null;
-    if (sessionFactory instanceof SessionFactoryImplementor) {
-      sessionFactoryImpl = ((SessionFactoryImplementor) sessionFactory);
+    if (sessionFactory != null) {
+      sessionFactoryImpl = sessionFactory;
     } else if (session != null) {
       SessionFactory internalFactory = session.getSessionFactory();
       if (internalFactory instanceof SessionFactoryImplementor) {
diff --git 
a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/sessioncontext/GrailsSessionContextSpec.groovy
 
b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/sessioncontext/GrailsSessionContextSpec.groovy
new file mode 100644
index 0000000000..d9d1461d48
--- /dev/null
+++ 
b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/sessioncontext/GrailsSessionContextSpec.groovy
@@ -0,0 +1,87 @@
+package grails.gorm.specs.sessioncontext
+
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.grails.orm.hibernate.GrailsSessionContext
+import org.grails.orm.hibernate.HibernateDatastore
+import org.hibernate.FlushMode
+import org.hibernate.Session
+import org.hibernate.engine.spi.SessionFactoryImplementor
+import org.springframework.orm.hibernate5.SessionHolder
+import 
org.springframework.transaction.support.TransactionSynchronizationManager
+
+class GrailsSessionContextSpec extends HibernateGormDatastoreSpec {
+
+    void "test GrailsSessionContext can be created with a SessionFactory"() {
+        given:
+        HibernateDatastore hibernateDatastore = manager.hibernateDatastore
+        SessionFactoryImplementor sessionFactory = 
hibernateDatastore.sessionFactory as SessionFactoryImplementor
+
+        when:
+        GrailsSessionContext sessionContext = new 
GrailsSessionContext(sessionFactory)
+
+        then:
+        sessionContext != null
+    }
+
+    void "test currentSession() returns session bound via 
TransactionSynchronizationManager"() {
+        given:
+        HibernateDatastore hibernateDatastore = manager.hibernateDatastore
+        SessionFactoryImplementor sessionFactory = 
hibernateDatastore.sessionFactory as SessionFactoryImplementor
+        GrailsSessionContext sessionContext = new 
GrailsSessionContext(sessionFactory)
+        Session session = sessionFactory.openSession()
+        // unbind whatever the test framework bound, then bind our own session
+        
TransactionSynchronizationManager.unbindResourceIfPossible(sessionFactory)
+        TransactionSynchronizationManager.bindResource(sessionFactory, new 
SessionHolder(session))
+
+        when:
+        Session current = sessionContext.currentSession()
+
+        then:
+        current != null
+        current == session
+
+        cleanup:
+        
TransactionSynchronizationManager.unbindResourceIfPossible(sessionFactory)
+        if (session.isOpen()) session.close()
+    }
+
+    void "test currentSession() throws when no session is bound and 
allowCreate is false"() {
+        given:
+        HibernateDatastore hibernateDatastore = manager.hibernateDatastore
+        SessionFactoryImplementor sessionFactory = 
hibernateDatastore.sessionFactory as SessionFactoryImplementor
+        GrailsSessionContext sessionContext = new 
GrailsSessionContext(sessionFactory)
+        // unbind whatever the test framework bound
+        def saved = 
TransactionSynchronizationManager.unbindResourceIfPossible(sessionFactory)
+
+        when:
+        sessionContext.currentSession()
+
+        then:
+        thrown(org.hibernate.HibernateException)
+
+        cleanup:
+        // restore the original binding so the framework is not broken for 
subsequent tests
+        if (saved) 
TransactionSynchronizationManager.bindResource(sessionFactory, saved)
+    }
+
+    void "test currentSession() returns session when bound as plain Session 
resource"() {
+        given:
+        HibernateDatastore hibernateDatastore = manager.hibernateDatastore
+        SessionFactoryImplementor sessionFactory = 
hibernateDatastore.sessionFactory as SessionFactoryImplementor
+        GrailsSessionContext sessionContext = new 
GrailsSessionContext(sessionFactory)
+        Session session = sessionFactory.openSession()
+        
TransactionSynchronizationManager.unbindResourceIfPossible(sessionFactory)
+        TransactionSynchronizationManager.bindResource(sessionFactory, session)
+
+        when:
+        Session current = sessionContext.currentSession()
+
+        then:
+        current == session
+
+        cleanup:
+        
TransactionSynchronizationManager.unbindResourceIfPossible(sessionFactory)
+        if (session.isOpen()) session.close()
+    }
+}
+

Reply via email to