Skip to content

Commit 41dc339

Browse files
authored
Merge pull request #328 from neo4j/1.2-small-api-updates
Small API updates
2 parents 87b1f38 + 17755a3 commit 41dc339

File tree

9 files changed

+397
-62
lines changed

9 files changed

+397
-62
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
import org.neo4j.driver.v1.Statement;
3838
import org.neo4j.driver.v1.StatementResult;
3939
import org.neo4j.driver.v1.Transaction;
40+
import org.neo4j.driver.v1.TransactionWork;
4041
import org.neo4j.driver.v1.Value;
4142
import org.neo4j.driver.v1.Values;
4243
import org.neo4j.driver.v1.exceptions.ClientException;
4344
import org.neo4j.driver.v1.types.TypeSystem;
44-
import org.neo4j.driver.v1.util.Function;
4545

4646
import static org.neo4j.driver.v1.Values.value;
4747

@@ -182,13 +182,13 @@ public synchronized Transaction beginTransaction( String bookmark )
182182
}
183183

184184
@Override
185-
public <T> T readTransaction( Function<Transaction,T> work )
185+
public <T> T readTransaction( TransactionWork<T> work )
186186
{
187187
return transaction( AccessMode.READ, work );
188188
}
189189

190190
@Override
191-
public <T> T writeTransaction( Function<Transaction,T> work )
191+
public <T> T writeTransaction( TransactionWork<T> work )
192192
{
193193
return transaction( AccessMode.WRITE, work );
194194
}
@@ -244,7 +244,7 @@ public synchronized void onConnectionError( boolean recoverable )
244244
}
245245
}
246246

247-
private synchronized <T> T transaction( AccessMode mode, Function<Transaction,T> work )
247+
private synchronized <T> T transaction( AccessMode mode, TransactionWork<T> work )
248248
{
249249
RetryDecision decision = null;
250250
List<Throwable> errors = null;
@@ -253,7 +253,21 @@ private synchronized <T> T transaction( AccessMode mode, Function<Transaction,T>
253253
{
254254
try ( Transaction tx = beginTransaction( mode ) )
255255
{
256-
return work.apply( tx );
256+
T result;
257+
try
258+
{
259+
result = work.execute( tx );
260+
}
261+
catch ( Throwable t )
262+
{
263+
// mark transaction for failure if the given unit of work threw exception
264+
// this will override any success marks that were made by the unit of work
265+
tx.failure();
266+
throw t;
267+
}
268+
// given unit of work completed successfully, mark transaction for commit
269+
tx.success();
270+
return result;
257271
}
258272
catch ( Throwable newError )
259273
{

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
3030
import org.neo4j.driver.v1.exceptions.SessionExpiredException;
3131
import org.neo4j.driver.v1.exceptions.TransientException;
32-
import org.neo4j.driver.v1.util.Function;
3332
import org.neo4j.driver.v1.util.Immutable;
3433
import org.neo4j.driver.v1.util.Resource;
3534

@@ -449,10 +448,11 @@ public ConfigBuilder withConnectionTimeout( long value, TimeUnit unit )
449448
}
450449

451450
/**
452-
* Specify the maximum time transactions are allowed to retry via {@link Session#readTransaction(Function)} and
453-
* {@link Session#writeTransaction(Function)} methods. These methods will retry the given unit of work on
454-
* {@link ServiceUnavailableException}, {@link SessionExpiredException} and {@link TransientException} with
455-
* exponential backoff using initial delay of 1 second.
451+
* Specify the maximum time transactions are allowed to retry via
452+
* {@link Session#readTransaction(TransactionWork)} and {@link Session#writeTransaction(TransactionWork)}
453+
* methods. These methods will retry the given unit of work on {@link ServiceUnavailableException},
454+
* {@link SessionExpiredException} and {@link TransientException} with exponential backoff using initial
455+
* delay of 1 second.
456456
* <p>
457457
* Default value is 30 seconds.
458458
*

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.neo4j.driver.v1;
2020

21-
import org.neo4j.driver.v1.util.Function;
2221
import org.neo4j.driver.v1.util.Resource;
2322

2423
/**
@@ -85,21 +84,27 @@ public interface Session extends Resource, StatementRunner
8584

8685
/**
8786
* Execute given unit of work in a {@link AccessMode#READ read} transaction.
87+
* <p>
88+
* Transaction will automatically be committed unless exception is thrown from the unit of work itself or from
89+
* {@link Transaction#close()} or transaction is explicitly marked for failure via {@link Transaction#failure()}.
8890
*
89-
* @param work the {@link Function} to be applied to a new read transaction.
91+
* @param work the {@link TransactionWork} to be applied to a new read transaction.
9092
* @param <T> the return type of the given unit of work.
9193
* @return a result as returned by the given unit of work.
9294
*/
93-
<T> T readTransaction( Function<Transaction,T> work );
95+
<T> T readTransaction( TransactionWork<T> work );
9496

9597
/**
9698
* Execute given unit of work in a {@link AccessMode#WRITE write} transaction.
99+
* <p>
100+
* Transaction will automatically be committed unless exception is thrown from the unit of work itself or from
101+
* {@link Transaction#close()} or transaction is explicitly marked for failure via {@link Transaction#failure()}.
97102
*
98-
* @param work the {@link Function} to be applied to a new write transaction.
103+
* @param work the {@link TransactionWork} to be applied to a new write transaction.
99104
* @param <T> the return type of the given unit of work.
100105
* @return a result as returned by the given unit of work.
101106
*/
102-
<T> T writeTransaction( Function<Transaction,T> work );
107+
<T> T writeTransaction( TransactionWork<T> work );
103108

104109
/**
105110
* Return the bookmark received following the last completed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.v1;
20+
21+
/**
22+
* Callback that executes operations against a given {@link Transaction}.
23+
* To be used with {@link Session#readTransaction(TransactionWork)} and
24+
* {@link Session#writeTransaction(TransactionWork)} methods.
25+
*
26+
* @param <T> the return type of this work.
27+
*/
28+
public interface TransactionWork<T>
29+
{
30+
/**
31+
* Executes all given operations against the same transaction.
32+
*
33+
* @param tx the transaction to use.
34+
* @return some result object or {@code null} if none.
35+
*/
36+
T execute( Transaction tx );
37+
}

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
import org.neo4j.driver.v1.AccessMode;
3939
import org.neo4j.driver.v1.Session;
4040
import org.neo4j.driver.v1.Transaction;
41+
import org.neo4j.driver.v1.TransactionWork;
4142
import org.neo4j.driver.v1.Value;
4243
import org.neo4j.driver.v1.exceptions.ClientException;
4344
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
4445
import org.neo4j.driver.v1.exceptions.SessionExpiredException;
45-
import org.neo4j.driver.v1.util.Function;
4646

4747
import static java.util.Collections.singletonMap;
4848
import static org.hamcrest.CoreMatchers.containsString;
@@ -764,10 +764,10 @@ private static void testConnectionAcquisition( AccessMode sessionMode, AccessMod
764764
when( connectionProvider.acquireConnection( transactionMode ) ).thenReturn( connection );
765765
NetworkSession session = newSession( connectionProvider, sessionMode );
766766

767-
Function<Transaction,Integer> work = new Function<Transaction,Integer>()
767+
TransactionWork<Integer> work = new TransactionWork<Integer>()
768768
{
769769
@Override
770-
public Integer apply( Transaction tx )
770+
public Integer execute( Transaction tx )
771771
{
772772
tx.success();
773773
return 42;
@@ -789,10 +789,10 @@ private static void testTxCommitOrRollback( AccessMode transactionMode, final bo
789789
when( connectionProvider.acquireConnection( transactionMode ) ).thenReturn( connection );
790790
NetworkSession session = newSession( connectionProvider, WRITE );
791791

792-
Function<Transaction,Integer> work = new Function<Transaction,Integer>()
792+
TransactionWork<Integer> work = new TransactionWork<Integer>()
793793
{
794794
@Override
795-
public Integer apply( Transaction tx )
795+
public Integer execute( Transaction tx )
796796
{
797797
if ( commit )
798798
{
@@ -831,10 +831,10 @@ private static void testTxRollbackWhenThrows( AccessMode transactionMode )
831831
NetworkSession session = newSession( connectionProvider, WRITE );
832832

833833
final RuntimeException error = new IllegalStateException( "Oh!" );
834-
Function<Transaction,Void> work = new Function<Transaction,Void>()
834+
TransactionWork<Void> work = new TransactionWork<Void>()
835835
{
836836
@Override
837-
public Void apply( Transaction tx )
837+
public Void execute( Transaction tx )
838838
{
839839
throw error;
840840
}
@@ -864,12 +864,12 @@ private static void testTxIsRetriedUntilSuccessWhenFunctionThrows( AccessMode mo
864864
when( connectionProvider.acquireConnection( mode ) ).thenReturn( connection );
865865
NetworkSession session = newSession( connectionProvider, retryLogic );
866866

867-
int answer = executeTransaction( session, mode, new Function<Transaction,Integer>()
867+
int answer = executeTransaction( session, mode, new TransactionWork<Integer>()
868868
{
869869
int invoked;
870870

871871
@Override
872-
public Integer apply( Transaction tx )
872+
public Integer execute( Transaction tx )
873873
{
874874
if ( invoked++ < failures )
875875
{
@@ -896,10 +896,10 @@ private static void testTxIsRetriedUntilSuccessWhenTxCloseThrows( AccessMode mod
896896
when( connectionProvider.acquireConnection( mode ) ).thenReturn( connection );
897897
NetworkSession session = newSession( connectionProvider, retryLogic );
898898

899-
int answer = executeTransaction( session, mode, new Function<Transaction,Integer>()
899+
int answer = executeTransaction( session, mode, new TransactionWork<Integer>()
900900
{
901901
@Override
902-
public Integer apply( Transaction tx )
902+
public Integer execute( Transaction tx )
903903
{
904904
tx.success();
905905
return 43;
@@ -925,12 +925,12 @@ private static void testTxIsRetriedUntilFailureWhenFunctionThrows( AccessMode mo
925925

926926
try
927927
{
928-
executeTransaction( session, mode, new Function<Transaction,Integer>()
928+
executeTransaction( session, mode, new TransactionWork<Integer>()
929929
{
930930
int invoked;
931931

932932
@Override
933-
public Integer apply( Transaction tx )
933+
public Integer execute( Transaction tx )
934934
{
935935
if ( invoked++ < failures )
936936
{
@@ -963,10 +963,10 @@ private static void testTxIsRetriedUntilFailureWhenTxCloseThrows( AccessMode mod
963963

964964
try
965965
{
966-
executeTransaction( session, mode, new Function<Transaction,Integer>()
966+
executeTransaction( session, mode, new TransactionWork<Integer>()
967967
{
968968
@Override
969-
public Integer apply( Transaction tx )
969+
public Integer execute( Transaction tx )
970970
{
971971
tx.success();
972972
return 42;
@@ -992,12 +992,12 @@ private static void testRetryErrorsAreCombined( AccessMode mode )
992992

993993
try
994994
{
995-
executeTransaction( session, mode, new Function<Transaction,Integer>()
995+
executeTransaction( session, mode, new TransactionWork<Integer>()
996996
{
997997
int invoked;
998998

999999
@Override
1000-
public Integer apply( Transaction tx )
1000+
public Integer execute( Transaction tx )
10011001
{
10021002
if ( invoked++ < failures )
10031003
{
@@ -1035,12 +1035,12 @@ private static void testRetryErrorsAreNotCombinedWhenSameErrorIsThrown( AccessMo
10351035
final ServiceUnavailableException error = new ServiceUnavailableException( "Oh!" );
10361036
try
10371037
{
1038-
executeTransaction( session, mode, new Function<Transaction,Integer>()
1038+
executeTransaction( session, mode, new TransactionWork<Integer>()
10391039
{
10401040
int invoked;
10411041

10421042
@Override
1043-
public Integer apply( Transaction tx )
1043+
public Integer execute( Transaction tx )
10441044
{
10451045
if ( invoked++ < failures )
10461046
{
@@ -1060,7 +1060,7 @@ public Integer apply( Transaction tx )
10601060
}
10611061
}
10621062

1063-
private static <T> T executeTransaction( Session session, AccessMode mode, Function<Transaction,T> work )
1063+
private static <T> T executeTransaction( Session session, AccessMode mode, TransactionWork<T> work )
10641064
{
10651065
if ( mode == READ )
10661066
{

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.neo4j.driver.v1.Record;
4545
import org.neo4j.driver.v1.Session;
4646
import org.neo4j.driver.v1.Transaction;
47+
import org.neo4j.driver.v1.TransactionWork;
4748
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
4849
import org.neo4j.driver.v1.exceptions.SessionExpiredException;
4950
import org.neo4j.driver.v1.util.Function;
@@ -877,12 +878,12 @@ private static Driver newDriver( String uriString, DriverFactory driverFactory )
877878
return driverFactory.newInstance( uri, auth, routingConf, RetrySettings.DEFAULT, config );
878879
}
879880

880-
private static Function<Transaction,List<Record>> queryWork( final String query, final AtomicInteger invocations )
881+
private static TransactionWork<List<Record>> queryWork( final String query, final AtomicInteger invocations )
881882
{
882-
return new Function<Transaction,List<Record>>()
883+
return new TransactionWork<List<Record>>()
883884
{
884885
@Override
885-
public List<Record> apply( Transaction tx )
886+
public List<Record> execute( Transaction tx )
886887
{
887888
invocations.incrementAndGet();
888889
return tx.run( query ).list();

0 commit comments

Comments
 (0)