jackrabbit-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicolas Belisle <Nicolas.Beli...@bibl.ulaval.ca>
Subject Re: Problems with concurrent sessions
Date Wed, 06 Jul 2005 23:47:06 GMT
Hi Oliver,

Thanks for your response.

 >Thereforeall your threads have to have their own session

My code doesn't share any session between threads. Only the repository is 
shared.

 > [...] or you have to synchronize session access to avoid
 >the exception you get (which is similar to ConcurrentModificationException
 > thrown by standard SDK collections/iterators).

Then a solution (as you suggest) would like something like this :

synchronized( repository ) {
         Session session = this.repository.login(new SimpleCredentials("", 
"".toCharArray()), null);
         //Do something
         session.save();
         session.logout();
}

What is the preferred approach ?

Also, if I'm responsible for preventing concurrrent sessions, then why is 
there those multithreading issues in JIRA : 
http://issues.apache.org/jira/browse/JCR-18 & 
http://issues.apache.org/jira/browse/JCR-155 ?

Regards,

Nicolas


Le 18:37 2005-07-06, vous avez écrit:
>Nicolas,
>
>the JCR spec does not require a session to be thread-safe so you are on
>your own to take care for problems like the one you describe. Therefore
>all your threads have to have their own session or you have to
>synchronize session access to avoid the exception you get (which is
>similar to ConcurrentModificationException thrown by standard SDK
>collections/iterators).
>
>Oliver
>
>
>Nicolas Belisle wrote:
>
> > Hi,
> >
> > I'm a Jackrabbit newbie. I'm doing some tests with the tool and I'm
> > having problem with concurrent sessions.
> >
> > I have joined my (simple) test class (JCRTest) and the exception I get
> > from running it.
> >
> > Anyone had similar problems ?
> >
> > Regards,
> >
> > Nicolas
> >
> >
> > -->The exception report:
> > java.util.NoSuchElementException: cdeab285-fdbc-4af3-918a-bf4316a29276
> > at
> > org.apache.jackrabbit.core.LazyItemIterator.next(LazyItemIterator.java:157)
> >
> > at
> > 
> org.apache.jackrabbit.core.LazyItemIterator.nextNode(LazyItemIterator.java:98)
> >
> > at app.JCRTest$ThreadDeleter.run(JCRTest.java:107)
> >
> > -->The class:
> > package app;
> >
> > import java.util.Hashtable;
> >
> > import javax.jcr.Node;
> > import javax.jcr.NodeIterator;
> > import javax.jcr.Repository;
> > import javax.jcr.RepositoryException;
> > import javax.jcr.Session;
> > import javax.jcr.SimpleCredentials;
> > import javax.jcr.observation.Event;
> > import javax.jcr.observation.EventIterator;
> > import javax.jcr.observation.EventListener;
> > import javax.naming.Context;
> > import javax.naming.InitialContext;
> >
> > import org.apache.jackrabbit.core.jndi.RegistryHelper;
> > import org.apache.jackrabbit.core.value.StringValue;
> >
> > public class JCRTest {
> >
> >         private Repository repository;
> >
> >         public JCRTest(Repository repository) {
> >                 this.repository = repository;
> >         }
> >
> >         public void execute() throws Exception {
> >                 Session session = this.repository.login(new
> > SimpleCredentials("", "".toCharArray()), null);
> >
> >                 Node rn = session.getRootNode();
> >                 new ThreadObserver().start();
> >                 Node n = rn.addNode("node" +
> > System.currentTimeMillis(), "nt:unstructured");
> >                 n.setProperty("testprop", new StringValue("Hello,
> > World."));
> >                 new ThreadDeleter().start();
> >
> >                 session.save();
> >                 session.logout();
> >         }
> >
> >         public static void main(String[] args) {
> >                 try {
> >                         String configFile = "repository/repository.xml";
> >                         String repHomeDir = "repository";
> >
> >                         Hashtable env = new Hashtable();
> >                         env.put(Context.INITIAL_CONTEXT_FACTORY,
> > "org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory");
> >                         env.put(Context.PROVIDER_URL, "localhost");
> >                         InitialContext ctx = new InitialContext(env);
> >
> >                         RegistryHelper.registerRepository(ctx, "repo",
> > configFile, repHomeDir, true);
> >                         Repository r = (Repository) ctx.lookup("repo");
> >
> >                         for (int i = 0; i < 20; i++) {
> >                                 JCRTest test = new JCRTest(r);
> >                                 test.execute();
> >                         }
> >
> >                 } catch (Exception e){
> >                         e.printStackTrace();
> >                 }
> >         }
> >
> >         public  class ThreadObserver extends Thread {
> >                 public void run() {
> >                         System.out.println("Observing thread started");
> >                         try {
> >                                 Session session = repository.login(new
> > SimpleCredentials("", "".toCharArray()));
> >                                 EventListener el = new EventListener() {
> >                                         public void
> > onEvent(EventIterator events) {
> >                                                 while
> > (events.hasNext()) {
> >                                                         try {
> >                                         Thread.sleep(100);
> >                                     } catch (InterruptedException e) {}
> >
> >                                     Event e = events.nextEvent();
> >                                                         if
> > (e.getType() == Event.NODE_ADDED) {
> >                                                                 try {
> >
> > System.out.println("Node added : " + e.getPath());
> >                                                                 }
> > catch (RepositoryException re) {
> >
> > re.printStackTrace();
> >                                                                 }
> >                                                         }
> >                                                 }
> >                                         }
> >                                 };
> >
> > session.getWorkspace().getObservationManager().addEventListener(el,
> > Event.NODE_ADDED, "/", true, null, null, false);
> >                                 session.save();
> >                                 //Some time to listen
> >                                 Thread.sleep(500);
> >                                 session.logout();
> >                         } catch (Exception e) {
> >                                 e.printStackTrace();
> >                         }
> >                 }
> >         }
> >
> >         public  class ThreadDeleter extends Thread {
> >                 public void run() {
> >                         System.out.println("Deleter thread started");
> >                         try {
> >                                 Session session = repository.login(new
> > SimpleCredentials("", "".toCharArray()));
> >
> >                                 Node rn = session.getRootNode();
> >                                 for (NodeIterator ni = rn.getNodes();
> > ni.hasNext(); ) {
> >                                         Thread.sleep(100);
> >                                         Node currentNode = ni.nextNode();
> >                                         if
> > (currentNode.getName().startsWith("node")) {
> >                                                 currentNode.remove();
> >                                         }
> >                                 }
> >                                 session.save();
> >                                 session.logout();
> >
> >                         } catch (Exception e) {
> >                                 e.printStackTrace();
> >                         }
> >                 }
> >         }
> > }
> >
>
>
>--
>Oliver Rossmueller
>Software Engineer and IT-Consultant
>Hamburg, Germany
>http://www.rossmueller.com


Mime
View raw message