Skip to content

Commit 4b1d351

Browse files
author
Zhen Li
committed
Allow more statements and tx after reset
1 parent b3d4bc2 commit 4b1d351

File tree

2 files changed

+18
-35
lines changed

2 files changed

+18
-35
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public void run()
5959

6060
private ExplicitTransaction currentTransaction;
6161
private AtomicBoolean isOpen = new AtomicBoolean( true );
62-
private boolean markedToClose;
6362

6463
NetworkSession( Connection connection, Logger logger )
6564
{
@@ -94,7 +93,7 @@ public StatementResult run( String statementText, Value statementParameters )
9493
}
9594

9695
@Override
97-
public synchronized StatementResult run( Statement statement )
96+
public StatementResult run( Statement statement )
9897
{
9998
ensureConnectionIsValidBeforeRunningSession();
10099
InternalStatementResult cursor = new InternalStatementResult( connection, null, statement );
@@ -104,23 +103,19 @@ public synchronized StatementResult run( Statement statement )
104103
return cursor;
105104
}
106105

107-
public synchronized void reset()
106+
public void reset()
108107
{
109108
ensureSessionIsOpen();
110109
ensureNoUnrecoverableError();
111110
ensureConnectionIsOpen();
112111

113-
if( !markedToClose )
114-
{
115-
markedToClose = true;
116-
connection.resetAsync();
117-
}
112+
connection.resetAsync();
118113
}
119114

120115
@Override
121116
public boolean isOpen()
122117
{
123-
return isOpen.get() && !markedToClose;
118+
return isOpen.get();
124119
}
125120

126121
@Override
@@ -168,7 +163,7 @@ public Transaction beginTransaction()
168163
}
169164

170165
@Override
171-
public synchronized Transaction beginTransaction( String bookmark )
166+
public Transaction beginTransaction( String bookmark )
172167
{
173168
ensureConnectionIsValidBeforeOpeningTransaction();
174169
currentTransaction = new ExplicitTransaction( connection, txCleanup, bookmark );

driver/src/test/java/org/neo4j/driver/v1/integration/SessionIT.java

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.neo4j.driver.v1.util.TestNeo4j;
2828

2929
import static org.hamcrest.CoreMatchers.equalTo;
30-
import static org.hamcrest.CoreMatchers.startsWith;
3130
import static org.hamcrest.Matchers.greaterThan;
3231
import static org.junit.Assert.assertFalse;
3332
import static org.junit.Assert.assertThat;
@@ -122,7 +121,6 @@ public void shouldKillLongRunningStatement() throws Throwable
122121
assertTrue( startTime > 0 );
123122
assertTrue( endTime - startTime > killTimeout * 1000 ); // get reset by session.reset
124123
assertTrue( endTime - startTime < executionTimeout * 1000 / 2 ); // finished before execution finished
125-
assertFalse( session.isOpen() );
126124
}
127125
catch ( Exception e )
128126
{
@@ -199,32 +197,25 @@ public void run()
199197
}
200198

201199
@Test
202-
public void shouldNotAllowMoreStatementAfterSessionReset()
200+
public void shouldAllowMoreStatementAfterSessionReset()
203201
{
204202
// Given
205203
try( Driver driver = GraphDatabase.driver( neo4j.uri() );
206204
Session session = driver.session() )
207205
{
208206

209-
session.run( "Return 1" );
207+
session.run( "Return 1" ).consume();
208+
209+
// When reset the state of this session
210210
session.reset();
211211

212-
// When & Then
213-
try
214-
{
215-
session.run( "Return 2" );
216-
fail( "Should disallow more statements" );
217-
}
218-
catch( Exception e )
219-
{
220-
assertThat( e.getMessage(), startsWith("No more interaction with this session is allowed " +
221-
"as the current session is already closed or marked as closed.") );
222-
}
212+
// Then can run successfully more statements without any error
213+
session.run( "Return 2" ).consume();
223214
}
224215
}
225216

226217
@Test
227-
public void shouldNotAllowMoreTxAfterSessionReset()
218+
public void shouldAllowMoreTxAfterSessionReset()
228219
{
229220
// Given
230221
try( Driver driver = GraphDatabase.driver( neo4j.uri() );
@@ -235,18 +226,15 @@ public void shouldNotAllowMoreTxAfterSessionReset()
235226
tx.run("Return 1");
236227
tx.success();
237228
}
229+
230+
// When reset the state of this session
238231
session.reset();
239232

240-
// When & Then
241-
try
242-
{
243-
session.beginTransaction();
244-
fail( "Should disallow more statements" );
245-
}
246-
catch( Exception e )
233+
// Then can run more Tx
234+
try( Transaction tx = session.beginTransaction() )
247235
{
248-
assertThat( e.getMessage(), startsWith("No more interaction with this session is allowed " +
249-
"as the current session is already closed or marked as closed.") );
236+
tx.run("Return 2");
237+
tx.success();
250238
}
251239
}
252240
}

0 commit comments

Comments
 (0)