【问题标题】:Adding relationships to a Neo4j database in parallel将关系并行添加到 Neo4j 数据库
【发布时间】:2013-09-23 18:01:38
【问题描述】:

我正在尝试将关系添加到我使用 Java 的 ExecutorService 并行创建的 Neo4j 图形中。我有两个问题。首先,当我的所有可运行文件都被提交时,我的程序会向前跳转并关闭事务。其次,如果我保持 Transaction 处于打开状态(预先通过无限,因此它不会关闭,因此问题并没有真正解决),当可运行对象被执行时,它们无法添加关系并且似乎被卡住了那些代码行。

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 解析一些文件并创建我的数据库。 Similirize 进行 m x n 比较并将这些关系添加到图表中。我希望并行执行此操作。这就是我现在要模拟的内容:

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();
                }
            }
                });
        }
}

我的输出看起来像: 开始|| 提交可运行文件 提交runnable....(持续很长时间) 运行可运行 尝试 尽管 如果 运行可运行 尝试 尽管 如果 运行可运行 尝试 尽管 如果 运行可运行 尝试 尽管 如果

然后它就永远卡在这里了。

感谢您的帮助!

【问题讨论】:

    标签: java multithreading parallel-processing neo4j


    【解决方案1】:

    每个线程都必须有自己的事务。

    来自docs

    所有访问图、索引或模式的数据库操作都必须在事务中执行。

    ...

    事务绑定到创建它们的线程。

    另外,我不认为您想在 finally 块中调用 service.shutdown()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多