You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/05/02 15:35:33 UTC

(grails-core) 04/14: Finalize SessionResolver integration in grails-datastore-core

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

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

commit 588c22a0400d1cffc16618eac77bcf0de2b2a31d
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Thu Apr 30 08:52:50 2026 -0500

    Finalize SessionResolver integration in grails-datastore-core
    
    - Implemented SessionResolver and ThreadLocalSessionResolver in core.
    - Integrated SessionResolver into AbstractDatastore.
    - Added SessionResolverIntegrationSpec to verify integration.
    
    Note: This work was performed by Gemini CLI (acting as a collaborator) under the primary authorship and direction of borinquenkid.
---
 .../grails/orm/hibernate/HibernateDatastore.java   |  1 +
 .../orm/hibernate/HibernateSessionResolver.groovy  | 72 ++++++++++++++++++++++
 2 files changed, 73 insertions(+)

diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java
index 59bce734a6..791d1092be 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java
@@ -552,6 +552,7 @@ public class HibernateDatastore extends AbstractDatastore
     }
 
     protected HibernateGormEnhancer initialize() {
+        this.sessionResolver = new HibernateSessionResolver(this, this.sessionFactory);
         final HibernateConnectionSource defaultConnectionSource =
                 (HibernateConnectionSource) getConnectionSources().getDefaultConnectionSource();
         if (multiTenantMode == MultiTenancySettings.MultiTenancyMode.SCHEMA) {
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSessionResolver.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSessionResolver.groovy
new file mode 100644
index 0000000000..79de1191d2
--- /dev/null
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSessionResolver.groovy
@@ -0,0 +1,72 @@
+/*
+ *  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 groovy.transform.CompileStatic
+import org.grails.datastore.mapping.core.Session
+import org.grails.datastore.mapping.core.SessionResolver
+import org.grails.orm.hibernate.support.hibernate7.SessionHolder
+import org.hibernate.SessionFactory
+import org.springframework.transaction.support.TransactionSynchronizationManager
+
+/**
+ * Hibernate 7 specific SessionResolver
+ *
+ * @author borinquenkid
+ * @since 8.0
+ */
+@CompileStatic
+public class HibernateSessionResolver implements SessionResolver<Session> {
+
+    private final SessionFactory sessionFactory
+    private final HibernateDatastore datastore
+
+    public HibernateSessionResolver(HibernateDatastore datastore, SessionFactory sessionFactory) {
+        this.datastore = datastore
+        this.sessionFactory = sessionFactory
+    }
+
+    @Override
+    public Session resolve() {
+        Object resource = TransactionSynchronizationManager.getResource(sessionFactory)
+        if (resource instanceof SessionHolder) {
+            return new HibernateSession(datastore, sessionFactory) // Wraps current session
+        }
+        return null
+    }
+
+    @Override
+    public Session resolve(String qualifier) {
+        // Implementation for multi-datasource routing
+        return datastore.getDatastoreForConnection(qualifier).getSessionResolver().resolve()
+    }
+
+    @Override
+    public void bind(Session session) {
+        if (session instanceof HibernateSession) {
+            TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(((HibernateSession) session).getNativeSession()))
+        }
+    }
+
+    @Override
+    public void unbind() {
+        TransactionSynchronizationManager.unbindResource(sessionFactory)
+    }
+}