Skip to content

Commit

Permalink
Fix Transaction#isOpen() after errors
Browse files Browse the repository at this point in the history
`ExplicitTransaction` has a FAILED state which is reached when fatal
connection error happens or `Session#reset()` is called. In both
cases corresponding transaction on server will be rolled back by the
database. This commit makes sure FAILED state is changed to ROLLED_BACK
when transaction is closed. It's needed to make sure
`Transaction#isOpen()` does not report status incorrectly after
closing of a FAILED transaction.
  • Loading branch information
lutovich committed May 18, 2017
1 parent 85760c8 commit de1faa6
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 de1faa6

Please sign in to comment.