cancel
Showing results for 
Search instead for 
Did you mean: 

LockService Issue

joaotpd
Champ on-the-rise
Champ on-the-rise
Hi all!

I'm using alfresco 3.4.3 in linux…

When a user creates a document that is numbered. To avoid duplication in numbering I have a counter and I'm using lockService as follows:
(quite simply, I think!)
import org.alfresco.service.cmr.lock.LockService;
import org.alfresco.service.cmr.lock.LockType;
//…
private LockService lockService;
//…
if (counterRef != null)
{
   // lock counter
   boolean locked = false;
   while (!locked)
   {
      try
      {
         lockService.lock(counterRef, LockType.WRITE_LOCK);
         locked = true;
      }
      catch (Exception e)
      {
         try
         {
            wait(500);
         }
         catch (Exception ex)
         {
         }
      }
   }          
   // get the value
   Integer count = (Integer)nodeService.getProperty(counterRef, CustomContentModel.PROP_OUTPUTNUMBER);
   // add one
   count++;
   
   // save the value
   nodeService.setProperty(counterRef, CustomContentModel.PROP_OUTPUTNUMBER, count);
   
   // unlock counter
   lockService.unlock(counterRef);
   
   return count;
}
//…
… so far  users have created about 3,000 documents… FIVE of them with duplicated number… … I can't understand why!?!

Is this the right way to use lockService?

Am I missing something here?

Is there a problem with lockserice in version 3.4.3?

Thanks in advance,

João
3 REPLIES 3

mrogers
Star Contributor
Star Contributor
Yes there's a big problem with the way that you are handling exceptions and transactions.

The lock method is not behaving as you would expects since at the point the lock method returns the transaction has not yet completed.

Really what the lock method is saying is that I'd like to take a lock if at all possible.  When it comes back successfully its simply saying that no-one else has the lock at that point.   The entire transaction could subsequently fail.   But then it will be automatically retry and succeed or fail on the retry.

joaotpd
Champ on-the-rise
Champ on-the-rise
Hi mrogers,

Thanks for the reply…

Are you suggesting retrying transactions to do the content lock?… I'm doing the lock when the content is submited… is that the right way? …

João

joaotpd
Champ on-the-rise
Champ on-the-rise
Hi again,

I've updated the code this way (with retrying transactions in mind):

if (counterRef != null)
{
   // lock counter
   lockService.lock(counterRef, LockType.WRITE_LOCK);
           
   // get the value
   Integer count = (Integer)nodeService.getProperty(counterRef, CustomContentModel.PROP_INPUTNUMBER);
   count++;
         
   // saves the value
   nodeService.setProperty(counterRef, CustomContentModel.PROP_INPUTNUMBER, count);
         
   // unlock counter
   lockService.unlock(counterRef);
           
   return count;
}

much more simple, I think! …

Util now I've no duplicated references… Is this a better way to use lock service?

Can someone please comment… …

Thanks in advance,

João