Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my Hibernate Query returning stale data?
    primarykey
    data
    text
    <p><strong>Quick Version</strong></p> <p>Basically, I'm updating a Hibernate Table and subsequent queries are loading a stale value.</p> <p><strong>Detailed Version</strong></p> <p>Hibernate (3.3.1.GA) and EhCache (2.4.2).</p> <p>Persisted <code>Book</code> object with a <code>List&lt;PageContent&gt;</code> of pages and I'm adding a page to the middle of this book. I'm using Databinder/Wicket, though I do not think that is related.</p> <pre><code> public void createPageContent(Book book, int index) { Databinder.getHibernateSession().lock(book, LockMode.UPGRADE); PageContent page = new PageContent(book); book.addPage(page, index); CwmService.get().flushChanges(); // commits the transaction } </code></pre> <p>The applicable fields/method in <code>Book</code> are:</p> <pre><code>@OneToMany @JoinColumn(name="book_id") @IndexColumn(name="pageNum") @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN}) private List&lt;PageContent&gt; pages = new ArrayList&lt;PageContent&gt;(); public synchronized void addPage(PageContent page, int index) { pages.add(index, page); } </code></pre> <p>The end result is that there is a new page added to a list and the database is updated accordingly and I've confirmed this in my datastore. However, the next query for a page, say "Page #4," loads the "old" Page #4 instead of the new Page #4:</p> <pre><code>criteria.add(Restrictions.eq("book", book)); criteria.add(Restrictions.eq("pageNum", pageNum)); criteria.setCacheable(true); </code></pre> <p>So, I grudgingly remove caching from the criteria. It queries the datastore, but <em>still returns the wrong value</em>. However, in both cases, if I wait about 2 minutes, everything is working as expected. I presume caching is still involved. Both <code>PageContent</code> and <code>Book</code> use this caching strategy:</p> <pre><code>@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) </code></pre> <p>I confess I'm new to caching and just set up this file for the first time. Here's my ehcache.xml:</p> <pre><code>&lt;defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" statistics="false"/&gt; &lt;!-- Hibernate's Cache for keeping 'lastUpdated' data on each table. Should never expire. --&gt; &lt;cache name="org.hibernate.cache.UpdateTimestampsCache" eternal="true" /&gt; &lt;!-- Hibernate's Query Cache - should probably be limited --&gt; &lt;cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="1000" /&gt; </code></pre> <p><strong>UPDATE</strong>: Removing the <code>@Cache</code> annotations on my datastore objects removes the problem. Of course, I would like to cache these objects because page modification is much less frequent than access.</p> <p>So, thoughts? There are several other issues related as well, including with deleting pages. Everything updates the database as expected, but actual behavior is wonky.</p> <p>Thanks in advance!</p> <p><strong>UPDATE #2</strong>: Via debugging, I can confirm that the Datastore has the correct information and when the query runs, it falls back on the Second-Level Cache - which has dirty information. I presume it's not up to me to evict from the cache every time the data changes?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload