-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Criteria.list() consistently returns stale data.
PostPosted: Fri Jul 09, 2010 8:41 pm 
Newbie

Joined: Thu Jul 08, 2010 5:20 am
Posts: 2
The Hibernate manual says that Query.list() is guaranteed not to return stale data. Is this also true for Criteria queries? I am consistently getting stale data from Criteria queries with Hibernate 3.5.2Final executing against SQLServer 2005.

The problem happens when users in different JVMs execute a list query, and then one of the users modifies one of the returned objects, and then both users re-execute the list query. The user who did not modify the data gets back stale information from his second list query.

The odd thing is that when I set show_sql to true, I can see that both sessions claim to be issuing the proper select for each list query, but the data that comes back to the second user's query from Hibernate is stale. I can even execute the query manually BEFORE the second user does to verify that the data has actually been changed, so there is clearly something wrong with the second user's session. Here is a code sample demonstrating the 4 points of execution:

Code:
// (1) User1 - session1/JVM1 - QUERY
Criteria q = session.createCriteria (Model.class).addOrder(Order.asc("created_at"));
List<Model> list = (List<Model>) q.list();
Model m = list.get(0); // Show to user -> not-stale

// (2) User2 - Session2/JVM2 - QUERY
Criteria q = session.createCriteria (Model.class).addOrder(Order.asc("created_at"));
List<Model> list = (List<Model>) q.list();
Model m = list.get(0); // Show to user -> not stale - yet

// (3) User1 - Session1/JVM1 - UPDATE
Model m = list.get(0);
m.setName("new-and-improved-name");
session.beginTransaction ();
session.save (m);
session.getCurrentTransaction ().commit ();
// ...  Show modified object to user
// Manual database access shows the above changes

// (4) User2 - Session2/JVM2 - re-execute QUERY
Criteria q = session.createCriteria (Model.class).addOrder(Order.asc("created_at"));
List<Model> list = (List<Model>) q.list(); // Session2 user refreshes list
Model m = list.get(0); // m is STALE!!!! (actually all of list is stale...)
session.refresh (m); // m no longer stale.


It seems clear that somehow the Criteria query is caching data somehow - even when I have set session.setCacheMode(IGNORE), and session.setFlushMode(ALWAYS). Is there some other way to disable the Criteria.list() cache so that I always get current data back?

I have found that if I execute session.clear() at the start of step (4), then the second user will get non-stale objects back from the list query, but then accessing lazy associations on the returned objects causes exceptions.

Anyone have any clues??


Top
 Profile  
 
 Post subject: Re: Criteria.list() consistently returns stale data.
PostPosted: Wed Jul 21, 2010 8:29 am 
Newbie

Joined: Wed Aug 19, 2009 8:49 am
Posts: 5
I'm facing a problem very near yours.
But i do not receive stale data for only one entity, i'm receiving the whole list staled.

The problem is: using the same SessionFactory, different Sessions, once i executed the criteria.list() and received correct data from the database (using Mysql) i don't receive the modifications like when i clear the referenced table.

Just to clarify:

Table state: populated,
On Java: criteria.list() and a list is returned with data (this is ok)
Then i delete data from the table using a database client, the result of the criteria should be empty, but it isn't, still returning the same list. (this is not ok)

Strange that i see the query been executed in the mysql query logs, and running it on mysql client i get the correct result.

Only when i change my SessionFactory i get different results.I'm in a dead end.


Top
 Profile  
 
 Post subject: Re: Criteria.list() consistently returns stale data.
PostPosted: Wed Jul 21, 2010 9:46 am 
Newbie

Joined: Wed Aug 19, 2009 8:49 am
Posts: 5
So, i solved my problem by changing the connection provider, i was in a test environment using the hibernate's default connection provider, when i moved to C3P0 provider the problem was gone.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.