-->
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.  [ 1 post ] 
Author Message
 Post subject: locking
PostPosted: Wed Jul 30, 2008 1:43 am 
Newbie

Joined: Wed Jul 30, 2008 1:03 am
Posts: 16
Hello,

I have a very simple and frequently occurring problem but I don't seem to find a solution. Or the canned solutions don't seem to work.

I have a simple operation table. The records in this tables have one column which states whether the operation has "not started" or is "in progress" or has been "completed".

Concurrent processes/machines, look for any available ("not started") operation and update the record so that it shows "in progress". This select and update should be atomic. And it also need to lock the row for the duration between the select and the update.

I have been reading that ReadCommitted isolation level with LockMode.Upgrade on the fetch query should do the trick. But when I regression test it with 5 threads against a MySQL server, I see more than 20% incorrect behavior where a record has been accessed by more than one thread (I see two and sometimes three threads accessing the same record). It's as if there is no locking.

Here is a code snippet:

Code:
            ISession session = factory.OpenSession(); ;
            ITransaction transaction = session.BeginTransaction();
            try
            {
                // try to get one item
                q = session.CreateQuery("from operation in class OperationObject " +
                        "where operation.StatusValue=:init");
                q.SetParameter("init", StatusEnum.init, new GenericEnumMapper<StatusEnum>());
                q.SetMaxResults(1);
                q.SetLockMode("operation", LockMode.Upgrade);

                OperationObject operation = q.UniqueResult<OperationObject>();
                if (operation == null)
                {
                    log.Debug("No operation available");
                    return (null);
                }

                operation.StartDate = START_DATE;
                operation.StatusValue = StatusEnum.progress;
                operation.Server = SERVER;

                session.Update(operation);
                transaction.Commit();
                return (operation);
            }
            catch (Exception e)
            {
                transaction.Rollback();
                throw e; // or display error message
            }
            finally
            {
                session.Close();
            }


I'd appreciate if someone could shed light on the problem and point me to my mistake.

Ramin


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.