-->
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: Stale object check outside of a session
PostPosted: Thu Jan 18, 2007 11:40 am 
Newbie

Joined: Mon Dec 18, 2006 4:39 pm
Posts: 2
Hibernate version: 3.1.1

Name and version of the database you are using: Oracle 10g

I am using Hibernate in a middle tier that communicates with various UIs. I have the need to not only detect stale objects across multiple hibernate sessions (which is easy enough with the "version" or "timestamp" tags in the hbm.xml files), but also from objects that have been returned to the UIs and gone in, out and back into a session for updates.

The situation is two users pull up the same record in a maintenance screen (and the sessions are now closed) and then one submits an update (through another request/session). When the second user submits an update (through another request/session), I want the update to fail because the record was changed and is stale. My plan is to detect this with a timestamp (the same way I have it working via the hbm.xml files for objects in the session).

The version and timestamp tags in the xml files don't handle this, at least from what I can tell, because the objects went out and back in to a session. When they are loaded the second time (for the update) it will pick up the new timestamp from the DB from the first user's update, but I want to override that with the original timestamp when the object was first retrieved for the UI (which I do have in hand)

I assume the original record's timestamp is somehow attached to a persistent object in a session but I can't find an attribute. Is there a way that I can set the original record timestamp on the persistent object such that the automatic Hibernate stale object check will work for me? Is there another best practice aside from rolling my own check?

I appreciate any help, thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 1:31 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
The version and timestamp tags in the xml files don't handle this, at least from what I can tell, because the objects went out and back in to a session.


Detached instances carry the version or timestamp. That's why you have to add a version or timestamp property to your entity. What you want already works.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 1:47 pm 
Newbie

Joined: Mon Dec 18, 2006 4:39 pm
Posts: 2
Well, the problem isn't that it is necessarily detatched, the problem is it is a new persistent object in a different session (because the java layer in this application is completely stateless). When the persistent object is loaded for the second request from the UI (to do the update of the object, which is on another session because the inquiry session has been long since closed), that PO is marked with a version/timestamp from the DB at the time when that was loaded into the second session, which is actually after the first update happened (which caused the object to be stale from the UI's perspective). Therefore, the request goes through. What I need to do is instruct the PO that in fact the version/timestamp of the original object is something actually older.

I might be struggling to describe the situation here, or maybe I am not understanding you. Think of a web page that talks to a stateless java web service. Two users on the web page pull up the same record, two sessions are started and closed to return the data to the UI. The record in the DB (and in the UI) is at version 1. One user updates the version 1 object to version 2 in a request that will go through on a completely different session which is started and closed. The second user then submits their request (an update to object of version 1 because that is what was on their screen). With at least the way my code is working, the middle layer will query the DB by primary key for the record desired to be updated and get an attached PO. Hibernate will internally note that it is version 2. My code then analyzes the object passed in by the UI and applies the necessary updates to the PO and then persists the PO to the DB. The PO in the session is at version 2 and the DB is at version 2 so the update will go through. I'd like a way to set the version of that PO back to version 1 after I load it but before I submit the update (because that is the version passed to me by the UI), so I'll get a stale object exception.

I can do all of that manually in code, but if I can just figure out how to get at the version attribute of a PO then I can rely on Hibernate to take care of it for me which would be very nice.

I appreciate the fast response. Thank you.


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.