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: Sense-checking my locking strategy
PostPosted: Fri Feb 08, 2008 7:39 pm 
Newbie

Joined: Thu Mar 09, 2006 12:31 am
Posts: 8
Hello-

I have an app that assigns tasks to a staff member. I'm using NHiberate in my DAL and MySql 5 for the database. What I want to do is select an available task, assign it to a staff member and commit, without the possibility of the task being assigned to two staff members. Here's some rough code that approximates what I want to do.

Code:
ITransaction tx = session.BeginTransaction();

Task task = (Task)session.CreateQuery("FROM tasks t WHERE StaffMember IS NULL ORDER BY t.Created DESC").SetMaxResults(1).UniqueResult();

task.StaffMember = availableStaffMember;

tx.Commit();


To prevent another query from selecting the same Task, I was thinking about using pessimistic locking, by changing the query line above to this:

Code:
IQuery query = _session.CreateQuery("FROM tasks t WHERE StaffMember IS NULL ORDER BY t.Created DESC").SetMaxResults(1);

query.SetLockMode("myKey", LockMode.Upgrade);

Task task = query.UniqueResult<Task>();

The Tasks table is a small table that just holds unassigned tasks.

Two questions. First, is this the best way to achieve my goal (and if not, what would you recommend)? Two, does anyone see any potential performance issues with the approach?

Regards-
Eric


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 11:21 pm 
Newbie

Joined: Thu Mar 09, 2006 12:31 am
Posts: 8
FYI, this doesn't work. I can still get situations where the last write wins. Does anyone have a good idea on what would be a better way to go about this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 13, 2008 4:49 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Use optimistic concurrency and versioning. Read about the <version> and <timestamp> mapping tags in the documentation for further information.

Cheers,

Symon.

_________________
Symon Rottem
http://blog.symbiotic-development.com


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.