-->
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.  [ 6 posts ] 
Author Message
 Post subject: Stale data
PostPosted: Tue Jan 13, 2004 9:50 am 
Newbie

Joined: Fri Aug 29, 2003 1:18 pm
Posts: 13
Location: Vitoria, ES - Brazil
Hi,

I'm developing a web application using Resin 2.10 and Hibernate 2.1.

There is an administration area where we can create Webpage objects and Area objects. Kind of like this:

Area: idArea, name, startingUrl
Webpage: idWebpage, idArea, url

Thus, an Area can be associated with n Webpages.


Here's the problem: when I edit an Area, change its name and go back to the listing, the system shows the old name. I checked the database and the name has been changed.

If I refresh the page a bunch of times and check on the log I can see that Hibernate issues the following SQL command:

Code:
Hibernate: select area0_.idArea as idArea, area0_.name as name, area0_.startingUrl as startingUrl from area area0_


But still displays stale data. If I keep refreshing the page, eventually it shows the updated data, and I see the following SQL code in the log:

Code:
Hibernate: select webpages0_.idWebPage as idWebPage__, webpages0_.idArea as idArea__, webpages0_.idWebPage as idWebPage0_, webpages0_.url as url0_, webpages0_.idArea as idArea0_ from webPage webpages0_ where webpages0_.idArea=? order by webpages0_.url asc



Does anyone have any ideas to what the problem is or can maybe point me some directions to material (forum archive, hibernate reference) related to this problem?

Thanks in advance,

V


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 10:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Probably something related to your session handling. Are you using a new session for every transaction? Are you flushing, closing the session correctly? Transaction management?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 1:04 pm 
Newbie

Joined: Fri Aug 29, 2003 1:18 pm
Posts: 13
Location: Vitoria, ES - Brazil
gloeglm wrote:
Probably something related to your session handling. Are you using a new session for every transaction? Are you flushing, closing the session correctly? Transaction management?



I use the TheadLocal Session pattern (http://www.hibernate.org/42.html). The code for storing an object is like this:

Code:
Session session = (Session)threadLocal.get();
if (session == null) session = createSession(); // Assigns to threadLocal
else if (! session.isConnected()) session.reconnect();

session.saveOrUpdate(object);
session.connection().commit();
session.flush();
session.disconnect();



And the code to retrieve objects, for instance, all objects of a class, is like this:

Code:
Session session = (Session)threadLocal.get();
if (session == null) session = createSession(); // Assigns to threadLocal
else if (! session.isConnected()) session.reconnect();

Query query = session.createQuery("from " + theClass.getName());
List result = query.list();
session.flush();
session.disconnect();



Anything wrong? Anything missing? Anything redundant?

Thanks again,

_________________
Vitor Souza


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 1:30 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Since you disconnect session, the 1st level cache is used (and it cannot be unplugged). The appropriate usage of session is to close them once your 'user tx' is done.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: [SOLVED] Stale Data
PostPosted: Tue Jan 13, 2004 2:51 pm 
Newbie

Joined: Fri Aug 29, 2003 1:18 pm
Posts: 13
Location: Vitoria, ES - Brazil
emmanuel wrote:
Since you disconnect session, the 1st level cache is used (and it cannot be unplugged). The appropriate usage of session is to close them once your 'user tx' is done.


Thank you. Replacing "disconnect" with "close" worked. For documentation purposes, the new code is now like:

Code:
Session session = (Session)threadLocal.get();
if ((session == null) || (! session.isOpen())) session = createSession();

session.saveOrUpdate(object);
session.flush();
session.connection().commit();
session.close();


and:

Code:
Session session = (Session)threadLocal.get();
if ((session == null) || (! session.isOpen())) session = createSession();

Query query = session.createQuery("from " + theClass.getName());
List result = query.list();
session.flush();
session.disconnect();


I will, however, make changes to my PersistenceUtil classes, after I look around in the documentation for best practices of Hibernate use in various kinds of applications (standalone, web, client-server, etc.). Suggestions on where to look are welcome.

Thanks again,

_________________
Vitor Souza


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 3:37 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The wiki area, the Architecture Forum.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.