Grails domain object data is stale without cache

369 views
Skip to first unread message

John Pandich

unread,
Jun 6, 2016, 4:09:22 PM6/6/16
to Grails Dev Discuss

I am testing concurrent updates on Grails domain objects. I am consistently seeing stale object data, including version, when fetching a domain object using dynamic finders in a session & thread exclusive region as if the newly fetched object is an old object in cache.


As I understand Grails/Hibernate, Dynamic finders should ensure we fetch data from the database and not a cache. In order to determine if caching was the problem, I disabled the hibernate cache as follows:


    hibernate {
        cache.use_second_level_cache = false
        cache.use_query_cache = false
    //    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
        cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
        singleSession = true // configure OSIV singleSession mode
        flush.mode = 'manual' // OSIV session flush mode outside of transactional context
    }

Despite disabling caching I am still seeing stale object data. How can this possibly happen? I am currently running on Grails version 2.5.4

Example code flow:

1) Session 1 enters exclusive region, fetches domain object with dynamic finder and saves update. version = 0 before update, version = 1 after update.

2) Session 2 enters exclusive region, fetches domain object with dynamic finder and saves update. version = 1 before update, version = 2 after update.

3) Session 1 renters exclusive region and fetches the domain object with a dynamic finder. The domain object version = 1 before update even though the database clearly indicates version = 2. Upon saving we see an optimistic locking failure occurs showing the version is actually 2.


John Pandich

unread,
Jun 23, 2016, 8:50:46 AM6/23/16
to Grails Dev Discuss
Would anyone be able to confirm that this is not expected behavior?

John Pandich

unread,
Jun 23, 2016, 11:12:41 AM6/23/16
to Grails Dev Discuss
It looks like this issue was addressed brought up by "robert_peszek" on stack overflow: http://stackoverflow.com/questions/25106636/strategies-for-dealing-with-concurrency-issues-caused-by-stale-domain-objects-g

Hibernate ticket https://hibernate.atlassian.net/browse/HHH-9367 was opened and sadly rejected with no good reason as to why. =\ I am going to assume that hibernate must be avoided in order to avoid this problem.

On Monday, June 6, 2016 at 4:09:22 PM UTC-4, John Pandich wrote:

Gregory Dickson

unread,
Jul 10, 2016, 12:33:34 PM7/10/16
to Grails Dev Discuss
John, did you eve solve this issue? The real solution is having a good understanding of how Hibernate works as these scenarios are common when using Hibernate. It is not necessarily a Grails issue. Also, I am not sure what you meant by 'exclusive region'. I have dealt with a lot of these issues. Things to look at:

1) are your domain objects marked cacheable?
2) is the query cacheable?
3) try to understand the issue of having a unique hibernate session for each thread and why one might have persisted and the other one might have stale data in its hibernate session.

FYI, dynamic finders do not have anything to do with whether you read data from the database or not.

G
Reply all
Reply to author
Forward
0 new messages