Skip to content

Commit 58177f4

Browse files
author
Zhen Li
committed
Invalid current tx when session.reset is called to avoid running more tx in the reseted tx.
Sync on tx.markToClose and tx.run to enforce no more statement in the current tx when tx is already failed (reset).
1 parent 0bb32e7 commit 58177f4

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public StatementResult run( String statementTemplate, Record statementParameters
170170
}
171171

172172
@Override
173-
public StatementResult run( Statement statement )
173+
public synchronized StatementResult run( Statement statement )
174174
{
175175
ensureNotFailed();
176176

@@ -217,7 +217,7 @@ public TypeSystem typeSystem()
217217
return InternalTypeSystem.TYPE_SYSTEM;
218218
}
219219

220-
public void markToClose()
220+
public synchronized void markToClose()
221221
{
222222
state = State.FAILED;
223223
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ public void reset()
109109
ensureNoUnrecoverableError();
110110
ensureConnectionIsOpen();
111111

112+
if( currentTransaction != null )
113+
{
114+
currentTransaction.markToClose();
115+
}
112116
connection.resetAsync();
113117
}
114118

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.neo4j.driver.v1.util.TestNeo4j;
2828

2929
import static org.hamcrest.CoreMatchers.equalTo;
30+
import static org.hamcrest.CoreMatchers.startsWith;
3031
import static org.hamcrest.Matchers.greaterThan;
3132
import static org.junit.Assert.assertFalse;
3233
import static org.junit.Assert.assertThat;
@@ -238,4 +239,26 @@ public void shouldAllowMoreTxAfterSessionReset()
238239
}
239240
}
240241
}
242+
243+
@Test
244+
public void shouldMarkTxAsFailedAndDisallowRunAfterSessionReset()
245+
{
246+
// Given
247+
try( Driver driver = GraphDatabase.driver( neo4j.uri() );
248+
Session session = driver.session() )
249+
{
250+
try( Transaction tx = session.beginTransaction() )
251+
{
252+
// When reset the state of this session
253+
session.reset();
254+
// Then
255+
tx.run( "Return 1" );
256+
fail( "Should not allow tx run as tx is already failed." );
257+
}
258+
catch( Exception e )
259+
{
260+
assertThat( e.getMessage(), startsWith( "Cannot run more statements in this transaction" ) );
261+
}
262+
}
263+
}
241264
}

0 commit comments

Comments
 (0)