The Neo4j official Java driver's session object has a readTransaction method that you can use for marking read queries explicitly.
For Version 4 of the driver, the below example using the ReadTransaction function is correct:
public List<String> getActors(String movieName) {
String readQuery = "MATCH (p:Person)-[:ACTED_IN]->(m) " +
"WHERE toLower(m.title) CONTAINS toLower($title) " +
"RETURN DISTINCT p.name as actorsTx";
try (Session session = driver.session()) {
return session.readTransaction(tx -> {
List<String> actors = new ArrayList<>();
Result result = tx.run(readQuery,parameters("title", movieName));
while (result.hasNext()) {
actors.add(result.next().get(0).asString());
}
for (String a : actors) {
System.out.println(String.format("Found actor: %s", a));
}
return actors;
});
}
For Version 5 of the driver, you need to use the ExecuteRead function instead:
public List<String> getActors(String movieName) {
String readQuery = "MATCH (p:Person)-[:ACTED_IN]->(m) " +
"WHERE toLower(m.title) CONTAINS toLower($title) " +
"RETURN DISTINCT p.name as actorsTx";
try (Session session = driver.session()) {
return session.executeRead(tx -> {
List<String> actors = new ArrayList<>();
Result result = tx.run(readQuery,parameters("title", movieName));
while (result.hasNext()) {
actors.add(result.next().get(0).asString());
}
for (String a : actors) {
System.out.println(String.format("Found actor: %s", a));
}
return actors;
});
}
Comments
0 comments
Please sign in to leave a comment.