-->
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.  [ 2 posts ] 
Author Message
 Post subject: Use of lock() in Auction example app
PostPosted: Wed Jan 12, 2005 8:55 pm 
Newbie

Joined: Tue Jan 11, 2005 5:01 pm
Posts: 10
Hibernate version: 2.1

I apologize if this has been addressed elsewhere but I have tried to search for an answer and as I haven't found one I've decided to ask here...

I've been looking at the auction example that comes with the Hibernate 2.1 (and 3.0) download. The example constructs tables, populates them with data and runs various queries and transactions on them. In one of these, bidOnAuction in the Main class, there are locks placed on two objects:

s.lock(item, LockMode.NONE);
s.lock(bidder, LockMode.NONE);

In the context of the example, I couldn't quite understand the necessity of these statements since we are then creating a new bid that relates to these objects via foreign keys. But if I comment the statements out what happens is the bid is not inserted until much later in the chain of actions in our test.

So it appears the lock statements ensure that the bid is inserted before the next query is executed.

Could someone kindly clarify why these statements are in fact needed and how using them acheives this result, and why without them the bid insert is delayed?

Thanks in advance,

Dan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 5:11 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
I'm not familiar with that particular example mode, but I can offer this:

There are two reasons you'd use lock() on an object: 1) to upgrade it's lock to an optimistic lock for concurrency reasons, and 2) to associate an object with a Hibernate session.

I suspect that this code is using session.lock(object, LockMode.NONE) as a way to associate the object with hibernate for persistence. I've used lock() often when I deal with detached Hibernate entities that are serialized either in a session or over the wire. When the object comes back from the client tier I lock() the detached object into the Hibernate session using LockMode.NONE. BTW, update() pretty much does a similar thing. Many folks think that update() will trigger an update. While it might trigger an update when the session flushes(), it's not actually there to force a direct save of the object. I've always thought that update() could have been better named something like "attach," but I guess that wouldn't have conveyed the meaning. In this case, "update" means "update the hibernate session's state with this object" not "update the database." lock(obj, LockMode.NONE) does a similar thing.

Hope that helps.


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