jackrabbit-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marcel Reutegger <marcel.reuteg...@gmx.net>
Subject Re: Concurrent modifications ...
Date Thu, 20 Apr 2006 11:36:50 GMT
Hi Bob,

in general your code should work, but the while loop is more or less a 
busy wait. that is your cpu will go to 100% while waiting for the lock.

There is a utility in jackrabbit that you can use to achieve exactly 
what you are trying to do:
org.apache.jackrabbit.util.Locked

here's an example how to use it:

final Node n = .... // the node where you want to add a folder
Node folder = (Node) new Locked() {
     protected Object run() throws RepositoryException {
         Node f = n.addNode("testFolder_" + System.currentTimeMillis(), 
"nt:folder");
         n.save();
         return f;
     }
}.with(n, false);
// do something with the newly created folder
folder.getPath();

The utility however is not included in the jackrabbit 1.0 release. You 
have to look it up in the jackrabbit svn trunk or build jackrabbit from 
trunk.

regards
  marcel

Robert Shiner wrote:
> Hi,
> 
> I wonder if you can help me.
> 
> I would like to write some code that adds a folder to a parent, but I want
> to do this in such a way that it will work during concurrent modifications.
> 
> The code I currently have is as follows ...
> 
>   // Start the session
>   Session session = repository.login(new SimpleCredentials("admin",
> "admin".toCharArray()));
>   try
>   {
>       // Get the root node for now
>       Node rootNode = session.getRootNode();
> 
>      // Wait untill a lock is available on the parent folder
>      boolean hasLock = false;
>      while (hasLock == false)
>      {
>         if (folder.isLocked() == false)
>         {
>            try
>            {
>               // Take the lock out on the parent
>               folder.lock(false, true);
>               hasLock = true;
>            }
>            catch (Exception exception)
>            {
>               // Ignore and retry
>            }
>         }
> 
>          Thread.yield();
>       }
> 
>       // Add the new folder
>       rootNode.addNode ("testFolder_" + System.currentTimeMillis(),
> "nt:folder");
> 
>       // Save the session
>       session.save();
>   }
>   finally
>   {
>       // Log out, lock is revoked
>       session.logout();
>   }
> 
> I have a couple of questions:
> 
> 1)  Is this pattern ok or is there a better way to do things?
> 2)  I have assumed that the parent node (the root node in this example) is
> lockable.  If it isn't what is the best way to apply the lockable aspect in
> a concurrent safe way?
> 
> I'm new to JackRabbit and trying to evaluate its capabilities.  Any help or
> feedback will be gratefully received.
> 
> Thanks in advance,
> Bob
> 


Mime
View raw message