jamesfredley commented on code in PR #16299:
URL: https://github.com/apache/grails-core/pull/16299#discussion_r3930183704
##########
grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfiguration.java:
##########
@@ -239,14 +236,9 @@ public SessionFactory buildSessionFactory() throws
HibernateException {
SessionFactory sessionFactory;
Object classLoaderObject =
getProperties().get(AvailableSettings.CLASSLOADERS);
- ClassLoader appClassLoader;
-
- if (classLoaderObject instanceof ClassLoader) {
- appClassLoader = (ClassLoader) classLoaderObject;
- }
- else {
- appClassLoader = getClass().getClassLoader();
- }
+ ClassLoader storedClassLoader = classLoaderObject instanceof
ClassLoader ?
+ (ClassLoader) classLoaderObject : getClass().getClassLoader();
+ ClassLoader appClassLoader =
DevToolsClassLoaders.resolve(storedClassLoader);
Review Comment:
Same as the Hibernate 7 counterpart: the re-resolve is intentional because
`addProperties()` can overwrite `CLASSLOADERS` and Hibernate re-resolves entity
classes from TCCL. `resolveSessionFactoryClassLoader()` is now tested, and
`buildSessionFactory` wraps TCCL around `super.buildSessionFactory`.
##########
grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfigurationSpec.groovy:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.cfg
+
+import javax.sql.DataSource
+
+import org.grails.datastore.gorm.jdbc.connections.DataSourceSettings
+import org.grails.datastore.mapping.core.connections.ConnectionSource
+import org.hibernate.cfg.AvailableSettings
+import org.springframework.context.ApplicationContext
+import spock.lang.Specification
+
+class HibernateMappingContextConfigurationSpec extends Specification {
+
+ ClassLoader originalContextClassLoader
+
+ def setup() {
+ originalContextClassLoader = Thread.currentThread().contextClassLoader
+ }
+
+ def cleanup() {
+ Thread.currentThread().contextClassLoader = originalContextClassLoader
+ }
+
+ void "setApplicationContext uses the context class loader when DevTools is
not active"() {
+ given:
+ def config = new HibernateMappingContextConfiguration()
+ ClassLoader cl = new URLClassLoader([] as URL[],
originalContextClassLoader)
+ ApplicationContext appCtx = Stub(ApplicationContext) {
+ containsBean("dataSource") >> false
+ getClassLoader() >> cl
+ }
+
+ when:
+ config.setApplicationContext(appCtx)
+
+ then:
+ config.getProperties().get(AvailableSettings.CLASSLOADERS).is(cl)
+ }
+
+ void "setApplicationContext prefers RestartClassLoader thread context
class loader over the context class loader"() {
+ given:
+ def config = new HibernateMappingContextConfiguration()
+ ClassLoader contextLoader = new URLClassLoader([] as URL[],
originalContextClassLoader)
+ ApplicationContext appCtx = Stub(ApplicationContext) {
+ containsBean("dataSource") >> false
+ getClassLoader() >> contextLoader
+ }
+ ClassLoader restartLoader = new GroovyClassLoader().parseClass(
+ 'class RestartClassLoader extends ClassLoader {}'
+ ).getDeclaredConstructor().newInstance() as ClassLoader
+
+ when:
+ Thread.currentThread().contextClassLoader = restartLoader
+ config.setApplicationContext(appCtx)
+
+ then:
+
config.getProperties().get(AvailableSettings.CLASSLOADERS).is(restartLoader)
+ }
+
+ void "setDataSourceConnectionSource uses RestartClassLoader thread context
class loader"() {
+ given:
+ def config = new HibernateMappingContextConfiguration()
+ DataSource ds = Stub(DataSource)
+ ClassLoader restartLoader = new GroovyClassLoader().parseClass(
+ 'class RestartClassLoader extends ClassLoader {}'
+ ).getDeclaredConstructor().newInstance() as ClassLoader
+
+ when:
+ Thread.currentThread().contextClassLoader = restartLoader
+ config.setDataSourceConnectionSource(Stub(ConnectionSource) {
+ getSource() >> ds
+ getName() >> "default"
+ })
+
+ then:
+
config.getProperties().get(AvailableSettings.CLASSLOADERS).is(restartLoader)
+
config.getProperties().get(org.hibernate.cfg.Environment.DATASOURCE).is(ds)
+ }
+
+ void "setDataSourceConnectionSource uses the connection source class
loader when DevTools is not active"() {
+ given:
+ def config = new HibernateMappingContextConfiguration()
+ DataSource ds = Stub(DataSource)
+ ConnectionSource<DataSource, DataSourceSettings> connSrc =
Stub(ConnectionSource) {
+ getName() >> "secondary"
+ getSource() >> ds
+ }
+
+ when:
+ config.setDataSourceConnectionSource(connSrc)
+
+ then:
+ config.dataSourceName == "secondary"
+ config.getProperties().containsKey(AvailableSettings.CLASSLOADERS)
Review Comment:
Fixed. The spec now asserts identity:
`config.getProperties().get(AvailableSettings.CLASSLOADERS).is(connSrc.getClass().getClassLoader())`.
##########
grails-doc/src/en/guide/gettingStarted/developmentReloading.adoc:
##########
@@ -25,6 +25,8 @@ Spring Boot Developer Tools is a feature of Spring Boot that
provides automatic
For larger applications, you may need to adjust the default settings for
optimal performance. This works well until your application becomes very
large, at which point restarts may take longer or fail.
+When Spring Boot Developer Tools restart is active, Grails bootstraps
Hibernate with the restart class loader. That keeps domain class identity
consistent with Hibernate's metamodel without moving GORM or Grails jars onto
the restart loader. Without that alignment, GORM calls such as `Role.count()`
and `save()` can fail with `IllegalArgumentException: Not an entity` while
`get()` and HQL still work.
Review Comment:
Dropped the `Role.count()` symptom sentence. The guide now only says that
when DevTools restart is active, Grails bootstraps Hibernate with the restart
class loader so domain class identity stays consistent with the metamodel.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]