Skip to content

Commit 86af35d

Browse files
committed
Handle failures after closed transaction
We should not run `onError` of a transaction after the transaction has been successfully closed. Fixes #146
1 parent af9083f commit 86af35d

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

driver/src/main/java/org/neo4j/driver/internal/InternalSession.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,13 @@ public Transaction beginTransaction()
137137
@Override
138138
public void run()
139139
{
140-
currentTransaction.markAsRolledBack();
141-
currentTransaction = null;
142-
connection.onError( null );
140+
//must check if transaction has been closed
141+
if (currentTransaction != null)
142+
{
143+
currentTransaction.markAsRolledBack();
144+
currentTransaction = null;
145+
connection.onError( null );
146+
}
143147
}
144148
});
145149
return currentTransaction;

driver/src/test/java/org/neo4j/driver/v1/integration/TransactionIT.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void shouldRunAndCommit() throws Throwable
5454
// Then the outcome of both statements should be visible
5555
StatementResult result = session.run( "MATCH (n) RETURN count(n)" );
5656
long nodes = result.single().get( "count(n)" ).asLong();
57-
assertThat( nodes, equalTo( 2l ) );
57+
assertThat( nodes, equalTo( 2L ) );
5858
}
5959

6060
@Test
@@ -70,7 +70,7 @@ public void shouldRunAndRollbackByDefault() throws Throwable
7070
// Then there should be no visible effect of the transaction
7171
StatementResult cursor = session.run( "MATCH (n) RETURN count(n)" );
7272
long nodes = cursor.single().get( "count(n)" ).asLong();
73-
assertThat( nodes, equalTo( 0l ) );
73+
assertThat( nodes, equalTo( 0L ) );
7474
}
7575

7676
@Test
@@ -146,4 +146,22 @@ public void shouldHandleNullParametersGracefully()
146146

147147
}
148148

149+
//See GH #146
150+
@Test
151+
public void shouldHandleFailureAfterClosingTransaction()
152+
{
153+
// GIVEN a successful query in a transaction
154+
Transaction tx = session.beginTransaction();
155+
StatementResult result = tx.run("CREATE (n) RETURN n");
156+
result.consume();
157+
tx.success();
158+
tx.close();
159+
160+
// EXPECT
161+
exception.expect( ClientException.class );
162+
163+
//WHEN running a malformed query in the original session
164+
session.run("CREAT (n) RETURN n").consume();
165+
}
166+
149167
}

0 commit comments

Comments
 (0)