org.neo4j.driver.internal.reactive.AbstractReactiveSession Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neo4j-java-driver Show documentation
Show all versions of neo4j-java-driver Show documentation
Access to the Neo4j graph database through Java
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.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.internal.reactive;
import static org.neo4j.driver.internal.reactive.RxUtils.createEmptyPublisher;
import static org.neo4j.driver.internal.reactive.RxUtils.createSingleItemPublisher;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import org.neo4j.driver.AccessMode;
import org.neo4j.driver.Bookmark;
import org.neo4j.driver.TransactionConfig;
import org.neo4j.driver.exceptions.TransactionNestingException;
import org.neo4j.driver.internal.async.NetworkSession;
import org.neo4j.driver.internal.async.UnmanagedTransaction;
import org.neo4j.driver.internal.util.Futures;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
public abstract class AbstractReactiveSession {
protected final NetworkSession session;
public AbstractReactiveSession(NetworkSession session) {
// RxSession accept a network session as input.
// The network session different from async session that it provides ways to both run for Rx and Async
// Note: Blocking result could just build on top of async result. However, Rx result cannot just build on top of
// async result.
this.session = session;
}
protected abstract S createTransaction(UnmanagedTransaction unmanagedTransaction);
protected abstract Publisher closeTransaction(S transaction, boolean commit);
Publisher doBeginTransaction(TransactionConfig config) {
return doBeginTransaction(config, null);
}
protected Publisher doBeginTransaction(TransactionConfig config, String txType) {
return createSingleItemPublisher(
() -> {
CompletableFuture txFuture = new CompletableFuture<>();
session.beginTransactionAsync(config, txType).whenComplete((tx, completionError) -> {
if (tx != null) {
txFuture.complete(createTransaction(tx));
} else {
releaseConnectionBeforeReturning(txFuture, completionError);
}
});
return txFuture;
},
() -> new IllegalStateException(
"Unexpected condition, begin transaction call has completed successfully with transaction being null"));
}
Publisher beginTransaction(AccessMode mode, TransactionConfig config) {
return createSingleItemPublisher(
() -> {
CompletableFuture txFuture = new CompletableFuture<>();
session.beginTransactionAsync(mode, config).whenComplete((tx, completionError) -> {
if (tx != null) {
txFuture.complete(createTransaction(tx));
} else {
releaseConnectionBeforeReturning(txFuture, completionError);
}
});
return txFuture;
},
() -> new IllegalStateException(
"Unexpected condition, begin transaction call has completed successfully with transaction being null"));
}
protected Publisher runTransaction(
AccessMode mode, Function> work, TransactionConfig config) {
Flux repeatableWork = Flux.usingWhen(
beginTransaction(mode, config),
work,
tx -> closeTransaction(tx, true),
(tx, error) -> closeTransaction(tx, false),
(tx) -> closeTransaction(tx, false));
return session.retryLogic().retryRx(repeatableWork);
}
private void releaseConnectionBeforeReturning(CompletableFuture returnFuture, Throwable completionError) {
// We failed to create a result cursor, so we cannot rely on result cursor to clean-up resources.
// Therefore, we will first release the connection that might have been created in the session and then notify
// the error.
// The logic here shall be the same as `SessionPullResponseHandler#afterFailure`.
// The reason we need to release connection in session is that we made `rxSession.close()` optional;
// Otherwise, session.close shall handle everything for us.
Throwable error = Futures.completionExceptionCause(completionError);
if (error instanceof TransactionNestingException) {
returnFuture.completeExceptionally(error);
} else {
session.releaseConnectionAsync()
.whenComplete((ignored, closeError) ->
returnFuture.completeExceptionally(Futures.combineErrors(error, closeError)));
}
}
public Set lastBookmarks() {
return session.lastBookmarks();
}
protected Publisher doClose() {
return createEmptyPublisher(session::closeAsync);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy