facebook

Hibernate + struts – stale data [MyEclipse 5.1]

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #269514 Reply

    jlunn
    Member

    I am trying to develop a relatively small and simple struts + hibernate application and am getting stale data back from my dao’s findById(Long) method in an intermittent but reproducible pattern.

    I’ve had no trouble building a login page and several pages that implemented basic record creation. Right now I’m working on a page that is supposed to let me edit the record. My ActionForm instance calls findById() in whatever thread Tomcat used to service the HTTP GET request for the page containing the form and therefore consults the Hibernate session for that thread [Session A]. I make the call to findById() in the form’s reset method.

    In the thread that processes the HTTP POST request initiated by submitting the formI do a findById() and everything works out great – I get the data I expect from the db, I change some values based on the values submitted in the form, and I perform a saveOrUpdate inside a transaction. Everything works fine – direct db queries always show that the db is getting updated. This always seem to happen in a distinct thread from the first one, so the Hibernate session is different [Session B].

    Getting no exceptions during the save operation, I redirect my user back to the page they came from so that they a) see the success message I’ve put in the session and b) see the form that they had just submitted display with all the latest values.

    This is where things get weird, because the call to findById() now returns stale data which does not reflect the successful updates. I’m sure that the form’s reset() method is being called because I have set breakpoints to make sure. To make it worse, if I reload the page, which causes the exact same reset() method of the form to execute, I get fresh data.

    I’m totally lost on how to get over this, having already spent the better half of the day trying various permutations of turning on the ehcache 2nd level cache, making calls to evict(), clear(), and close(), turning off the 2nd level cache,

    Surely someone here has hit something similar?

    #269542 Reply

    Riyad Kalla
    Member

    There are a few things that could be going on here.

    1) You need to make sure to commit your transaction after the insertion is done, this will flush/sync the session.

    2) This is a lot more vague, but if you have enabled the L2 cache in Hibernate, try turning it off. It’s an agressive cache that can frequently get out of sync with the DB, and unless you are running an enterprise app, you shouldn’t need it.

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Hibernate + struts – stale data [MyEclipse 5.1]

You must be logged in to post in the forum log in