Skip to content

Commit f8b5d56

Browse files
author
Zhen
committed
Fix the nested tx error
A session could be used to run multiple Txs, but each Tx should close before starting another tx in the same session.
1 parent ef35fea commit f8b5d56

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

examples/src/main/java/org/neo4j/docs/driver/Examples.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,22 @@ public static void accessRecord( Session session ) throws Exception
137137
public static void retainResultsForNestedQuerying( Session session ) throws Exception
138138
{
139139
// tag::nested-statements[]
140+
StatementResult result = null;
140141
try ( Transaction transaction = session.beginTransaction() )
141142
{
142-
StatementResult result = transaction.run(
143+
result = transaction.run(
143144
"MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN id(knight) AS knight_id",
144145
Values.parameters( "castle", "Camelot" ) );
145-
146-
for ( Record record : result.list() )
146+
}
147+
for ( Record record : result.list() )
148+
{
149+
try ( Transaction tx = session.beginTransaction() )
147150
{
148-
try ( Transaction tx = session.beginTransaction() )
149-
{
150-
tx.run( "MATCH (knight) WHERE id(knight) = {id} " +
151-
"MATCH (king:Person) WHERE king.name = {king} " +
152-
"CREATE (knight)-[:DEFENDS]->(king)",
153-
Values.parameters( "id", record.get( "knight_id" ), "king", "Arthur" ) );
154-
tx.success();
155-
}
151+
tx.run( "MATCH (knight) WHERE id(knight) = {id} " +
152+
"MATCH (king:Person) WHERE king.name = {king} " +
153+
"CREATE (knight)-[:DEFENDS]->(king)",
154+
Values.parameters( "id", record.get( "knight_id" ), "king", "Arthur" ) );
155+
tx.success();
156156
}
157157
}
158158
// end::nested-statements[]

0 commit comments

Comments
 (0)