Search in sources :

Example 1 with ServerSession

use of com.mongodb.session.ServerSession in project spring-data-mongodb by spring-projects.

the class MongoTransactionManagerUnitTests method suspendTransactionWhilePropagationRequiresNew.

// DATAMONGO-1920
@Test
void suspendTransactionWhilePropagationRequiresNew() {
    when(dbFactory.withSession(session2)).thenReturn(dbFactory2);
    when(dbFactory2.getMongoDatabase()).thenReturn(db2);
    when(session2.getServerSession()).thenReturn(serverSession);
    when(serverSession.isClosed()).thenReturn(false);
    MongoTransactionManager txManager = new MongoTransactionManager(dbFactory);
    TransactionStatus txStatus = txManager.getTransaction(new DefaultTransactionDefinition());
    MongoTemplate template = new MongoTemplate(dbFactory);
    template.execute(db -> {
        db.drop();
        return null;
    });
    TransactionTemplate txTemplate = new TransactionTemplate(txManager);
    txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    txTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            template.execute(db -> {
                db.drop();
                return null;
            });
        }
    });
    template.execute(MongoDatabase::listCollections);
    txManager.commit(txStatus);
    verify(session).startTransaction();
    verify(session2).startTransaction();
    verify(dbFactory, times(2)).withSession(eq(session));
    verify(dbFactory).withSession(eq(session2));
    verify(db).drop();
    verify(db2).drop();
    verify(db).listCollections();
    verify(session).close();
    verify(session2).close();
}
Also used : Strictness(org.mockito.quality.Strictness) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) Mock(org.mockito.Mock) TransactionDefinition(org.springframework.transaction.TransactionDefinition) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) MongoDatabase(com.mongodb.client.MongoDatabase) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) ServerSession(com.mongodb.session.ServerSession) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MongoExceptionTranslator(org.springframework.data.mongodb.core.MongoExceptionTranslator) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) Assertions(org.assertj.core.api.Assertions) ClientSession(com.mongodb.client.ClientSession) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.jupiter.api.Test)

Example 2 with ServerSession

use of com.mongodb.session.ServerSession in project spring-data-mongodb by spring-projects.

the class ReactiveMongoDatabaseUtilsUnitTests method shouldParticipateInOngoingMongoTransactionWhenSessionSychronizationIsNative.

// DATAMONGO-2265
@Test
void shouldParticipateInOngoingMongoTransactionWhenSessionSychronizationIsNative() {
    when(session.getServerSession()).thenReturn(serverSession);
    when(databaseFactory.getSession(any())).thenReturn(Mono.just(session));
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    when(session.abortTransaction()).thenReturn(Mono.empty());
    TransactionalOperator operator = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
    operator.execute(tx -> {
        return TransactionSynchronizationManager.forCurrentTransaction().doOnNext(synchronizationManager -> {
            assertThat(synchronizationManager.isSynchronizationActive()).isTrue();
            assertThat(tx.isNewTransaction()).isTrue();
            assertThat(synchronizationManager.hasResource(databaseFactory)).isTrue();
        }).then(Mono.fromRunnable(tx::setRollbackOnly));
    }).as(StepVerifier::create).verifyComplete();
    verify(session).startTransaction();
    verify(session).abortTransaction();
    verify(session).close();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ArgumentMatchers(org.mockito.ArgumentMatchers) StepVerifier(reactor.test.StepVerifier) Mock(org.mockito.Mock) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Mono(reactor.core.publisher.Mono) ClientSession(com.mongodb.reactivestreams.client.ClientSession) ServerSession(com.mongodb.session.ServerSession) Test(org.junit.jupiter.api.Test) TransactionSynchronizationManager(org.springframework.transaction.reactive.TransactionSynchronizationManager) Mockito(org.mockito.Mockito) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Assertions(org.assertj.core.api.Assertions) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with ServerSession

use of com.mongodb.session.ServerSession in project spring-data-mongodb by spring-projects.

the class ReactiveMongoDatabaseUtilsUnitTests method isTransactionActiveShouldLookupTxForActiveTransactionSynchronizationViaTxManager.

// DATAMONGO-2265
@Test
void isTransactionActiveShouldLookupTxForActiveTransactionSynchronizationViaTxManager() {
    when(session.getServerSession()).thenReturn(serverSession);
    when(session.hasActiveTransaction()).thenReturn(true);
    when(databaseFactory.getSession(any())).thenReturn(Mono.just(session));
    when(databaseFactory.isTransactionActive()).thenReturn(false);
    when(session.commitTransaction()).thenReturn(Mono.empty());
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    TransactionalOperator operator = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
    operator.execute(tx -> {
        return ReactiveMongoDatabaseUtils.isTransactionActive(databaseFactory);
    }).as(StepVerifier::create).expectNext(true).verifyComplete();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ArgumentMatchers(org.mockito.ArgumentMatchers) StepVerifier(reactor.test.StepVerifier) Mock(org.mockito.Mock) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Mono(reactor.core.publisher.Mono) ClientSession(com.mongodb.reactivestreams.client.ClientSession) ServerSession(com.mongodb.session.ServerSession) Test(org.junit.jupiter.api.Test) TransactionSynchronizationManager(org.springframework.transaction.reactive.TransactionSynchronizationManager) Mockito(org.mockito.Mockito) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Assertions(org.assertj.core.api.Assertions) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Aggregations

ServerSession (com.mongodb.session.ServerSession)3 Assertions (org.assertj.core.api.Assertions)3 Test (org.junit.jupiter.api.Test)3 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)3 Mock (org.mockito.Mock)3 Mockito (org.mockito.Mockito)3 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)3 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)3 ClientSession (com.mongodb.reactivestreams.client.ClientSession)2 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)2 ArgumentMatchers (org.mockito.ArgumentMatchers)2 TransactionSynchronizationManager (org.springframework.transaction.reactive.TransactionSynchronizationManager)2 TransactionalOperator (org.springframework.transaction.reactive.TransactionalOperator)2 Mono (reactor.core.publisher.Mono)2 StepVerifier (reactor.test.StepVerifier)2 ClientSession (com.mongodb.client.ClientSession)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 AfterEach (org.junit.jupiter.api.AfterEach)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 MockitoSettings (org.mockito.junit.jupiter.MockitoSettings)1