Skip to content

Commit

Permalink
Invalid current tx when session.reset is called to avoid running more…
Browse files Browse the repository at this point in the history
… 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).
  • Loading branch information
Zhen Li committed Sep 8, 2016
1 parent 0bb32e7 commit 21dedff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public StatementResult run( String statementTemplate, Record statementParameters
}

@Override
public StatementResult run( Statement statement )
public synchronized StatementResult run( Statement statement )
{
ensureNotFailed();

Expand Down Expand Up @@ -217,7 +217,7 @@ public TypeSystem typeSystem()
return InternalTypeSystem.TYPE_SYSTEM;
}

public void markToClose()
public synchronized void markToClose()
{
state = State.FAILED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public void reset()
ensureNoUnrecoverableError();
ensureConnectionIsOpen();

if( currentTransaction != null )
{
currentTransaction.markToClose();
}
connection.resetAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.neo4j.driver.v1.util.TestNeo4j;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -238,4 +239,26 @@ public void shouldAllowMoreTxAfterSessionReset()
}
}
}

@Test
public void shouldMarkTxAsFailedAndDisAllowRunAfterSessionReset()
{
// Given
try( Driver driver = GraphDatabase.driver( neo4j.uri() );
Session session = driver.session() )
{
try( Transaction tx = session.beginTransaction() )
{
// When reset the state of this session
session.reset();
// Then
tx.run( "Return 1" );
fail( "Should not allow tx run as tx is already failed." );
}
catch( Exception e )
{
assertThat( e.getMessage(), startsWith( "Cannot run more statements in this transaction" ) );
}
}
}
}

0 comments on commit 21dedff

Please sign in to comment.