Skip to content

Small API updates #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions driver/src/main/java/org/neo4j/driver/internal/NetworkSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
import org.neo4j.driver.v1.Statement;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.TransactionWork;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.Values;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.types.TypeSystem;
import org.neo4j.driver.v1.util.Function;

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

Expand Down Expand Up @@ -182,13 +182,13 @@ public synchronized Transaction beginTransaction( String bookmark )
}

@Override
public <T> T readTransaction( Function<Transaction,T> work )
public <T> T readTransaction( TransactionWork<T> work )
{
return transaction( AccessMode.READ, work );
}

@Override
public <T> T writeTransaction( Function<Transaction,T> work )
public <T> T writeTransaction( TransactionWork<T> work )
{
return transaction( AccessMode.WRITE, work );
}
Expand Down Expand Up @@ -244,7 +244,7 @@ public synchronized void onConnectionError( boolean recoverable )
}
}

private synchronized <T> T transaction( AccessMode mode, Function<Transaction,T> work )
private synchronized <T> T transaction( AccessMode mode, TransactionWork<T> work )
{
RetryDecision decision = null;
List<Throwable> errors = null;
Expand All @@ -253,7 +253,21 @@ private synchronized <T> T transaction( AccessMode mode, Function<Transaction,T>
{
try ( Transaction tx = beginTransaction( mode ) )
{
return work.apply( tx );
T result;
try
{
result = work.execute( tx );
}
catch ( Throwable t )
{
// mark transaction for failure if the given unit of work threw exception
// this will override any success marks that were made by the unit of work
tx.failure();
throw t;
}
// given unit of work completed successfully, mark transaction for commit
tx.success();
return result;
}
catch ( Throwable newError )
{
Expand Down
10 changes: 5 additions & 5 deletions driver/src/main/java/org/neo4j/driver/v1/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
import org.neo4j.driver.v1.exceptions.SessionExpiredException;
import org.neo4j.driver.v1.exceptions.TransientException;
import org.neo4j.driver.v1.util.Function;
import org.neo4j.driver.v1.util.Immutable;
import org.neo4j.driver.v1.util.Resource;

Expand Down Expand Up @@ -449,10 +448,11 @@ public ConfigBuilder withConnectionTimeout( long value, TimeUnit unit )
}

/**
* Specify the maximum time transactions are allowed to retry via {@link Session#readTransaction(Function)} and
* {@link Session#writeTransaction(Function)} methods. These methods will retry the given unit of work on
* {@link ServiceUnavailableException}, {@link SessionExpiredException} and {@link TransientException} with
* exponential backoff using initial delay of 1 second.
* Specify the maximum time transactions are allowed to retry via
* {@link Session#readTransaction(TransactionWork)} and {@link Session#writeTransaction(TransactionWork)}
* methods. These methods will retry the given unit of work on {@link ServiceUnavailableException},
* {@link SessionExpiredException} and {@link TransientException} with exponential backoff using initial
* delay of 1 second.
* <p>
* Default value is 30 seconds.
*
Expand Down
15 changes: 10 additions & 5 deletions driver/src/main/java/org/neo4j/driver/v1/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.neo4j.driver.v1;

import org.neo4j.driver.v1.util.Function;
import org.neo4j.driver.v1.util.Resource;

/**
Expand Down Expand Up @@ -85,21 +84,27 @@ public interface Session extends Resource, StatementRunner

/**
* Execute given unit of work in a {@link AccessMode#READ read} transaction.
* <p>
* Transaction will automatically be committed unless exception is thrown from the unit of work itself or from
* {@link Transaction#close()} or transaction is explicitly marked for failure via {@link Transaction#failure()}.
*
* @param work the {@link Function} to be applied to a new read transaction.
* @param work the {@link TransactionWork} to be applied to a new read transaction.
* @param <T> the return type of the given unit of work.
* @return a result as returned by the given unit of work.
*/
<T> T readTransaction( Function<Transaction,T> work );
<T> T readTransaction( TransactionWork<T> work );

/**
* Execute given unit of work in a {@link AccessMode#WRITE write} transaction.
* <p>
* Transaction will automatically be committed unless exception is thrown from the unit of work itself or from
* {@link Transaction#close()} or transaction is explicitly marked for failure via {@link Transaction#failure()}.
*
* @param work the {@link Function} to be applied to a new write transaction.
* @param work the {@link TransactionWork} to be applied to a new write transaction.
* @param <T> the return type of the given unit of work.
* @return a result as returned by the given unit of work.
*/
<T> T writeTransaction( Function<Transaction,T> work );
<T> T writeTransaction( TransactionWork<T> work );

/**
* Return the bookmark received following the last completed
Expand Down
37 changes: 37 additions & 0 deletions driver/src/main/java/org/neo4j/driver/v1/TransactionWork.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.v1;

/**
* Callback that executes operations against a given {@link Transaction}.
* To be used with {@link Session#readTransaction(TransactionWork)} and
* {@link Session#writeTransaction(TransactionWork)} methods.
*
* @param <T> the return type of this work.
*/
public interface TransactionWork<T>
{
/**
* Executes all given operations against the same transaction.
*
* @param tx the transaction to use.
* @return some result object or {@code null} if none.
*/
T execute( Transaction tx );
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
import org.neo4j.driver.v1.AccessMode;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.TransactionWork;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
import org.neo4j.driver.v1.exceptions.SessionExpiredException;
import org.neo4j.driver.v1.util.Function;

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

Function<Transaction,Integer> work = new Function<Transaction,Integer>()
TransactionWork<Integer> work = new TransactionWork<Integer>()
{
@Override
public Integer apply( Transaction tx )
public Integer execute( Transaction tx )
{
tx.success();
return 42;
Expand All @@ -789,10 +789,10 @@ private static void testTxCommitOrRollback( AccessMode transactionMode, final bo
when( connectionProvider.acquireConnection( transactionMode ) ).thenReturn( connection );
NetworkSession session = newSession( connectionProvider, WRITE );

Function<Transaction,Integer> work = new Function<Transaction,Integer>()
TransactionWork<Integer> work = new TransactionWork<Integer>()
{
@Override
public Integer apply( Transaction tx )
public Integer execute( Transaction tx )
{
if ( commit )
{
Expand Down Expand Up @@ -831,10 +831,10 @@ private static void testTxRollbackWhenThrows( AccessMode transactionMode )
NetworkSession session = newSession( connectionProvider, WRITE );

final RuntimeException error = new IllegalStateException( "Oh!" );
Function<Transaction,Void> work = new Function<Transaction,Void>()
TransactionWork<Void> work = new TransactionWork<Void>()
{
@Override
public Void apply( Transaction tx )
public Void execute( Transaction tx )
{
throw error;
}
Expand Down Expand Up @@ -864,12 +864,12 @@ private static void testTxIsRetriedUntilSuccessWhenFunctionThrows( AccessMode mo
when( connectionProvider.acquireConnection( mode ) ).thenReturn( connection );
NetworkSession session = newSession( connectionProvider, retryLogic );

int answer = executeTransaction( session, mode, new Function<Transaction,Integer>()
int answer = executeTransaction( session, mode, new TransactionWork<Integer>()
{
int invoked;

@Override
public Integer apply( Transaction tx )
public Integer execute( Transaction tx )
{
if ( invoked++ < failures )
{
Expand All @@ -896,10 +896,10 @@ private static void testTxIsRetriedUntilSuccessWhenTxCloseThrows( AccessMode mod
when( connectionProvider.acquireConnection( mode ) ).thenReturn( connection );
NetworkSession session = newSession( connectionProvider, retryLogic );

int answer = executeTransaction( session, mode, new Function<Transaction,Integer>()
int answer = executeTransaction( session, mode, new TransactionWork<Integer>()
{
@Override
public Integer apply( Transaction tx )
public Integer execute( Transaction tx )
{
tx.success();
return 43;
Expand All @@ -925,12 +925,12 @@ private static void testTxIsRetriedUntilFailureWhenFunctionThrows( AccessMode mo

try
{
executeTransaction( session, mode, new Function<Transaction,Integer>()
executeTransaction( session, mode, new TransactionWork<Integer>()
{
int invoked;

@Override
public Integer apply( Transaction tx )
public Integer execute( Transaction tx )
{
if ( invoked++ < failures )
{
Expand Down Expand Up @@ -963,10 +963,10 @@ private static void testTxIsRetriedUntilFailureWhenTxCloseThrows( AccessMode mod

try
{
executeTransaction( session, mode, new Function<Transaction,Integer>()
executeTransaction( session, mode, new TransactionWork<Integer>()
{
@Override
public Integer apply( Transaction tx )
public Integer execute( Transaction tx )
{
tx.success();
return 42;
Expand All @@ -992,12 +992,12 @@ private static void testRetryErrorsAreCombined( AccessMode mode )

try
{
executeTransaction( session, mode, new Function<Transaction,Integer>()
executeTransaction( session, mode, new TransactionWork<Integer>()
{
int invoked;

@Override
public Integer apply( Transaction tx )
public Integer execute( Transaction tx )
{
if ( invoked++ < failures )
{
Expand Down Expand Up @@ -1035,12 +1035,12 @@ private static void testRetryErrorsAreNotCombinedWhenSameErrorIsThrown( AccessMo
final ServiceUnavailableException error = new ServiceUnavailableException( "Oh!" );
try
{
executeTransaction( session, mode, new Function<Transaction,Integer>()
executeTransaction( session, mode, new TransactionWork<Integer>()
{
int invoked;

@Override
public Integer apply( Transaction tx )
public Integer execute( Transaction tx )
{
if ( invoked++ < failures )
{
Expand All @@ -1060,7 +1060,7 @@ public Integer apply( Transaction tx )
}
}

private static <T> T executeTransaction( Session session, AccessMode mode, Function<Transaction,T> work )
private static <T> T executeTransaction( Session session, AccessMode mode, TransactionWork<T> work )
{
if ( mode == READ )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.TransactionWork;
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
import org.neo4j.driver.v1.exceptions.SessionExpiredException;
import org.neo4j.driver.v1.util.Function;
Expand Down Expand Up @@ -877,12 +878,12 @@ private static Driver newDriver( String uriString, DriverFactory driverFactory )
return driverFactory.newInstance( uri, auth, routingConf, RetrySettings.DEFAULT, config );
}

private static Function<Transaction,List<Record>> queryWork( final String query, final AtomicInteger invocations )
private static TransactionWork<List<Record>> queryWork( final String query, final AtomicInteger invocations )
{
return new Function<Transaction,List<Record>>()
return new TransactionWork<List<Record>>()
{
@Override
public List<Record> apply( Transaction tx )
public List<Record> execute( Transaction tx )
{
invocations.incrementAndGet();
return tx.run( query ).list();
Expand Down
Loading