Hi all,
We are using hibernate 3.2.2 and ehcache 1.3. The cache provider class is net.sf.ehcache.hibernate.SingletonEhCacheProvider. We have a versioned object X that can get updated by concurrent user threads (via ajax calls in the real world). Without the second level cache on all is fine. StaleObjectStateException gets thrown and we can retry the add if appropriate. However, with the level 2 cache on things can go very wrong:
1 - Hibernate does a put into the cache of X with version 12 and then just after another thread does a put with version 11. The database version is therefore now 12 but the cache thinks its 11. This prevents the object being written to until the cache times out as the cache thinks its up to date.
2 - Hibernate does a put into the cache of X with version 12. The transaction that does the put gets rolled back because of some problem (potentially another thread adjusting object X). The database version is therefore still 11 but the cache thinks its 12. This prevents the object being written to until the cache times out as the cache thinks its up to date.
This is 100% reproducible in a test case that starts 5 threads and calls the add service.
Clearly I must be doing something stupid here as I don't believe that other people would not have seen this before but googleing and searching the forums have produced nothing so far...
Cheers in advance,
Dan
|