This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA/Hibernate Query Returns Stale Results

 
Mark Cloutier
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am using an EXTENDED Persistent Context because it will allow me to Lazily Load a one-many relationship on an object and it also won't require a SELECT before I "merge" an object with the persistent context.

I have an DummyObject with:

1. A "Last Updated" Date Field
2. A One-Many Relationship

This Object is being updated every 5 seconds in one JVM through a call.

In another JVM, I query for the DummyObject doing a call like the following



I am also doing this Query every 5 seconds.

The problem is, is that the Objects resulted from the Query all have a Timestamp of the very first Query after successive calls, even though Hibernate is generating the correct SQL statement (when I have statement logging on), and the Database is getting the updates correctly (I have verified).

I have also tried all sorts of optimistic locking with @Version to no avail.

Another thing is that this does work correctly when:

1. I change the PersistentContextType to TRANSACTIONAL (something that will not allow me to lazily load the ONE-MANY relationship)
2. I do an EntityManager.clear() call before I do the Query above (Something that will also not allow me to lazily load the ONE-MANY relationship).

Why does my Query return stale data? I have no Second Level Caching or Query Caching enabled.

Am I doing something wrong? Is there something I can set through query.setHint( , )?

Maybe I don't understand "EXTENDED" vs TRANSACTIONAL correctly.
 
Lalit Bhatt
Ranch Hand
Posts: 69
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The query will not see the latest data till you do not commit the data from the extended persistent context.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!