Skip to content

Commit de1faa6

Browse files
committed
Fix Transaction#isOpen() after errors
`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.
1 parent 85760c8 commit de1faa6

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ else if ( state == State.MARKED_FAILED || state == State.ACTIVE )
135135
{
136136
rollbackTx();
137137
}
138+
else if ( state == State.FAILED )
139+
{
140+
// unrecoverable error happened, transaction should've been rolled back on the server
141+
// update state so that this transaction does not remain open
142+
state = State.ROLLED_BACK;
143+
}
138144
}
139145
}
140146
finally

driver/src/test/java/org/neo4j/driver/internal/ExplicitTransactionTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ public void shouldBeClosedAfterRollback()
207207
assertFalse( tx.isOpen() );
208208
}
209209

210+
@Test
211+
public void shouldBeClosedWhenMarkedToCloseAndClosed()
212+
{
213+
ExplicitTransaction tx = new ExplicitTransaction( openConnectionMock(), mock( Runnable.class ) );
214+
215+
tx.markToClose();
216+
tx.close();
217+
218+
assertFalse( tx.isOpen() );
219+
}
220+
210221
private static Connection openConnectionMock()
211222
{
212223
Connection connection = mock( Connection.class );

driver/src/test/java/org/neo4j/driver/internal/NetworkSessionTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
import static junit.framework.Assert.fail;
3131
import static junit.framework.TestCase.assertNotNull;
3232
import static org.hamcrest.CoreMatchers.equalTo;
33+
import static org.junit.Assert.assertFalse;
3334
import static org.junit.Assert.assertThat;
35+
import static org.junit.Assert.assertTrue;
3436
import static org.mockito.Mockito.mock;
3537
import static org.mockito.Mockito.verify;
3638
import static org.mockito.Mockito.when;
@@ -164,4 +166,38 @@ public void shouldGetExceptionIfTryingToCloseSessionMoreThanOnce() throws Throwa
164166
assertThat( e.getMessage(), equalTo("This session has already been closed." ));
165167
}
166168
}
169+
170+
@Test
171+
public void transactionShouldBeOpenAfterSessionReset()
172+
{
173+
NetworkSession session = new NetworkSession( openConnectionMock() );
174+
Transaction tx = session.beginTransaction();
175+
176+
assertTrue( tx.isOpen() );
177+
178+
session.reset();
179+
assertTrue( tx.isOpen() );
180+
}
181+
182+
@Test
183+
public void transactionShouldBeClosedAfterSessionResetAndClose()
184+
{
185+
NetworkSession session = new NetworkSession( openConnectionMock() );
186+
Transaction tx = session.beginTransaction();
187+
188+
assertTrue( tx.isOpen() );
189+
190+
session.reset();
191+
assertTrue( tx.isOpen() );
192+
193+
tx.close();
194+
assertFalse( tx.isOpen() );
195+
}
196+
197+
private static Connection openConnectionMock()
198+
{
199+
Connection connection = mock( Connection.class );
200+
when( connection.isOpen() ).thenReturn( true );
201+
return connection;
202+
}
167203
}

0 commit comments

Comments
 (0)