Skip to content

Commit 4acd774

Browse files
committed
Another one bites the dust
1 parent d00c176 commit 4acd774

File tree

10 files changed

+48
-30
lines changed

10 files changed

+48
-30
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private void receiveOne()
186186
{
187187
if (transaction != null)
188188
{
189-
transaction.failure();
189+
transaction.defunct();
190190
}
191191
throw ex;
192192
}

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333

3434
public class InternalTransaction implements Transaction
3535
{
36-
private final Connection conn;
3736
private final Runnable cleanup;
37+
private Connection conn;
3838

3939
private enum State
4040
{
@@ -90,25 +90,36 @@ public void failure()
9090
}
9191
}
9292

93+
@Override
94+
public void defunct()
95+
{
96+
state = State.ROLLED_BACK;
97+
conn = null;
98+
}
99+
93100
@Override
94101
public void close()
95102
{
96103
try
97104
{
98-
if ( state == State.MARKED_SUCCESS )
99-
{
100-
conn.run( "COMMIT", Collections.<String, Value>emptyMap(), null );
101-
conn.discardAll();
102-
conn.sync();
103-
state = State.SUCCEEDED;
104-
}
105-
else if ( state == State.MARKED_FAILED || state == State.ACTIVE )
105+
if ( conn != null && conn.isOpen() )
106106
{
107-
// If alwaysValid of the things we've put in the queue have been sent off, there is no need to
108-
// do this, we could just clear the queue. Future optimization.
109-
conn.run( "ROLLBACK", Collections.<String, Value>emptyMap(), null );
110-
conn.discardAll();
111-
state = State.ROLLED_BACK;
107+
if ( state == State.MARKED_SUCCESS )
108+
{
109+
conn.run( "COMMIT", Collections.<String, Value>emptyMap(), null );
110+
conn.discardAll();
111+
conn.sync();
112+
state = State.SUCCEEDED;
113+
}
114+
else if ( state == State.MARKED_FAILED || state == State.ACTIVE )
115+
{
116+
// If alwaysValid of the things we've put in the queue have been sent off, there is no need to
117+
// do this, we could just clear the queue. Future optimization.
118+
conn.run( "ROLLBACK", Collections.<String, Value>emptyMap(), null );
119+
conn.discardAll();
120+
conn.sync();
121+
state = State.ROLLED_BACK;
122+
}
112123
}
113124
}
114125
finally
@@ -158,7 +169,7 @@ public boolean isOpen()
158169

159170
private void ensureNotFailed()
160171
{
161-
if ( state == State.FAILED || state == State.MARKED_FAILED )
172+
if ( state == State.FAILED || state == State.MARKED_FAILED || state == State.ROLLED_BACK )
162173
{
163174
throw new ClientException(
164175
"Cannot run more statements in this transaction, because previous statements in the " +

driver/src/main/java/org/neo4j/driver/internal/connector/socket/LoggingResponseHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void handleSuccessMessage( Map<String,Value> meta )
8585
public void handleRecordMessage( Value[] fields )
8686
{
8787
super.handleRecordMessage( fields );
88-
logger.debug( "S: RecordMessage{%s}", Arrays.asList( fields ) );
88+
logger.debug( "S: [RECORD %s]", Arrays.asList( fields ) );
8989
}
9090

9191
@Override

driver/src/main/java/org/neo4j/driver/internal/connector/socket/SocketClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public int sendAll( Queue<Message> messages ) throws IOException
102102
}
103103
else
104104
{
105+
logger.debug( "C: %s", message );
105106
writer.write( message );
106107
messageCount += 1;
107108
}

driver/src/main/java/org/neo4j/driver/internal/connector/socket/SocketConnection.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ else if ( e instanceof SocketTimeoutException )
187187
private void queueMessage( Message msg, StreamCollector collector )
188188
{
189189
pendingMessages.add( msg );
190-
logger.debug( "C: %s", msg );
191190
responseHandler.appendResultCollector( collector );
192191
}
193192

@@ -202,9 +201,4 @@ public boolean isOpen()
202201
{
203202
return socket.isOpen();
204203
}
205-
206-
private int nextRequestId()
207-
{
208-
return (requestCounter++);
209-
}
210204
}

driver/src/main/java/org/neo4j/driver/internal/messaging/RecordMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void dispatch( MessageHandler handler ) throws IOException
4141
@Override
4242
public String toString()
4343
{
44-
return "RecordMessage{" + Arrays.toString( fields ) + '}';
44+
return "[RECORD " + Arrays.toString( fields ) + ']';
4545
}
4646

4747
@Override

driver/src/main/java/org/neo4j/driver/v1/Transaction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ public interface Transaction extends Resource, StatementRunner
6565
* </pre>
6666
*/
6767
void failure();
68+
69+
void defunct();
6870
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@
3030
import static org.mockito.Mockito.mock;
3131
import static org.mockito.Mockito.verify;
3232
import static org.mockito.Mockito.verifyNoMoreInteractions;
33+
import static org.mockito.Mockito.when;
3334

3435
public class InternalTransactionTest
3536
{
3637
@Test
37-
public void shouldRollbackOnNoExplicitSuccess() throws Throwable
38+
public void shouldRollbackOnImplicitFailure() throws Throwable
3839
{
3940
// Given
4041
Connection conn = mock( Connection.class );
42+
when( conn.isOpen() ).thenReturn( true );
4143
Runnable cleanup = mock( Runnable.class );
4244
InternalTransaction tx = new InternalTransaction( conn, cleanup );
4345

@@ -48,8 +50,10 @@ public void shouldRollbackOnNoExplicitSuccess() throws Throwable
4850
InOrder order = inOrder( conn );
4951
order.verify( conn ).run( "BEGIN", Collections.<String, Value>emptyMap(), null );
5052
order.verify( conn ).discardAll();
53+
order.verify( conn ).isOpen();
5154
order.verify( conn ).run( "ROLLBACK", Collections.<String, Value>emptyMap(), null );
5255
order.verify( conn ).discardAll();
56+
order.verify( conn ).sync();
5357
verify( cleanup ).run();
5458
verifyNoMoreInteractions( conn, cleanup );
5559
}
@@ -59,21 +63,23 @@ public void shouldRollbackOnExplicitFailure() throws Throwable
5963
{
6064
// Given
6165
Connection conn = mock( Connection.class );
66+
when( conn.isOpen() ).thenReturn( true );
6267
Runnable cleanup = mock( Runnable.class );
6368
InternalTransaction tx = new InternalTransaction( conn, cleanup );
6469

70+
// When
6571
tx.failure();
6672
tx.success(); // even if success is called after the failure call!
67-
68-
// When
6973
tx.close();
7074

7175
// Then
7276
InOrder order = inOrder( conn );
7377
order.verify( conn ).run( "BEGIN", Collections.<String, Value>emptyMap(), null );
7478
order.verify( conn ).discardAll();
79+
order.verify( conn ).isOpen();
7580
order.verify( conn ).run( "ROLLBACK", Collections.<String, Value>emptyMap(), null );
7681
order.verify( conn ).discardAll();
82+
order.verify( conn ).sync();
7783
verify( cleanup ).run();
7884
verifyNoMoreInteractions( conn, cleanup );
7985
}
@@ -83,18 +89,20 @@ public void shouldCommitOnSuccess() throws Throwable
8389
{
8490
// Given
8591
Connection conn = mock( Connection.class );
92+
when( conn.isOpen() ).thenReturn( true );
8693
Runnable cleanup = mock( Runnable.class );
8794
InternalTransaction tx = new InternalTransaction( conn, cleanup );
8895

89-
tx.success();
90-
9196
// When
97+
tx.success();
9298
tx.close();
9399

94100
// Then
101+
95102
InOrder order = inOrder( conn );
96103
order.verify( conn ).run( "BEGIN", Collections.<String, Value>emptyMap(), null );
97104
order.verify( conn ).discardAll();
105+
order.verify( conn ).isOpen();
98106
order.verify( conn ).run( "COMMIT", Collections.<String, Value>emptyMap(), null );
99107
order.verify( conn ).discardAll();
100108
order.verify( conn ).sync();

driver/src/test/java/org/neo4j/driver/internal/connector/socket/LoggingResponseHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void shouldLogRecordMessage() throws Throwable
131131
handler.handleRecordMessage( new Value[]{} );
132132

133133
// Then
134-
assertEquals( "S: RecordMessage{[]}", log );
134+
assertEquals( "S: [RECORD []]", log );
135135
assertEquals( format( new RecordMessage( new Value[]{} ) ), log );
136136
}
137137

driver/src/test/java/org/neo4j/driver/v1/util/Neo4jRunner.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import java.io.IOException;
2323
import java.net.URI;
2424
import java.util.Map;
25+
import java.util.logging.Level;
2526

2627
import org.neo4j.driver.internal.connector.socket.SocketClient;
28+
import org.neo4j.driver.internal.logging.ConsoleLogging;
2729
import org.neo4j.driver.internal.logging.DevNullLogger;
2830
import org.neo4j.driver.v1.Config;
2931
import org.neo4j.driver.v1.Driver;

0 commit comments

Comments
 (0)