Permalink
Browse files

Merge branch '2.1.x' of github.com:grails/grails-core into 2.1.x

  • Loading branch information...
2 parents 0fae2b4 + 0d67868 commit d00aedd15f498cb92e02e1ebbc1e938a5e7aef90 @graemerocher graemerocher committed Sep 19, 2012
@@ -1355,6 +1355,20 @@ public static Mapping getMapping(Class<?> theClass) {
public static Mapping getMapping(GrailsDomainClass domainClass) {
return domainClass == null ? null : MAPPING_CACHE.get(domainClass.getClazz());
}
+
+ public static void clearMappingCache() {
+ MAPPING_CACHE.clear();
+ }
+
+ public static void clearMappingCache(Class<?> theClass) {
+ String className = theClass.getName();
+ for(Iterator<Map.Entry<Class<?>, Mapping>> it = MAPPING_CACHE.entrySet().iterator(); it.hasNext();) {
+ Map.Entry<Class<?>, Mapping> entry = it.next();
+ if(className.equals(entry.getKey().getName())) {
+ it.remove();
+ }
+ }
+ }
/**
* Binds the specified persistant class to the runtime model based on the
@@ -29,7 +29,6 @@ import org.codehaus.groovy.grails.commons.spring.DefaultRuntimeSpringConfigurati
import org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator
import org.codehaus.groovy.grails.commons.spring.RuntimeSpringConfiguration
import org.codehaus.groovy.grails.orm.hibernate.*
-import org.codehaus.groovy.grails.orm.hibernate.cfg.DefaultGrailsDomainConfiguration
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil
import org.codehaus.groovy.grails.orm.hibernate.events.PatchedDefaultFlushEventListener
@@ -39,34 +38,31 @@ import org.codehaus.groovy.grails.orm.hibernate.validation.HibernateConstraintsE
import org.codehaus.groovy.grails.orm.hibernate.validation.HibernateDomainClassValidator
import org.codehaus.groovy.grails.orm.hibernate.validation.PersistentConstraintFactory
import org.codehaus.groovy.grails.orm.hibernate.validation.UniqueConstraint
+import org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin
import org.codehaus.groovy.grails.validation.ConstrainedProperty
import org.codehaus.groovy.grails.validation.ConstraintsEvaluator
+import org.grails.datastore.mapping.core.Datastore
+import org.grails.datastore.mapping.model.MappingContext
+import org.grails.datastore.mapping.model.PersistentEntity
import org.hibernate.EmptyInterceptor
import org.hibernate.FlushMode
import org.hibernate.Session
import org.hibernate.SessionFactory
-import org.hibernate.cfg.Environment
import org.hibernate.cfg.ImprovedNamingStrategy
import org.hibernate.proxy.HibernateProxy
import org.springframework.beans.SimpleTypeConverter
import org.springframework.beans.TypeMismatchException
-import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.config.BeanDefinition
import org.springframework.beans.factory.config.PropertiesFactoryBean
import org.springframework.beans.factory.xml.XmlBeanFactory
import org.springframework.context.ApplicationContext
import org.springframework.dao.DataAccessException
-import org.grails.datastore.mapping.model.MappingContext
-import org.grails.datastore.mapping.model.PersistentEntity
import org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor
import org.springframework.orm.hibernate3.HibernateAccessor
import org.springframework.orm.hibernate3.HibernateCallback
import org.springframework.orm.hibernate3.HibernateTemplate
-import org.springframework.orm.hibernate3.HibernateTransactionManager
-import org.springframework.validation.Validator
import org.springframework.transaction.PlatformTransactionManager
-import org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity
-import org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin
-import org.grails.datastore.mapping.core.Datastore
+import org.springframework.validation.Validator
/**
* Used by HibernateGrailsPlugin to implement the core parts of GORM.
@@ -220,6 +216,7 @@ Using Grails' default naming strategy: '${ImprovedNamingStrategy.name}'"""
}
hibProps.putAll(hibConfig.flatten().toProperties('hibernate'))
+ hibProps.remove('hibernate.reload')
// move net.sf.ehcache.configurationResourceName to "top level" if it exists
if (hibProps.'hibernate.net.sf.ehcache.configurationResourceName') {
@@ -340,33 +337,58 @@ Using Grails' default naming strategy: '${ImprovedNamingStrategy.name}'"""
static final onChange = { event ->
LOG.debug "onChange() started"
- def datasourceNames = [GrailsDomainClassProperty.DEFAULT_DATA_SOURCE]
+ def allDatasourceNames = [GrailsDomainClassProperty.DEFAULT_DATA_SOURCE] as Set
for (name in application.config.keySet()) {
if (name.startsWith('dataSource_')) {
- datasourceNames << name - 'dataSource_'
+ allDatasourceNames << name - 'dataSource_'
}
}
-
+
+ def datasourceNames
+ if(event.source instanceof Class) {
+ GrailsDomainClass dc = application.getDomainClass(event.source.name)
+ if(!dc.getMappingStrategy().equalsIgnoreCase(GrailsDomainClass.GORM)) {
+ return
+ }
+ GrailsDomainBinder.clearMappingCache(event.source)
+ def dcMappingDsNames = GrailsHibernateUtil.getDatasourceNames(dc) as Set
+ datasourceNames = [] as Set
+ for(name in allDatasourceNames) {
+ if(name in dcMappingDsNames || dcMappingDsNames.contains(GrailsDomainClassProperty.ALL_DATA_SOURCES)) {
+ datasourceNames << name
+ }
+ }
+ } else {
+ GrailsDomainBinder.clearMappingCache()
+ datasourceNames = allDatasourceNames
+ }
+
def beans = beans {
for (String datasourceName in datasourceNames) {
LOG.debug "processing DataSource $datasourceName"
boolean isDefault = datasourceName == GrailsDomainClassProperty.DEFAULT_DATA_SOURCE
String suffix = isDefault ? '' : '_' + datasourceName
-
- "${SessionFactoryHolder.BEAN_ID}$suffix"(SessionFactoryHolder) {
- sessionFactory = bean(ConfigurableLocalSessionFactoryBean) { bean ->
- bean.parent = ref("abstractSessionFactoryBeanConfig$suffix")
- proxyIfReloadEnabled = false
- }
- }
-
- GrailsDomainClass dc = application.getDomainClass(event.source.name)
- if (!dc.abstract && GrailsHibernateUtil.usesDatasource(dc, datasourceName)) {
- "${dc.fullName}Validator$suffix"(HibernateDomainClassValidator) {
- messageSource = ref("messageSource")
- domainClass = ref("${dc.fullName}DomainClass")
- sessionFactory = ref("sessionFactory$suffix")
- grailsApplication = ref("grailsApplication", true)
+ def hibConfig = application.config["hibernate$suffix"]
+ def sessionFactoryReload = hibConfig?.containsKey('reload') ? hibConfig.reload : true
+
+ if(sessionFactoryReload) {
+ "${SessionFactoryHolder.BEAN_ID}$suffix"(SessionFactoryHolder) {
+ sessionFactory = bean(ConfigurableLocalSessionFactoryBean) { bean ->
+ bean.parent = ref("abstractSessionFactoryBeanConfig$suffix")
+ proxyIfReloadEnabled = false
+ }
+ }
+
+ if(event.source instanceof Class) {
+ GrailsDomainClass dc = application.getDomainClass(event.source.name)
+ if (!dc.abstract && GrailsHibernateUtil.usesDatasource(dc, datasourceName)) {
+ "${dc.fullName}Validator$suffix"(HibernateDomainClassValidator) {
+ messageSource = ref("messageSource")
+ domainClass = ref("${dc.fullName}DomainClass")
+ sessionFactory = ref("sessionFactory$suffix")
+ grailsApplication = ref("grailsApplication", true)
+ }
+ }
}
}
}
@@ -2,6 +2,7 @@ package org.codehaus.groovy.grails.reload;
import org.codehaus.groovy.grails.orm.hibernate.AbstractGrailsHibernateTests
+import org.hibernate.SessionFactory;
/**
* Tests for auto-reloading of domain classes
@@ -30,6 +31,9 @@ class DomainReloadEye {
assert testDomain.respondsTo("getName")
assert !testDomain.respondsTo("getDescription")
assert !testDomain.respondsTo("addToEyes")
+
+ Map<String,Object> sessionFactoryBeans=ga.mainContext.getBeansOfType(SessionFactory.class);
+ assertEquals 1, sessionFactoryBeans.size()
def newGcl = new GroovyClassLoader()
def event = [source: newGcl.parseClass(domain1), ctx: appCtx, application: ga]
@@ -49,6 +53,9 @@ class DomainReloadEye {
assert newDomain.respondsTo("getDescription")
assert newDomain.respondsTo("save")
assert newDomain.respondsTo("addToEyes")
+
+ sessionFactoryBeans=ga.mainContext.getBeansOfType(SessionFactory.class);
+ assertEquals 1, sessionFactoryBeans.size()
}
void onSetUp() {

0 comments on commit d00aedd

Please sign in to comment.