Neo4j通过蓝图从不同线程使用时卡住

Neo4j通过蓝图从不同线程使用时卡住,neo4j,deadlock,tinkerpop,Neo4j,Deadlock,Tinkerpop,我使用的是来自maven存储库的blueprints-neo4j-graph-2.5.0 当使用来自不同线程的图形时,neo4j冻结。重新创建问题的代码,并附加调用堆栈。任何解决方案或任何使用模式都将不胜感激 public class Simplest { public static void main(String[] args) { new Simplest(); } Graph g = new Neo4jGraph("E:/temp/neoTes

我使用的是来自maven存储库的blueprints-neo4j-graph-2.5.0

当使用来自不同线程的图形时,neo4j冻结。重新创建问题的代码,并附加调用堆栈。任何解决方案或任何使用模式都将不胜感激

public class Simplest {

    public static void main(String[] args) {
        new Simplest();
    }

    Graph g = new Neo4jGraph("E:/temp/neoTest");
    Vertex a = g.addVertex(null);

    public Simplest() { 
        new Thread(new Runnable() {
            @Override
            public void run() {
                Vertex b = g.addVertex(null);
                a.addEdge("relation", b);
                System.out.println("never reaches here...");
            }
        }).start();
    }

}
堆栈跟踪如下

Thread [Thread-2] (Suspended)   
    waiting for: RWLock  (id=43)    
    Object.wait(long) line: not available [native method]   
    RWLock(Object).wait() line: 503 
    RWLock.deadlockGuardedWait(Transaction, RWLock$TxLockElement, LockType) line: 652   
    RWLock.acquireWriteLock(Transaction) line: 344  
    LockManagerImpl.getWriteLock(Object, Transaction) line: 84  
    LockManagerImpl.getWriteLock(Object) line: 77   
    WritableTransactionState.acquireWriteLock(Object) line: 269 
    LockType$2.acquire(TransactionState, Object) line: 51   
    NodeManager.getNodeForProxy(long, LockType) line: 473   
    InternalAbstractGraphDatabase$8.lookup(long, LockType) line: 791    
    NodeProxy.createRelationshipTo(Node, RelationshipType) line: 207    
    Neo4jGraph.addEdge(Object, Vertex, Vertex, String) line: 487    
    Neo4jVertex.addEdge(String, Vertex) line: 47    
    Simplest$1.run() line: 24   
    Thread.run() line: not available    

我认为您应该使用显式事务管理,而不是依赖蓝图中的隐式事务处理。最好禁用自动事务—任何事务都只能通过
neo4jGraph.beginTransaction()
或该函数的调用来手动管理它们。

蓝图似乎将当前事务与当前线程相关联。因此,在使用另一个线程的图形之前,必须先关闭它。commit()关闭当前事务。下面的工作

public class Simplest {

    public static void main(String[] args) {
        new Simplest();
    }

    Neo4jGraph g;
    Vertex a;

    {
        g = new Neo4jGraph("E:/temp/neoTest");
        a = g.addVertex(null);
        g.commit();
    }

    public Simplest() {
        new Thread(new Runnable() {
            public void run() {
                Vertex b = g.addVertex(null);
                a.addEdge("relation", b);
                g.commit();
                System.out.println("now reaches here...");
            }
        }).start();
    }

}