Search in sources :

Example 1 with CommitTransactionOperation

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

the class ClientSessionPublisherImpl method commitTransaction.

@Override
public Publisher<Void> commitTransaction() {
    if (transactionState == TransactionState.ABORTED) {
        throw new IllegalStateException("Cannot call commitTransaction after calling abortTransaction");
    }
    if (transactionState == TransactionState.NONE) {
        throw new IllegalStateException("There is no transaction started");
    }
    if (!messageSentInCurrentTransaction) {
        cleanupTransaction(TransactionState.COMMITTED);
        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");
        }
        boolean alreadyCommitted = commitInProgress || transactionState == TransactionState.COMMITTED;
        commitInProgress = true;
        return executor.execute(new CommitTransactionOperation(transactionOptions.getWriteConcern(), alreadyCommitted).recoveryToken(getRecoveryToken()).maxCommitTime(transactionOptions.getMaxCommitTime(MILLISECONDS), MILLISECONDS), readConcern, this).doOnTerminate(() -> {
            commitInProgress = false;
            transactionState = TransactionState.COMMITTED;
        }).doOnError(MongoException.class, this::clearTransactionContextOnError);
    }
}
Also used : MonoSink(reactor.core.publisher.MonoSink) CommitTransactionOperation(com.mongodb.internal.operation.CommitTransactionOperation) ReadConcern(com.mongodb.ReadConcern) MongoInternalException(com.mongodb.MongoInternalException)

Example 2 with CommitTransactionOperation

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

the class ClientSessionImpl method commitTransaction.

@Override
public void commitTransaction() {
    if (transactionState == TransactionState.ABORTED) {
        throw new IllegalStateException("Cannot call commitTransaction after calling abortTransaction");
    }
    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");
            }
            commitInProgress = true;
            delegate.getOperationExecutor().execute(new CommitTransactionOperation(transactionOptions.getWriteConcern(), transactionState == TransactionState.COMMITTED).recoveryToken(getRecoveryToken()).maxCommitTime(transactionOptions.getMaxCommitTime(MILLISECONDS), MILLISECONDS), readConcern, this);
        }
    } catch (MongoException e) {
        clearTransactionContextOnError(e);
        throw e;
    } finally {
        transactionState = TransactionState.COMMITTED;
        commitInProgress = false;
    }
}
Also used : CommitTransactionOperation(com.mongodb.internal.operation.CommitTransactionOperation) MongoException(com.mongodb.MongoException) ReadConcern(com.mongodb.ReadConcern) MongoInternalException(com.mongodb.MongoInternalException)

Aggregations

MongoInternalException (com.mongodb.MongoInternalException)2 ReadConcern (com.mongodb.ReadConcern)2 CommitTransactionOperation (com.mongodb.internal.operation.CommitTransactionOperation)2 MongoException (com.mongodb.MongoException)1 MonoSink (reactor.core.publisher.MonoSink)1