Java 并行向Neo4j数据库添加关系
我试图将关系添加到我使用Java的ExecutorService并行创建的Neo4j图中。我有两个问题。首先,当我的所有runnables都被sumbit时,我的程序向前跳并关闭事务。第二,如果我让事务保持打开状态(通过一个无穷大的函数,这样它就不会关闭,这样问题就不会真正得到解决),当可运行程序被执行时,它们就无法添加关系,并且似乎被卡在了那些代码行上Java 并行向Neo4j数据库添加关系,java,multithreading,parallel-processing,neo4j,Java,Multithreading,Parallel Processing,Neo4j,我试图将关系添加到我使用Java的ExecutorService并行创建的Neo4j图中。我有两个问题。首先,当我的所有runnables都被sumbit时,我的程序向前跳并关闭事务。第二,如果我让事务保持打开状态(通过一个无穷大的函数,这样它就不会关闭,这样问题就不会真正得到解决),当可运行程序被执行时,它们就无法添加关系,并且似乎被卡在了那些代码行上 private void createDB() { graphDB = new GraphDatabaseFactory().newE
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解析一些文件并创建我的数据库。Similize进行m x n比较,并将这些关系添加到图表中。我希望能同时做到这一点。这是我现在为Similize准备的:
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…(持续很长时间)
可运行
尝试
虽然
如果
可运行
尝试
虽然
如果
可运行
尝试
虽然
如果
可运行
尝试
虽然
如果
然后它就永远卡在这里了
谢谢你的帮助 每个线程必须有自己的事务 从: 访问图形、索引或架构的所有数据库操作都必须在事务中执行 事务绑定到创建它们的线程 另外,我认为您不希望在finally块中调用service.shutdown()