1

我正在使用来自 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    
4

2 回答 2

1

蓝图似乎将当前事务与当前线程相关联。所以在使用另一个线程的图表之前必须关闭它。TransactionalGraph.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();
    }

}
于 2014-08-19T10:56:57.127 回答
0

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

于 2014-08-16T19:40:00.887 回答