遍历图表时无法删除节点 - neo4j java embedded api

时间:2014-05-26 14:31:34

标签: java neo4j graph-databases

我正在使用此遍历来获取我想要删除的所有节点。我进行遍历,并将节点保持在一个单独的结构中,而不是打破遍历。然后我迭代单独的结构,删除所有关系,连接到节点的所有关系(所以我可以安全地删除节点),最后删除所有节点。我没有提出任何异常,删除似乎有效。我正确完成了事务并关闭了数据库。但是当我重新运行代码时,我看到节点和关系仍然存在。我尝试了2个单独的事务,用" delete"标记节点和rels。属性,然后在单独的事务中尝试删除它们。似乎没什么用。有任何想法吗?

public synchronized void cleanDatabase()
{
    Transaction tx = null;
    try 
    {
        tx = service.beginTx();
        ResourceIterator<Node> restaurants = service.findNodesByLabelAndProperty(DynamicLabel.label("Restaurant"), "name", "restaurant").iterator();//GlobalGraphOperations.at(service).getAllNodesWithLabel(DynamicLabel.label("Restaurant")).iterator();//
        Node restaurantNode = null;

        Set<Relationship> removableRelations = new HashSet<Relationship>();
        Set<Node> removableNodes = new HashSet<Node>();

        while(restaurants.hasNext())
        {
            TraversalDescription td = service.traversalDescription()
                    .uniqueness( Uniqueness.NODE_GLOBAL)
                    .breadthFirst()             
                    .relationships(AllRelationshipTypes.hasAgents, Direction.OUTGOING)
                    .relationships(AllRelationshipTypes.hasClients, Direction.OUTGOING)
                    //etc

            restaurantNode = restaurants.next();
            Traverser results = td.traverse( restaurantNode );
            ResourceIterator<Node> nodeIt = results.nodes().iterator();
            int nodeCount = 0;

            while(nodeIt.hasNext())
            {
                nodeCount++;
                Node node = nodeIt.next();
                removableNodes.add(node);
                Iterator<Relationship> relIt = node.getRelationships().iterator();
                while(relIt.hasNext())
                {
                    Relationship rel = relIt.next();
                    removableRelations.add(rel);
                }
            }

            System.out.println("have found "+nodeCount);

        }

        //System.out.println(uniqueRels.toString());

        for(Relationship removableRel:removableRelations){
            //removableRel.setProperty("delete", true);
            removableRel.delete();
            //System.out.println(removableRel.toString());
        }
        int otherNodeCount = 0;
        for(Node remNode:removableNodes){

            Iterator<Relationship> relsAttached = remNode.getRelationships().iterator();
            while(relsAttached.hasNext())
            {
                Relationship relAttach = relsAttached.next();                   
                //relAttach.setProperty("delete", true);
                relAttach.delete();
            }           
            //remNode.setProperty("delete", true);
            remNode.delete();
            System.out.println(remNode.getLabels().toString());

            otherNodeCount++;
        }
        System.out.println(otherNodeCount+" other node count");
        System.out.println("done");
        tx.success();
        System.out.println("success");
    }
    catch(Exception e)
    {
        System.out.println("exception");
        System.out.println(e.getMessage());
        tx.failure();
    }
}

1 个答案:

答案 0 :(得分:3)

您似乎没有拨打tx.finish()tx.close(),也没有使用try-with-resources(在较新版本中自动调用close())。

您使用的是什么版本的neo4j?

如果它有close()方法,无论事务是否失败,你都必须在finally中调用它。

如果没有close()方法,则必须致电finish()。两者都应该做同样的事情。

但问题是,当您使用try-with-resources(Java 7+)创建事务时,它会实现java.lang.AutoCloseable,因此您不需要close()方法。< / p>