[Neo] Fwd: Neo4J and Concurrency

Tobias Ivarsson tobias.ivarsson at neotechnology.com
Mon Apr 26 14:58:44 CEST 2010


If you want to get the orderings right without having to rely on waiting for
a wall clock, I'd suggest using a CountDownLatch[1]
Then you would change the tearDown() method, and the run() method of the
Executor task as follows:

<code>
CountDownLatch latch = new CountDownLatch(10);

@After
public void tearDown() {
    latch.await();
    db.shutdown();
}

   ....

public void run() {
    Transaction tx = db.beginTx();
    try {
        writeNode(j);
        // If you don't want ALL your transactions to roll back, add
<tx.success();> here.
    } catch (DeadlockDetectedException e) {
        System.err.println(e.getMessage());
    } finally {
        tx.finish();
        latch.countDown();
    }
}
</code>

What Johan is saying is that we are unable to reproduce the Exception here
(we do however get other exceptions related to the GraphDatabaseService
being shut down). If you can trigger this error with the
GraphDatabaseService open, then it is an actual problem and we need to dig
deeper. If the fix with the latches above does not solve the issue, could
you please send me a package (tarball or zip or something like that),
containing all the code you use for this (we have currently guessed
what DatabaseAccessor, Kunde and KundeImpl does), along with a specification
of what platform you are running on, which version of Neo4j you are using,
and which JVM you are running on.

Cheers,
Tobias

[1]
http://java.sun.com/javase/6/docs/api/index.html?java/util/concurrent/CountDownLatch.html

On Mon, Apr 26, 2010 at 2:33 PM, Johan Svensson <johan at neotechnology.com>wrote:

> Hi,
>
> This code will shutdown the kernel right away. Depending on timing you
> may shutdown the kernel while the thread pool is still executing and
> that could be the cause of your error.
>
> If you remove the @After / kernel shutdown code or add code in the
> @Test method to wait for the thread pool to execute all tasks will it
> work then?
>
> Regards,
> Johan
>
> On Mon, Apr 26, 2010 at 2:05 PM, Stefan Berndt <kontakt at stberndt.de>
> wrote:
> > Off course i can. This is my testcase-classfile:
> >
> > import org.junit.After;
> > import org.junit.Before;
> > import org.junit.Test;
> > import org.neo4j.graphdb.Transaction;
> > import org.neo4j.kernel.impl.transaction.DeadlockDetectedException;
> >
> > import java.io.IOException;
> > import java.util.concurrent.ExecutorService;
> > import java.util.concurrent.Executors;
> >
> >
> > public class WriteOneNode {
> >    private DatabaseAccessor db;
> >    private static long NodeId;
> >
> >    @Before
> >    public void setUp() throws IOException {
> >        db = SingletonFactory.getInstance();
> >        createNodes();
> >    }
> >
> >    @After
> >
> >    public void tearDown() {
> >        db.shutdown();
> >    }
> >
> >    private void createNodes() {
> >        Transaction tx = db.beginTx();
> >
> >        try {
> >            Kunde k = new KundeImpl(db.createNode());
> >            k.setName("ol");
> >            NodeId = k.getunderlyingNode().getId();
> >            tx.success();
> >        }
> >        finally {
> >            tx.finish();
> >        }
> >    }
> >
> >    @Test
> >    public void writeOneNodeConcurrent() {
> >        ExecutorService pool = Executors.newFixedThreadPool(1);
> >        for (int i = 0; i < 10; i++) {
> >            final int j = i;
> >
> >
> >            pool.execute(new Runnable() {
> >
> >
> >                public void run() {
> >                    Transaction tx = db.beginTx();
> >                    try {
> >
> >                        writeNode(j);
> >                    }
> >                    catch (DeadlockDetectedException e) {
> >                        System.err.println(e.getMessage());
> >                    }
> >                    finally {
> >                        tx.finish();
> >                    }
> >
> >
> >                }
> >
> >
> >            });
> >
> >
> >        }
> >    }
> >
> >    private void writeNode(int j) {
> >        if (j % 2 == 1) {
> >            db.getNodeById(NodeId).setProperty(KundeImpl.KEY_NAME, "n1");
> >
> >        } else db.getNodeById(NodeId).setProperty(KundeImpl.KEY_NAME,
> > "n2");
> >    }
> >
> >
> > }
> >
> > Thx for your help
> > --
> > Stefan
> >
> > ----
> > We should fix this as soon as we can, could you provide a (small) test
> case
> > that can reproduce this with some reliability?
> >
> > /Tobias
> >
> >
> > -------- Original Message --------
> > Subject: Neo4J and Concurrency
> > Date: Mon, 26 Apr 2010 09:10:18 +0000
> > From: Stefan Berndt <kontakt at stberndt.de>
> > To: user at lists.neo4j.org
> >
> > Hello,
> >
> > I am testing Neo4j for a week now and i'm trying to make some operations
> on
> > the Graph concurrent.
> > For this I use the PoolExecutor and do some Write-Operations. but the
> > TransactionManager just throws Exceptions i don't understand.
> > F.ex.:
> >
> > javax.transaction.xa.XAException: Unknown
> > xid[GlobalId[NEOKERNL|1272272945279|2], BranchId[ 52 49 52 49 52 49 ]]
> >        at
> >
> org.neo4j.kernel.impl.transaction.xaframework.XaResourceManager.rollback(XaResourceManager.java:416)
> >        at
> >
> org.neo4j.kernel.impl.transaction.xaframework.XaResourceHelpImpl.rollback(XaResourceHelpImpl.java:111)
> >        at
> >
> org.neo4j.kernel.impl.transaction.TransactionImpl.doRollback(TransactionImpl.java:531)
> >        at
> > org.neo4j.kernel.impl.transaction.TxManager.rollback(TxManager.java:728)
> >        at
> >
> org.neo4j.kernel.impl.transaction.TransactionImpl.rollback(TransactionImpl.java:114)
> >        at
> >
> org.neo4j.kernel.EmbeddedGraphDbImpl$TransactionImpl.finish(EmbeddedGraphDbImpl.java:336)
> >        at ConcurrentTest$1.run(ConcurrentTest.java:62)
> >        at
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
> >        at
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
> >        at java.lang.Thread.run(Thread.java:619)
> > 26.04.2010 11:09:05 org.neo4j.kernel.impl.transaction.TxManager rollback
> > SCHWERWIEGEND: Unable to rollback marked or active transaction. Some
> > resources may be commited others not. Neo4j kernel should be SHUTDOWN for
> > resource maintance and transaction recovery ---->
> > Exception in thread "pool-1-thread-1"
> > org.neo4j.kernel.impl.transaction.TransactionFailureException: Unable to
> > rollback transaction
> >        at
> >
> org.neo4j.kernel.EmbeddedGraphDbImpl$TransactionImpl.finish(EmbeddedGraphDbImpl.java:349)
> >        at ConcurrentTest$1.run(ConcurrentTest.java:62)
> >        at
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
> >        at
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
> >        at java.lang.Thread.run(Thread.java:619)
> > Caused by: javax.transaction.SystemException: Unable to rollback  --->
> > error code for rollback: 0
> >        at
> > org.neo4j.kernel.impl.transaction.TxManager.rollback(TxManager.java:738)
> >        at
> >
> org.neo4j.kernel.impl.transaction.TransactionImpl.rollback(TransactionImpl.java:114)
> >        at
> >
> org.neo4j.kernel.EmbeddedGraphDbImpl$TransactionImpl.finish(EmbeddedGraphDbImpl.java:336)
> >        ... 4 more
> >
> > This Exceptions are coming up already just with one Thread in the
> > ExcecutorPool. Thx for your help.
> _______________________________________________
> Neo mailing list
> User at lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
>



-- 
Tobias Ivarsson <tobias.ivarsson at neotechnology.com>
Hacker, Neo Technology
www.neotechnology.com
Cellphone: +46 706 534857


More information about the User mailing list