9
9
10
10
< h3 > Example</ h3 >
11
11
12
- < pre > < code > import org.neo4j.driver.v1. *;
12
+ < pre > < code > import org.neo4j.driver.*;
13
13
14
14
import static org.neo4j.driver.Values.parameters;
15
15
@@ -28,21 +28,21 @@ <h3>Example</h3>
28
28
// Sessions are lightweight and disposable connection wrappers.
29
29
try (Session session = driver.session())
30
30
{
31
- // Wrapping Cypher in an unmanaged transaction provides atomicity
31
+ // Wrapping a Cypher Query in a Managed Transaction provides atomicity
32
32
// and makes handling errors much easier.
33
- try (Transaction tx = session.beginTransaction())
34
- {
35
- tx.run("MERGE (a:Person {name: $x})", parameters("x", name));
36
- tx.success(); // Mark this write as successful.
37
- }
33
+ // Use `session.writeTransaction` for writes and `session.readTransaction` for reading data.
34
+ // These methods are also able to handle connection problems and transient errors using an automatic retry mechanism.
35
+ session.writeTransaction(tx -> tx.run("MERGE (a:Person {name: $x})", parameters("x", name)));
38
36
}
39
37
}
40
38
41
39
private void printPeople(String initial)
42
40
{
43
41
try (Session session = driver.session())
44
42
{
45
- // Auto-commit transactions are a quick and easy way to wrap a read.
43
+ // A Managed Transaction transactions are a quick and easy way to wrap a Cypher Query.
44
+ // The `session.run` method will run the specified Query.
45
+ // This simpler method does not use any automatic retry mechanism.
46
46
Result result = session.run(
47
47
"MATCH (a:Person) WHERE a.name STARTS WITH $x RETURN a.name AS name",
48
48
parameters("x", initial));
0 commit comments