-->
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.  [ 5 posts ] 
Author Message
 Post subject: Is there any way to find out if an object is outdated?
PostPosted: Thu Apr 28, 2011 4:03 am 
Beginner
Beginner

Joined: Tue Dec 21, 2010 5:26 am
Posts: 25
Hi,

is there any way to find out if a given object is still up to date without detaching it and later merge etc? Or is the common practise not to know and let optimistic locking produce an exception upon commit if it has changed in the database?

But then what if I just want to make sure that the object is up to date for displaying purposes? Is there some sort of command I could execute right before the end of my unit of work? It would even be enough if I could circumvent the session (cache) and just retrieve the same object fresh out of the database and simply check key properties of it to see if I need to adjust my object to be displayed...

thanks


Top
 Profile  
 
 Post subject: Re: Is there any way to find out if an object is outdated?
PostPosted: Thu Apr 28, 2011 4:52 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
you could use a version-property. Then you would compare your local version with the current version in the db.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Is there any way to find out if an object is outdated?
PostPosted: Fri Apr 29, 2011 3:06 am 
Beginner
Beginner

Joined: Tue Dec 21, 2010 5:26 am
Posts: 25
Thanks for the reply.

What good would a version property to in this case? I mean once I load an object from the database it is cached in the persistence context. Every subsequent load would simply return this chached reference ... not a new (probably more recent) object.

An example. A long running transaction. To get my point across lets say ... 10 minutes(I know this wouldn't/shouldn't happen in any production code)

Thread A loads object ID:1 after the transaction starts then continues doing whatever for 10 mins...
In the mean time Thread B also loads object ID:1 and quickly changes a couple of attributes then commits them.
Thread A doesn't change any attributes on its object. It just needs to display them. So now there is no way it can display the most recent version of this object since it only has "access" to the chached one from 10 minutes ago.

I hope I didn't explain it too complicated. So, is there a way to retrieve a fresh object from the database even if one has already been loaded to the persistence context?


Top
 Profile  
 
 Post subject: Re: Is there any way to find out if an object is outdated?
PostPosted: Fri Apr 29, 2011 3:30 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
From API documentation:

Quote:
Session.refresh(Object): Re-read the state of the given instance from the underlying database.


Edit: Just discovered a warning, but I don't know the reason for it...

Quote:
It is inadvisable to use this to implement long-running sessions that span many business tasks.


Top
 Profile  
 
 Post subject: Re: Is there any way to find out if an object is outdated?
PostPosted: Fri Apr 29, 2011 6:21 am 
Beginner
Beginner

Joined: Tue Dec 21, 2010 5:26 am
Posts: 25
Thanks,

It works. I'm going to propose this as a possibility and see if it gets accepted.


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