Adding relationships to a Neo4j database in parallel

一曲冷凌霜 提交于 2019-12-11 19:48:14

问题


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:


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.



来源:https://stackoverflow.com/questions/18881351/adding-relationships-to-a-neo4j-database-in-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!