Grails domain object data is stale without cache

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Grails domain object data is stale without cache

John Pandich

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.


--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/17f8e0fe-4470-4afa-b0fd-2c0add9955c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Grails domain object data is stale without cache

John Pandich
Would anyone be able to confirm that this is not expected behavior?

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

<a title="Click to mark as favorite question (click again to undo)" href="http://stackoverflow.com/questions/37145081/grails-domain-object-data-is-stale-without-cache#" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F37145081%2Fgrails-domain-object-data-is-stale-without-cache%23\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGzQlkTjMgctiLj3AawpST6C81M7g&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F37145081%2Fgrails-domain-object-data-is-stale-without-cache%23\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGzQlkTjMgctiLj3AawpST6C81M7g&#39;;return true;">

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.


--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/7e032bec-e980-4d42-a564-8a0918e3225e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Grails domain object data is stale without cache

John Pandich
In reply to this post by John Pandich
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:

<a title="Click to mark as favorite question (click again to undo)" href="http://stackoverflow.com/questions/37145081/grails-domain-object-data-is-stale-without-cache#" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F37145081%2Fgrails-domain-object-data-is-stale-without-cache%23\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGzQlkTjMgctiLj3AawpST6C81M7g&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F37145081%2Fgrails-domain-object-data-is-stale-without-cache%23\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGzQlkTjMgctiLj3AawpST6C81M7g&#39;;return true;">

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.


--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/6f66e26d-4e40-4b21-9ce2-c697d18cd306%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Grails domain object data is stale without cache

Gregory Dickson
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

El jueves, 23 de junio de 2016, 10:12:41 (UTC-5), John Pandich escribió:
It looks like this issue was addressed brought up by "<a href="http://stackoverflow.com/users/3626747/robert-peszek" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fusers%2F3626747%2Frobert-peszek\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFxjdsqtMVvHJ4ns5tK1lzPg4-zow&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fusers%2F3626747%2Frobert-peszek\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFxjdsqtMVvHJ4ns5tK1lzPg4-zow&#39;;return true;">robert_peszek" on stack overflow: <a href="http://stackoverflow.com/questions/25106636/strategies-for-dealing-with-concurrency-issues-caused-by-stale-domain-objects-g" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F25106636%2Fstrategies-for-dealing-with-concurrency-issues-caused-by-stale-domain-objects-g\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFlkxV1JLnYxy4ZASZ2_EUQu7t2dA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F25106636%2Fstrategies-for-dealing-with-concurrency-issues-caused-by-stale-domain-objects-g\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFlkxV1JLnYxy4ZASZ2_EUQu7t2dA&#39;;return true;">http://stackoverflow.com/questions/25106636/strategies-for-dealing-with-concurrency-issues-caused-by-stale-domain-objects-g

Hibernate ticket <a href="https://hibernate.atlassian.net/browse/HHH-9367" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fhibernate.atlassian.net%2Fbrowse%2FHHH-9367\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHgv_ddy5Jmj_sc27cKUtBdzB_fqw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fhibernate.atlassian.net%2Fbrowse%2FHHH-9367\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHgv_ddy5Jmj_sc27cKUtBdzB_fqw&#39;;return true;">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:

<a title="Click to mark as favorite question (click again to undo)" href="http://stackoverflow.com/questions/37145081/grails-domain-object-data-is-stale-without-cache#" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F37145081%2Fgrails-domain-object-data-is-stale-without-cache%23\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGzQlkTjMgctiLj3AawpST6C81M7g&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F37145081%2Fgrails-domain-object-data-is-stale-without-cache%23\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGzQlkTjMgctiLj3AawpST6C81M7g&#39;;return true;">

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.


--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/d1e7643c-b119-441d-b751-fdac61836b72%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.