-->
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.  [ 7 posts ] 
Author Message
 Post subject: HibernateUtil loading old values
PostPosted: Sun Dec 02, 2007 6:29 pm 
Newbie

Joined: Sat Jul 14, 2007 11:50 am
Posts: 12
Location: the Burgh
Hi I'm having a problem where Hibernate is loading old values into an object that I'm getting from the DB. This problem happens after my webapp has modified a few things...

It baffles me as to where the problem would be coming from, because any change I make are committed to the DB appropriately whenever I make the changes in the webapp.

I use a commonly available HibernateUtil with threaded sessions & transactions. In the beginning of each action I begin a new Transaction. If I make changes that need to be persisted I save them through the session and then I commit the Transaction... otherwise I don't commit the Transaction and leave it open for the next request.

Is that my mistake? I'm baffled how my HibernateUtil could load old values when I successfully updated those values in the database through that same HibernateUtil.

My HibernateUtil is posted http://www.andrew.cmu.edu/user/tbs/Random/HibernateUtil.java incase you think that's the most likely culprit. I'm just interested in someone suggesting how I debug this problem.

To be more specific this problem arises when I've deleted a child from a parent. I delete the child by removing it from the parent, saving the parent, and committing the Transaction. But I've thoroughly read and understand the chapter 21 example of parent/child relationship and have the parent mapped to the child with cascade="all-delete-orphan". That's all I need to do right?

Thanks for even reading this far. If someone can help me solve this I'm totally willing to compensate them for their time.

-Tommy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 12:33 am 
Newbie

Joined: Fri Aug 31, 2007 11:58 am
Posts: 19
Hi,
Just few checks,
a) Your Hibernate util seems ok, but the way you are calling these helper methods needs to be checked.Are you indeed committing your transaction when you modify something.

b) How are you sure that Changes are reflecting in DB?

c) Can you just make a small example project so that we can run locally and see the behavior.


problem seems interesting , keep us posted if you found any clue!

_________________
Note:Don't forget to rate,if useful.

Blog:http://elope.wordpress.com/


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 3:18 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi Tomisina,

I'm assuming that the values that are old in your objects are the Child:-)If that is the case, dont lazy load the child with the parent. Initialize the child whenever it is required. using Hibernate.initialize();

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject: Re: HibernateUtil loading old values
PostPosted: Mon Dec 03, 2007 12:13 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Another idea:

Whenn you modify objects in one session (A) commit changes, and then load them from another Session (B) you might see values from the beginning of the session/transaction B depending on the isolation level provided by your database.

Jens


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 3:01 pm 
Newbie

Joined: Sat Jul 14, 2007 11:50 am
Posts: 12
Location: the Burgh
Thank you all for your suggestions.

khangharoth:
1&2) I'm definitely committing the changes and they're being reflected in the database immediately when the HibernateUtil.commitTransaction(); executes. I know this because I watch the database in terminal.
3) If this problem persists (most likely will) I will make a really compact version of the problem and post it on this forum... I can't do that right now though because I'm in a bit of a crunch to finish up another project as well.

ramnath:
Yeah I thought that would be the problem, but oddly enough the objects that I'm looking at (through Eclipse's Debugger) are all initialized. In my mind initialized should mean up-to-date with the database... but that's not the case. After executing this code:

Code:
HibernateUtil.beginTransaction();      
Long userId = (Long)session.getAttribute("userId");
User user = userDAO.findById(userId, true);


Where the findById is simply calling this code:

Code:
public T findById(ID id, boolean lock) {
  try {
    T entity;
    if (lock) {
      entity = (T) getSession().get(getPersistentClass(),id,LockMode.UPGRADE);
    } else {
      entity = (T) getSession().get(getPersistentClass(), id);
    }
    return entity;
  } catch (HibernateException e) {
    log.error("unable to find by id=" + id + " entity="+ getPersistentClass());
    return null;
  }
}


After executing that chunk of code, which should simply grab the object out of the database, initialized values inside the User I'm accessing are inaccurate. I've tried initializing them again... in hopes that that would double check the values form the DB or something but no go.

schauder:
This suggestion got me really excited... maybe if I just tweak the hibernate.cfg.xml I could fix everything. Alas no go. Without much testing the values being loaded in my User were inaccurate. These are the properties I have in my cfg.xml:

Code:
hibernate.current_session_context_class: thread
hibernate.default_batch_fetch_size: 16
hibernate.connection.isolation: 8 //TRANSACTION_SERIALIZABLE


and I also turned off any caching... thinking that might help

Code:
hibernate.cache.use_query_cache: false
hibernate.cache.use_second_level_cache: false


Once again thank you guys for your suggestions.
-Tommy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 4:09 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Isolation level 'serializable' will probably create exactly the problem I described, although I am not familiar with the hibernate parameter and it's exact effect.

For a quick check try to rollback or commit the transaction just before you try to load the manipulated data. If the changed values show up transaction handling is the problem.

Jens


Top
 Profile  
 
 Post subject: Old Transactions
PostPosted: Mon Dec 03, 2007 4:15 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
This business of leaving transactions open if there is no change needed to be written troubles me.

Old Sessions will have caches of old data. Close them and see if that helps, or try a brand new session for debug purposes to see if it loads the up-to-date objects.


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