Adding relationships to a Neo4j database in parall

2019-09-02 12:52发布

Im trying to add relationships to a Neo4j graph I created in parallel using Java's ExecutorService. I'm having two issues. First, is while all my runnables are sumbitted my program jumps forward and closes the Transaction. Second, if I keep the Transaction open (via an infinite before hand so it doesn't close so that problem isn't really solved), when the runnables are being executed they are not able to add the relationships and seem to be stuck on those lines of code.

private void createDB()
{
    graphDB = new GraphDatabaseFactory().newEmbeddedDatabase(Neo4j_DBPath);
    registerShutdownHook( graphDB );

    Transaction tx = graphDB.beginTx();
    try
    {
        parser.read(File, graphDB);
        parser.similirize();
        while (Global.running) {

        }
        tx.success();
        System.out.println("donedone");
    }
    finally
    {
        System.out.println("closing");
        tx.finish();
    }
}

read parses through some files and creates my database. Similirize makes m x n comparisons and add these relationships to the graph. I was hoping to do this in parallel. This is what I have for similirize right now:

public void similirize() {
    System.out.println("starting ||");
    final Node NoSim = graphDB.createNode();
    NoSim.setProperty("type", "No Similarites");
    int threads = Runtime.getRuntime().availableProcessors();
    System.out.println(threads);
    final ExecutorService service = Executors.newFixedThreadPool(threads);
    for (final Reaction r: FailedRxns){
        System.out.println("submitting runnable");
        service.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("running runnable");
                try {
                    System.out.println("try");
                    Node SIM = Parser.this.mostSimilar(r);
                    while (!Thread.interrupted()) {
                        System.out.println("while");
                        if (SIM != null) {
                            System.out.println("if");
                            r.getUnderlyingNode().createRelationshipTo(SIM, RelTypes.SIMILAR_TO);
                            System.out.println("btwn the lines");
                            r.getUnderlyingNode().createRelationshipTo(SIM.getRelationships(Direction.OUTGOING).iterator().next().getOtherNode(SIM), RelTypes.INFERRED_IN);
                            System.out.println("made connection!");
                        } else {
                            System.out.println("else");
                            r.getUnderlyingNode().createRelationshipTo(NoSim, RelTypes.IN);
                            System.out.println("Couldn't make connection :(");
                        }
                    }
                } finally {
                    service.shutdown();
                }
            }
                });
        }
}

my output looks something like: starting || submitting runnable submitting runnable....(goes on for a long time) running runnable try while if running runnable try while if running runnable try while if running runnable try while if

and then it is stuck here forever.

Thanks for all your help!

1条回答
姐就是有狂的资本
2楼-- · 2019-09-02 13:24

Each Thread must have it's own Transaction.

from the docs:

All database operations that access the graph, indexes, or the schema must be performed in a transaction.

...

Transactions are bound to the thread in which they were created.

Also, I don't think you want to invoke service.shutdown() in the finally block.

查看更多
登录 后发表回答