jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stefan Guggisberg" <stefan.guggisb...@gmail.com>
Subject Re: Concurrent writing to a JCR repository
Date Sun, 01 Oct 2006 14:10:02 GMT
hi behrang,

On 10/1/06, Behrang Saeedzadeh <behrangsa@gmail.com> wrote:
> Hi Again,
>
> I changed a code a bit and surprisingly I'm getting a new exception:
>
> javax.jcr.InvalidItemStateException:
> 6aa4218b-93a6-4a34-b078-42e843baa5b2: the item cannot be saved because
> it has been modified externally.

this exception indicates that you have 2 or more separate sessions
that try to modify
the *same* item (in your case adding nodes to the /positions node).

you can avoid such situations if you lock the node before
you start modifiying it.

cheers
stefan

>         at org.apache.jackrabbit.core.ItemImpl.getTransientStates(ItemImpl.java:387)
>         at org.apache.jackrabbit.core.ItemImpl.save(ItemImpl.java:1054)
>         at org.apache.jackrabbit.core.SessionImpl.save(SessionImpl.java:805)
>         at org.behrang.practice.jsr170.UserThread.run(Main.java:66)
>         at java.lang.Thread.run(Thread.java:595)
>
> Here's the changed code:
>
> --- --- --- ---
> /*
>  * Created by Behrang Saeedzadeh
>  *  on Sep 29, 2006 @ 5:30:34 PM
>  */
> package org.behrang.practice.jsr170;
>
> import org.apache.jackrabbit.core.TransientRepository;
>
> import javax.jcr.*;
> import java.util.ArrayList;
> import java.util.List;
>
> /**
>  * @author Behrang Saeedzadeh
>  */
> public class Main {
>
>     public static void main(String[] args) throws Exception {
>
>         final int THREAD_COUNT = 2;
>
>         Repository rep = new TransientRepository();
>         List<Session> sessions = new ArrayList<Session>(THREAD_COUNT);
>
>         Session s = rep.login(new SimpleCredentials("username",
> "password".toCharArray()));
>         s.getRootNode().addNode("positions").remove();
>         s.getRootNode().addNode("positions");
>         s.save();
>         s.logout();
>
>         for (int i = 0; i < THREAD_COUNT; i++) {
>             Session sess = rep.login(new SimpleCredentials("User " +
> (i + 1), "password".toCharArray()));
>             sessions.add(sess);
>         }
>
>         for (Session sess : sessions) {
>             new Thread(new UserThread(sess)).start();
>         }
>
>     }
>
> }
>
> class UserThread implements Runnable {
>
>     private Session session;
>     private long start;
>     private long end;
>     private String user;
>
>     public UserThread(Session session) {
>         this.session = session;
>         this.user = session.getUserID();
>     }
>
>     public void run() {
>         start = System.currentTimeMillis();
>         try {
>             Node root = session.getRootNode();
>             Node positions = root.getNode("positions");
>             for (int i = 0; i < 100; i++) {
>                 Node position = positions.addNode("position");
>                 position.setProperty("name", user + " " + (i + 1));
>             }
>
>             session.save();
>             session.logout();
>             end = System.currentTimeMillis();
>             synchronized(System.out) {
>                 System.out.println(user + " finished its job at " +
> (end - start) / 1000d);
>             }
>         } catch (RepositoryException re) {
>             re.printStackTrace();
>         }
>     }
> }
> --- --- --- ---
>
> On 10/1/06, Behrang Saeedzadeh <behrangsa@gmail.com> wrote:
> > Hi,
> >
> > I have written a very simple performance test for Jackrabbit that
> > creates 10 threads each saving 1000 simple one-property nodes to the
> > repository.
> >
> > When I run the test, an exception is thrown:
> >
> > javax.jcr.ItemNotFoundException:
> > 0cada011-dc27-4531-bfcc-e8dde67532d9/{http://www.jcp.org/jcr/1.0}primaryType
> >         at org.apache.jackrabbit.core.ItemManager.createItemInstance(ItemManager.java:464)
> >         at org.apache.jackrabbit.core.ItemManager.getItem(ItemManager.java:320)
> >         at org.apache.jackrabbit.core.ItemImpl.restoreTransientItems(ItemImpl.java:708)
> >         at org.apache.jackrabbit.core.ItemImpl.save(ItemImpl.java:1208)
> >         at org.apache.jackrabbit.core.SessionImpl.save(SessionImpl.java:805)
> >
> >
> >
> > When I reduce the thread count to 1, the program completes with no
> > problem so the problem should be that I am using the JCR API
> > incorrectly. Could someone please show me how can I fix the sample
> > program so that it works correctly?
> >
> > Regards,
> > Behi
> >
> > The Code:
> >
> > --- --- --- ---
> > /*
> >  * Created by Behrang Saeedzadeh
> >  *  on Sep 29, 2006 @ 5:30:34 PM
> >  */
> > package org.behrang.practice.jsr170;
> >
> > import org.apache.jackrabbit.core.TransientRepository;
> >
> > import javax.jcr.*;
> > import java.util.ArrayList;
> > import java.util.List;
> >
> > /**
> >  * @author Behrang Saeedzadeh
> >  */
> > public class Main {
> >
> >     public static void main(String[] args) throws Exception {
> >
> >         final int THREAD_COUNT = 10;
> >
> >         Repository rep = new TransientRepository();
> >         List<Session> sessions = new ArrayList<Session>(THREAD_COUNT);
> >
> >         Session s = rep.login(new SimpleCredentials("username",
> > "password".toCharArray()));
> >         s.getRootNode().addNode("positions").remove();
> >         s.getRootNode().addNode("positions");
> >         s.save();
> >         s.logout();
> >
> >         for (int i = 0; i < THREAD_COUNT; i++) {
> >             Session sess = rep.login(new SimpleCredentials("User " +
> > (i + 1), "password".toCharArray()));
> >             sessions.add(sess);
> >         }
> >
> >         for (Session sess : sessions) {
> >             new Thread(new UserThread(sess)).start();
> >         }
> >
> >     }
> >
> > }
> >
> > class UserThread implements Runnable {
> >
> >     private Session session;
> >     private long start;
> >     private long end;
> >     private String user;
> >
> >     public UserThread(Session session) {
> >         this.session = session;
> >         this.user = session.getUserID();
> >     }
> >
> >     public void run() {
> >         start = System.currentTimeMillis();
> >         try {
> >             for (int i = 0; i < 100; i++) {
> >                 Node root = session.getRootNode();
> >                 Node positions = root.getNode("positions");
> >                 Node position = positions.addNode("position");
> >                 position.setProperty("name", user + " " + (i + 1));
> >             }
> >
> >             session.save();
> >             session.logout();
> >             end = System.currentTimeMillis();
> >             synchronized(System.out) {
> >                 System.out.println(user + " finished its job at " +
> > (end - start) / 1000d);
> >             }
> >         } catch (RepositoryException re) {
> >             re.printStackTrace();
> >         }
> >     }
> > }
> > --- --- --- ---
> >
> > --
> > "Science is a differential equation. Religion
> > is a boundary condition" - Alan Turing
> >
> > Behrang Saeedzadeh
> > http://www.jroller.com/page/behrangsa
> > http://my.opera.com/behrangsa
> >
>
>
> --
> "Science is a differential equation. Religion
> is a boundary condition" - Alan Turing
>
> Behrang Saeedzadeh
> http://www.jroller.com/page/behrangsa
> http://my.opera.com/behrangsa
>

Mime
View raw message