brunoid
brunoid

Reputation: 2211

Spring Data Neo4j 4 GraphDatabaseService and result.single()

I'm moving from Spring Data Neo4j 3 to Spring Data Neo4j 4.

I use Embedded Neo4j database.

Right now I can't rewrite the following method:

    public static void cleanDb(Neo4jTemplate template) {
        logger.info("Cleaning database");
        long deletedNodesCount = 0;
        do {
            GraphDatabaseService graphDatabaseService = template.getGraphDatabaseService();
            Transaction tx = graphDatabaseService.beginTx();
            try {
                Result<Map<String, Object>> result = template.query("MATCH (n) WITH n LIMIT " + BATCH_SIZE + " OPTIONAL MATCH (n)-[r]-() DELETE n, r RETURN count(n) as count", null);
                deletedNodesCount = (long) result.single().get("count");
                tx.success();
                logger.info("Deleted " + deletedNodesCount + " nodes...");
            } catch (Throwable th) {
                logger.error("Error while deleting database", th);
                throw th;
            } finally {
                tx.close();
            }
        } while (deletedNodesCount > 0);
    }

How to correctly get graphDatabaseService in SDN4 and also result.single() is absent.

Please help me to rewrite this method for SDN4.

Upvotes: 1

Views: 135

Answers (1)

Luanne
Luanne

Reputation: 19373

You can get a handle to the GraphDatabaseService when using the EmbeddedDriver like this:

 EmbeddedDriver embeddedDriver = (EmbeddedDriver) Components.driver();
 GraphDatabaseService databaseService = embeddedDriver.getGraphDatabaseService();

However, if you're managing transactions manually, you can use @Transactional or the transaction methods available in the OGM Session.

Upvotes: 1

Related Questions