-->
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.  [ 8 posts ] 
Author Message
 Post subject: Beginner question on locking
PostPosted: Fri Feb 27, 2004 10:58 am 
Beginner
Beginner

Joined: Tue Feb 17, 2004 11:20 am
Posts: 28
Hi,

I'm a total newbie to hibernate and to locking as well so I read few times the manual about locking, but since I have no experience with locking I don't know how I should solve the following problem:

My application uses hibernate to connect to the oracle db, and read data..
I have a status flasg which show what the application user should do with the record.

if only 1 person logs in, there is no problem of course. as he searches the db, and starts working with the records, changing them one by one. until he processes all of them.

but what if 5 users log in, they all will see that there is 20 new records to process, they will all start let's say in the same time... I will have problem as I have to make sure records processed only onces. :) I also have a cluster where the application is running.

how can I use hibernate locking to aboid this problem? is there any easy way to do this without changint the DB modell (that's something I can't do :))

Thanks for any help


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 10:59 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I don't understand what you are trying to do. Do you have a DBA you can ask? I think there is some major confusion about locking and concurrency.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 11:09 am 
Beginner
Beginner

Joined: Tue Feb 17, 2004 11:20 am
Posts: 28
Right now there is noone I could ask here, that's why I though someone maybe could help me here.

this is a webapp, running on tomcat 4.1 and what I want to make sure is:

application users should not process db entries in the same time...

so let's say I have records to process a, b, c

if 3 users are logged in the same time,
when someone already is in the process with the record a,
noone of the other users are able to process that record..

But if this is not hibernate related as I guess, then sorry for posting it here. I will try to find something next week.

Thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 11:14 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You need a pessimistic exclusive lock for that. Hibernate supports the "SELECT ... FOR UPDATE" syntax of Oracle with the LockMode. See the documentation. Keep in mind that you should keep locking time as short as possible, so you don't run into deadlocks. This also means that locks are held in the database, thus a database connection and transaction has to be open for locking.

Have a look at optimistic concurrency, using version numbers and comparision of versions at the end of a transaction. It may not be exactly what you need, but you should try to use it whenever possible instead of using bottleneck exclusive locks.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 11:21 am 
Beginner
Beginner

Joined: Tue Feb 17, 2004 11:20 am
Posts: 28
Thanks for answering.

I though of using pessimistic locking, but my understanding is that when I close the session the connection is closed as well (returned to the pool).

does it mean I will loose the lock as well?

cos I use the ThreadLocal pattern with the ServletFilter to close always my connection at the end of each req-resp-cycle.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 11:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You could also try optimistic-locking=all or optimistic-locking=dirty


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 11:28 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Of course, the lock is only for a single transaction. There are several ways to implement an exclusive lock that spans multiple transactions. Hibernate doesn't support that natively, as we think that locking in the middle tier is not performant. Have a look at Martin Fowlers "Offline Pessimistic Lock" pattern, you can easily implement this with Hibernate. Exclusive locks are easy, but read/write locks are not, however (no matter what persistence framework you use).

Keep in mind that you might create a bottleneck, as every unit of work has to check the lock first. Use it only for the critical parts of your application and not for all use cases.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 11:37 am 
Beginner
Beginner

Joined: Tue Feb 17, 2004 11:20 am
Posts: 28
Thanks for your help! It helped me a lot. Have a nice weekend!


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