Grails ehcache plugin - Another unnamed CacheManager already exists in the same VM - grails

I run my Grails application using ehcache for my 2nd level Cache and it works. I installed the ehcache plugin + cache plugin and then it doesn't. I tried almost all solutions from the internet and found no solution I keep getting Another unnamed CacheManager already exists in the same VM.
One of the possible solutions is to set p:shared=true in the EhCacheManagerFactoryBean, this works if I use an old plugin "springcache plugin from grails" but with the new plugin they use a modified version of this manager and the property shared is not available.
I tried defining a new ehcache.xml file but still I can not put inside a new name for this cache manager.
I tried changing the cache.provider class inside my DataSource.groovy to use one another EhCacheProvider such as a Singleton.
Needless to say, I tested putting a different name using DSL in different places but still no luck.
At the end I'm using the old plugin for spring cache which is deprecated. Can anybody help?
I'm using Grails 2.0.3 and ehcache-core:2.5.3.

In the hibernate section of DataSource.groovy, make sure your cache.provider.class is up to date:
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.region.factory_class = 'grails.plugin.cache.ehcache.hibernate.BeanEhcacheRegionFactory' // For Hibernate before 4.0
cache.region.factory_class = 'grails.plugin.cache.ehcache.hibernate.BeanEhcacheRegionFactory4' // For Hibernate before 4.0 and higher
}
I had the same problem because my cache.region.factory_class was outdated: net.sf.ehcache.hibernate.EhCacheProvider.
See http://grails-plugins.github.io/grails-cache-ehcache/guide/usage.html

For those having this error with Grails 2.5.x, just add this to Config.groovy :
beans {
cacheManager {
shared = true
}
}
This solved the problem for me.
source : https://github.com/grails/grails-core/releases/tag/v2.5.0

Try to use cache & cache-ehcache plugins, it works for me with some limitations. But for 2ndlevel Cache it work correctly

For people arriving here getting the same error as OP might consider that error might be caused by an update of a domain class at runtime (hot code swap), which is not done nicely in alle versions of Grails.
I hit this bug with Grails 2.5.4, yet with the application restart button as the only option to solve.

Related

How to Modify Grails 5 Configuration by Adding Groovy Files

I have a large number of Grails 2.5 applications that I want to upgrade to Grails 5, but have been unable to get the configuration to work. In particular, I want my plugin to set up the data source and Spring Security configuration as it did in Grails 2.5.
In my Grails 2.5 applications, I was able to add files to the configuration by adding this code to the top of Config.groovy.
if (!grails.config.location || !(grails.config.location instanceof List)) {
grails.config.location = []
}
grails.config.location << ["classpath:jcc-server-config.properties"]
grails.config.location << ["classpath:SecurityConfig.groovy"]
But this doesn't work in Grails 5. I've tried adding an application.groovy file, but everything defined in the application.yml seems to be set in stone. Has anybody found a way to add a Groovy file to the Grails configuration that will override or add to the settings in application.yml? YAML will not do because I have logic embedded in the configuration to make it work correctly in different environments.
Thanks.
Did you remember to include the external-config dependency? i.e.
implementation 'dk.glasius:external-config:3.0.0'
Re' your question on accessing config values this way, there should be no difference, in my apps I get to the config either via grailsApplication.config, or if grailsApplication isn't immediately available(e.g. classes under src), then with Holders, i.e. Holders.grailsApplication.config.

Grails 3 and Java 8 time support

In grails 3 application, I see that new java.time classes are persisted to database. Dynamic queries and create criteria works. However, I am wondering if there is some better way how to store these time data to database. When I look to database I see some binary data, it seems it just serialize the time objects.
Is there any similar plugin as joda-time-plugin or is there any way how to configure database mapping for java.time classes?
Edit: Grails 3.1.1 has hibernate 5 so this is not an issue anymore...
Grails
I finally found a solution. First of all hibernate 5 implements persistent for java 8 time (https://hibernate.atlassian.net/browse/HHH-8844). However, grails 3 uses hibernate 4 and I haven't found any plans to upgrade hibernate in grails.
There is an implementation for java 8 time hibernate user types (jadira extended package) which can be used similarly as in joda time plugin (see http://gpc.github.io/joda-time/guide/persistence.html). For me only version 3.1.0.GA and lower worked.
build.gradle
dependencies {
compile "org.jadira.usertype:usertype.extended:3.1.0.GA"
}
application.groovy
grails.gorm.default.mapping = {
"user-type" type: org.jadira.usertype.dateandtime.threeten.PersistentInstantAsMillisLong, class: java.time.Instant
}

How can use Flyway with grails

I know there is a Flyway2 plugin. However im not satisfied since it seems fitted for working by console commands. What i want is to integrate Flyway in a programatic way so:
1st Integration tests use flyway to handle db schema with H2 database
2nd Flyway get's triggered on tomcat deployment and handles also the environment database (maybe through running it from bootstrap?)
Does anyone has experienced with this?
EDIT after some discussion:
In order to use the plugin i would need to get a fully configured instance of GFlyway from spring context. This becomes difficult since the bean only property is def config from where it will read all the required properties. The question is how to replicate this behavior within the resources.groovy ... how to provide the application config as a parameter to the bean.
As we have been discussing in the comments, the correct way to configure this as a bean would be:
// Resources.groovy
beans {
grailsApplication = ref('grailsApplication')
gFlyaway(gflyway2.GFlyway) {
config = grailsApplication.config
}
}
Configure the settings as usual within your Config.groovy per the documentation of the plugin.
That should get you closer, if not all the way there.

Grails with multiple data sources and Hibernate Envers

I'm running a Grails 2.2.4 application with multiple data sources. One requirement is to provide auditing with Hibernate Envers. I did the following:
Domain classes are annotated with org.hibernate.envers.Audited
org.hibernate:hibernate-envers:3.6.10.Final is in the classpath
Hibernate event listeners are defined as follows. This should work with all defined data sources.
beans {
auditEventListener(AuditEventListener)
hibernateEventListeners(HibernateEventListeners) {
listenerMap = [
'post-insert': auditEventListener,
'post-update': auditEventListener,
'post-delete': auditEventListener,
'pre-collection-update': auditEventListener,
'pre-collection-remove': auditEventListener,
'post-collection-recreate': auditEventListener
]
}
}
However, no audit entries are inserted into the revision tables. Has anyone a hint how to fix this? Is this still an issue with the latest Grails version? I could invest the effort to upgrade. Note, I am using just Hibernate Envers. I do not use any Grails plugin. There is a post on nabble.com, but this did not work form.
You can use audit log plugin for this. Audit log plugin in grails
Also. see the following for multiple datasource click me
Or try to use
Grails Envers plugin

Grails 2.3.2/.3 Upgrade - unable to map URLs ending in .html

After upgrading a grails app from version 2.2.2 to 2.3.2 and eventually 2.3.3, I noticed that some links that are previously working, now returns a 404 status.
To illustrate, here are the sample links and the corresponding URL mapping entry that was originally working in v2.2.2:
http://localhost:7080/pages/mytestpage
http://localhost:7080/pages/mytestpage.html
UrlMappings.groovy
static mappings = {
"pages/mytestpage"(controller: 'testController', action: 'testAction')
}
After upgrade, among the given links, the link below does not work anymore (i.e. the link with .html):
http://localhost:7080/pages/mytestpage.html
One way to fix this issue is by changing the URLMappings entry to as below:
UrlMappings.groovy (MODIFIED)
static mappings = {
"pages/mytestpage(.$format)?"(controller: 'testController', action: 'testAction')
}
My question is, is there a way to resolve this problem without having to update the URLMappings entry? Anybody who can explain how such mapping actually worked in version 2.2.2 will also be a great help. Thanks!
UPDATE
Using (.html)? instead of (.$format)? in UrlMappings.groovy works as well and was actually used.
Also, in this example the app server is being hit directly and web server is not used.
In Grails 2.2.x, the grails.mime.file.extensions = true setting along with the grails.mime.types controlled the extension in the URL. Basically, Grails ignored the extensions that are listed in the mime type and mapped the url to the controller accordingly.(thats why mytestpage.html works and not mytestpage.exe nor mytestpage.anything)
It appears that the above behavior was change in Grails 2.3.x+ to support the REST improvements. Even the default mappings in URLMappings.groovy have changed accrodingly
//Grails 2.2.x
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
to
//Grails 2.3.x
"/$controller/$action?/$id?(.${format})?"{
constraints {
// apply constraints here
}
}
Your solution seems to be the correct way to fix the issue

Resources