Grails DataSources plugin

Grails DataSources Plugin NoCachingEnabledException Fix

Featured Snippet: To resolve the org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage error in the Grails DataSources plugin, you must explicitly enable the second-level cache in your DataSource.groovy or application configuration by setting hibernate.cache.use_second_level_cache=true and configuring your caching provider properly.

Understanding the Grails Multiple DataSources Error

When working with the Grails framework to integrate multiple datasources, developers occasionally encounter caching configuration issues. A common scenario involves configuring primary and secondary databases, only to be met with a frustrating exception during runtime execution. This issue typically stems from misconfigured Hibernate caching settings across the secondary datasource connections.

The Exception Trace

If you have been trying to integrate multiple datasources with Grails, you might encounter a strange error trace similar to the following within your application logs:

nested exception is org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache] ->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync

This exception halts application execution and prevents successful database queries. It explicitly indicates that Grails and Hibernate are attempting to utilize a second-level cache mechanism that has not been globally enabled or properly configured for the specific datasource being queried.

How to Fix the Hibernate Second-Level Cache Error

Resolving this error requires adjusting your Hibernate configuration properties to ensure that the second-level cache is explicitly enabled and that a valid cache provider is assigned. This must be done for the specific datasource throwing the error.

Step 1: Enable the Second-Level Cache

Navigate to your configuration files, typically grails-app/conf/DataSource.groovy or application.yml depending on your Grails version. Ensure that the Hibernate settings for your custom datasource include the properties to enable caching.

Step 2: Define the Cache Provider

Along with enabling the cache, you must define the caching provider, such as Ehcache. This ensures Hibernate knows exactly which caching implementation to utilize for second-level cache operations.

Frequently Asked Questions (FAQ)

What causes the NoCachingEnabledException in Grails?

This exception is caused when a query or domain class is configured to use caching, but the global Hibernate setting hibernate.cache.use_second_level_cache is set to false or is completely missing from the datasource configuration.

Does the Grails DataSources plugin support second-level caching natively?

Yes, the Grails DataSources plugin fully supports second-level caching. However, each defined datasource maintains its own isolated Hibernate session factory, meaning caching must be explicitly configured for each individual datasource rather than assuming the primary configuration applies globally.

How do I verify my Hibernate caching configuration?

You can verify your caching configuration by enabling Hibernate SQL logging and inspecting the startup logs for cache initialization messages. Ensure that your secondary datasources explicitly declare their cache providers.