Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5965721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:40:34+00:00 2026-05-22T19:40:34+00:00

Quick Version Basically, I’m updating a Hibernate Table and subsequent queries are loading a

  • 0

Quick Version

Basically, I’m updating a Hibernate Table and subsequent queries are loading a stale value.

Detailed Version

Hibernate (3.3.1.GA) and EhCache (2.4.2).

Persisted Book object with a List<PageContent> 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.

 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
 }

The applicable fields/method in Book are:

@OneToMany
@JoinColumn(name="book_id")
@IndexColumn(name="pageNum")
@Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN})
private List<PageContent> pages = new ArrayList<PageContent>();

public synchronized void addPage(PageContent page, int index) {
    pages.add(index, page);
}

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:

criteria.add(Restrictions.eq("book", book));
criteria.add(Restrictions.eq("pageNum", pageNum));
criteria.setCacheable(true);  

So, I grudgingly remove caching from the criteria. It queries the datastore, but still returns the wrong value. However, in both cases, if I wait about 2 minutes, everything is working as expected. I presume caching is still involved. Both PageContent and Book use this caching strategy:

@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)

I confess I’m new to caching and just set up this file for the first time. Here’s my ehcache.xml:

<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" statistics="false"/>

<!-- Hibernate's Cache for keeping 'lastUpdated' data on each table.  Should never expire. -->
<cache name="org.hibernate.cache.UpdateTimestampsCache" eternal="true" />

<!-- Hibernate's Query Cache - should probably be limited -->
<cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="1000" />

UPDATE: Removing the @Cache 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.

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.

Thanks in advance!

UPDATE #2: 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:40:35+00:00Added an answer on May 22, 2026 at 7:40 pm

    I discovered the problem, but it introduces something else.

    Basically, when modifying a Book object’s List<PageContent> field, Hibernate does three things:

    1. Expires the TimeStamp cache entry for both Book and PageContent
    2. Does many queries to reset the pageNum field on each PageContent object
    3. Removes the Book object from the Second Level Cache.

    This ensures that subsequent queries will search for new objects, etc. However:

    1. Hibernate fails to remove each renumbered PageContent object from the Second Level Cache

    As a result, any query for the list of pages will run properly, but then will fall back on stale Second Level Cache values for the actual data.

    I presume this is because Hibernate feels a pageNum change is not a change in data but a change in behind-the-scenes management. However, that is the data that I would like to read and display.

    The solution is to manually refresh every page after the insertion/deletion has occurred.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick version of my question: Is the only time you need to use lock
Quick version: How do I get an image that was generated on the users
Just a quick questions.. I have made an outlook add-in for 2007 version and
Quick version: We're looking for a way to force a transaction to rollback when
Quick question, does Kohana (version 3) automatically escape data that is passed into ORM::factory.....
Quick question; I've recently upgraded to VS2010, and got the new version of ReSharper.
Just a quick question involving upgrading Magento. I currently have version 1.4.1 and would
Quick version: Do the names of parameters of forms being sent using the standard
This got a bit long-winded, so here's the quick version: Why does this cause
That is a quick question: Where can I get PHP's runkit extension for version

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.