Search in sources :

Example 1 with TransactionSynchronizationManager

use of org.springframework.transaction.reactive.TransactionSynchronizationManager in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTransactionManager method doBegin.

/*
	 * (non-Javadoc)
	 * @see org.springframework.transaction.reactive.AbstractReactiveTransactionManager#doBegin(org.springframework.transaction.reactive.TransactionSynchronizationManager, java.lang.Object, org.springframework.transaction.TransactionDefinition)
	 */
@Override
protected Mono<Void> doBegin(TransactionSynchronizationManager synchronizationManager, Object transaction, TransactionDefinition definition) throws TransactionException {
    return Mono.defer(() -> {
        ReactiveMongoTransactionObject mongoTransactionObject = extractMongoTransaction(transaction);
        Mono<ReactiveMongoResourceHolder> holder = newResourceHolder(definition, ClientSessionOptions.builder().causallyConsistent(true).build());
        return holder.doOnNext(resourceHolder -> {
            mongoTransactionObject.setResourceHolder(resourceHolder);
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("About to start transaction for session %s.", debugString(resourceHolder.getSession())));
            }
        }).doOnNext(resourceHolder -> {
            mongoTransactionObject.startTransaction(options);
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("Started transaction for session %s.", debugString(resourceHolder.getSession())));
            }
        }).onErrorMap(ex -> new TransactionSystemException(String.format("Could not start Mongo transaction for session %s.", debugString(mongoTransactionObject.getSession())), ex)).doOnSuccess(resourceHolder -> {
            synchronizationManager.bindResource(getRequiredDatabaseFactory(), resourceHolder);
        }).then();
    });
}
Also used : ClassUtils(org.springframework.util.ClassUtils) TransactionDefinition(org.springframework.transaction.TransactionDefinition) MongoException(com.mongodb.MongoException) Mono(reactor.core.publisher.Mono) AbstractReactiveTransactionManager(org.springframework.transaction.reactive.AbstractReactiveTransactionManager) ClientSession(com.mongodb.reactivestreams.client.ClientSession) InitializingBean(org.springframework.beans.factory.InitializingBean) TransactionSynchronizationManager(org.springframework.transaction.reactive.TransactionSynchronizationManager) TransactionOptions(com.mongodb.TransactionOptions) SmartTransactionObject(org.springframework.transaction.support.SmartTransactionObject) TransactionSystemException(org.springframework.transaction.TransactionSystemException) GenericReactiveTransaction(org.springframework.transaction.reactive.GenericReactiveTransaction) ClientSessionOptions(com.mongodb.ClientSessionOptions) TransactionException(org.springframework.transaction.TransactionException) Nullable(org.springframework.lang.Nullable) Assert(org.springframework.util.Assert) TransactionSystemException(org.springframework.transaction.TransactionSystemException)

Aggregations

ClientSessionOptions (com.mongodb.ClientSessionOptions)1 MongoException (com.mongodb.MongoException)1 TransactionOptions (com.mongodb.TransactionOptions)1 ClientSession (com.mongodb.reactivestreams.client.ClientSession)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1 Nullable (org.springframework.lang.Nullable)1 TransactionDefinition (org.springframework.transaction.TransactionDefinition)1 TransactionException (org.springframework.transaction.TransactionException)1 TransactionSystemException (org.springframework.transaction.TransactionSystemException)1 AbstractReactiveTransactionManager (org.springframework.transaction.reactive.AbstractReactiveTransactionManager)1 GenericReactiveTransaction (org.springframework.transaction.reactive.GenericReactiveTransaction)1 TransactionSynchronizationManager (org.springframework.transaction.reactive.TransactionSynchronizationManager)1 SmartTransactionObject (org.springframework.transaction.support.SmartTransactionObject)1 Assert (org.springframework.util.Assert)1 ClassUtils (org.springframework.util.ClassUtils)1 Mono (reactor.core.publisher.Mono)1