Search in sources :

Example 1 with AbortTransactionOperation

use of com.mongodb.internal.operation.AbortTransactionOperation in project mongo-java-driver by mongodb.

the class ClientSessionImpl method abortTransaction.

@Override
public void abortTransaction() {
    if (transactionState == TransactionState.ABORTED) {
        throw new IllegalStateException("Cannot call abortTransaction twice");
    }
    if (transactionState == TransactionState.COMMITTED) {
        throw new IllegalStateException("Cannot call abortTransaction after calling commitTransaction");
    }
    if (transactionState == TransactionState.NONE) {
        throw new IllegalStateException("There is no transaction started");
    }
    try {
        if (messageSentInCurrentTransaction) {
            ReadConcern readConcern = transactionOptions.getReadConcern();
            if (readConcern == null) {
                throw new MongoInternalException("Invariant violated.  Transaction options read concern can not be null");
            }
            delegate.getOperationExecutor().execute(new AbortTransactionOperation(transactionOptions.getWriteConcern()).recoveryToken(getRecoveryToken()), readConcern, this);
        }
    } catch (Exception e) {
    // ignore exceptions from abort
    } finally {
        clearTransactionContext();
        cleanupTransaction(TransactionState.ABORTED);
    }
}
Also used : ReadConcern(com.mongodb.ReadConcern) AbortTransactionOperation(com.mongodb.internal.operation.AbortTransactionOperation) MongoInternalException(com.mongodb.MongoInternalException) MongoClientException(com.mongodb.MongoClientException) MongoException(com.mongodb.MongoException) MongoExecutionTimeoutException(com.mongodb.MongoExecutionTimeoutException) MongoInternalException(com.mongodb.MongoInternalException)

Example 2 with AbortTransactionOperation

use of com.mongodb.internal.operation.AbortTransactionOperation in project mongo-java-driver by mongodb.

the class ClientSessionPublisherImpl method abortTransaction.

@Override
public Publisher<Void> abortTransaction() {
    if (transactionState == TransactionState.ABORTED) {
        throw new IllegalStateException("Cannot call abortTransaction twice");
    }
    if (transactionState == TransactionState.COMMITTED) {
        throw new IllegalStateException("Cannot call abortTransaction after calling commitTransaction");
    }
    if (transactionState == TransactionState.NONE) {
        throw new IllegalStateException("There is no transaction started");
    }
    if (!messageSentInCurrentTransaction) {
        cleanupTransaction(TransactionState.ABORTED);
        return Mono.create(MonoSink::success);
    } else {
        ReadConcern readConcern = transactionOptions.getReadConcern();
        if (readConcern == null) {
            throw new MongoInternalException("Invariant violated. Transaction options read concern can not be null");
        }
        return executor.execute(new AbortTransactionOperation(transactionOptions.getWriteConcern()).recoveryToken(getRecoveryToken()), readConcern, this).onErrorResume(Throwable.class, (e) -> Mono.empty()).doOnTerminate(() -> {
            clearTransactionContext();
            cleanupTransaction(TransactionState.ABORTED);
        });
    }
}
Also used : MonoSink(reactor.core.publisher.MonoSink) MongoInternalException(com.mongodb.MongoInternalException) MongoClientException(com.mongodb.MongoClientException) MonoSink(reactor.core.publisher.MonoSink) AsyncClientSession(com.mongodb.internal.async.client.AsyncClientSession) MongoClient(com.mongodb.reactivestreams.client.MongoClient) TransactionOptions(com.mongodb.TransactionOptions) TRANSIENT_TRANSACTION_ERROR_LABEL(com.mongodb.MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL) BaseClientSessionImpl(com.mongodb.internal.session.BaseClientSessionImpl) Assertions.isTrue(com.mongodb.assertions.Assertions.isTrue) ServerSessionPool(com.mongodb.internal.session.ServerSessionPool) ReadConcern(com.mongodb.ReadConcern) CommitTransactionOperation(com.mongodb.internal.operation.CommitTransactionOperation) MongoException(com.mongodb.MongoException) AsyncWriteOperation(com.mongodb.internal.operation.AsyncWriteOperation) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ClientSession(com.mongodb.reactivestreams.client.ClientSession) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) ClientSessionOptions(com.mongodb.ClientSessionOptions) Assertions.assertTrue(com.mongodb.assertions.Assertions.assertTrue) AsyncReadOperation(com.mongodb.internal.operation.AsyncReadOperation) UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL(com.mongodb.MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL) AbortTransactionOperation(com.mongodb.internal.operation.AbortTransactionOperation) WriteConcern(com.mongodb.WriteConcern) ReadConcern(com.mongodb.ReadConcern) AbortTransactionOperation(com.mongodb.internal.operation.AbortTransactionOperation) MongoInternalException(com.mongodb.MongoInternalException)

Aggregations

MongoClientException (com.mongodb.MongoClientException)2 MongoException (com.mongodb.MongoException)2 MongoInternalException (com.mongodb.MongoInternalException)2 ReadConcern (com.mongodb.ReadConcern)2 AbortTransactionOperation (com.mongodb.internal.operation.AbortTransactionOperation)2 ClientSessionOptions (com.mongodb.ClientSessionOptions)1 TRANSIENT_TRANSACTION_ERROR_LABEL (com.mongodb.MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL)1 UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL (com.mongodb.MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)1 MongoExecutionTimeoutException (com.mongodb.MongoExecutionTimeoutException)1 TransactionOptions (com.mongodb.TransactionOptions)1 WriteConcern (com.mongodb.WriteConcern)1 Assertions.assertTrue (com.mongodb.assertions.Assertions.assertTrue)1 Assertions.isTrue (com.mongodb.assertions.Assertions.isTrue)1 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)1 SingleResultCallback (com.mongodb.internal.async.SingleResultCallback)1 AsyncClientSession (com.mongodb.internal.async.client.AsyncClientSession)1 AsyncReadOperation (com.mongodb.internal.operation.AsyncReadOperation)1 AsyncWriteOperation (com.mongodb.internal.operation.AsyncWriteOperation)1 CommitTransactionOperation (com.mongodb.internal.operation.CommitTransactionOperation)1 BaseClientSessionImpl (com.mongodb.internal.session.BaseClientSessionImpl)1