Skip to content

Commit

Permalink
Merge pull request #375 from lutovich/1.1-tx-isOpen-reset
Browse files Browse the repository at this point in the history
Fix `Transaction#isOpen()` after errors
  • Loading branch information
lutovich authored May 19, 2017
2 parents 85760c8 + de1faa6 commit 30cc1a4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ else if ( state == State.MARKED_FAILED || state == State.ACTIVE )
{
rollbackTx();
}
else if ( state == State.FAILED )
{
// unrecoverable error happened, transaction should've been rolled back on the server
// update state so that this transaction does not remain open
state = State.ROLLED_BACK;
}
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ public void shouldBeClosedAfterRollback()
assertFalse( tx.isOpen() );
}

@Test
public void shouldBeClosedWhenMarkedToCloseAndClosed()
{
ExplicitTransaction tx = new ExplicitTransaction( openConnectionMock(), mock( Runnable.class ) );

tx.markToClose();
tx.close();

assertFalse( tx.isOpen() );
}

private static Connection openConnectionMock()
{
Connection connection = mock( Connection.class );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import static junit.framework.Assert.fail;
import static junit.framework.TestCase.assertNotNull;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -164,4 +166,38 @@ public void shouldGetExceptionIfTryingToCloseSessionMoreThanOnce() throws Throwa
assertThat( e.getMessage(), equalTo("This session has already been closed." ));
}
}

@Test
public void transactionShouldBeOpenAfterSessionReset()
{
NetworkSession session = new NetworkSession( openConnectionMock() );
Transaction tx = session.beginTransaction();

assertTrue( tx.isOpen() );

session.reset();
assertTrue( tx.isOpen() );
}

@Test
public void transactionShouldBeClosedAfterSessionResetAndClose()
{
NetworkSession session = new NetworkSession( openConnectionMock() );
Transaction tx = session.beginTransaction();

assertTrue( tx.isOpen() );

session.reset();
assertTrue( tx.isOpen() );

tx.close();
assertFalse( tx.isOpen() );
}

private static Connection openConnectionMock()
{
Connection connection = mock( Connection.class );
when( connection.isOpen() ).thenReturn( true );
return connection;
}
}

0 comments on commit 30cc1a4

Please sign in to comment.