Search in sources :

Example 16 with TransactionalOperator

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

the class ReactiveMongoTransactionManagerUnitTests method participateInOnGoingTransactionWithCommit.

// DATAMONGO-2265
@Test
void participateInOnGoingTransactionWithCommit() {
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    ReactiveMongoTemplate template = new ReactiveMongoTemplate(databaseFactory);
    when(session.commitTransaction()).thenReturn(Mono.empty());
    TransactionalOperator operator = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
    template.execute(db -> {
        db.drop();
        return Mono.empty();
    }).as(StepVerifier::create).verifyComplete();
    template.execute(db -> {
        db.drop();
        return Mono.empty();
    }).as(// 
    operator::transactional).as(// 
    StepVerifier::create).verifyComplete();
    verify(databaseFactory, times(1)).withSession(eq(session));
    verify(session).startTransaction();
    verify(session).commitTransaction();
    verify(session).close();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) StepVerifier(reactor.test.StepVerifier) Mock(org.mockito.Mock) TransactionDefinition(org.springframework.transaction.TransactionDefinition) 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) Mockito(org.mockito.Mockito) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

Example 17 with TransactionalOperator

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

the class ReactiveMongoTransactionManagerUnitTests method suspendTransactionWhilePropagationRequiresNew.

// DATAMONGO-2265
@Test
void suspendTransactionWhilePropagationRequiresNew() {
    when(databaseFactory.withSession(session2)).thenReturn(databaseFactory2);
    when(databaseFactory2.getMongoDatabase()).thenReturn(Mono.just(db2));
    when(session2.getServerSession()).thenReturn(serverSession);
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    ReactiveMongoTemplate template = new ReactiveMongoTemplate(databaseFactory);
    when(session.commitTransaction()).thenReturn(Mono.empty());
    when(session2.commitTransaction()).thenReturn(Mono.empty());
    TransactionalOperator outer = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    TransactionalOperator inner = TransactionalOperator.create(txManager, definition);
    outer.execute(tx1 -> {
        return template.execute(db -> {
            db.drop();
            return inner.execute(tx2 -> {
                return template.execute(db2 -> {
                    db2.drop();
                    return Mono.empty();
                });
            });
        });
    }).as(StepVerifier::create).verifyComplete();
    verify(session).startTransaction();
    verify(session2).startTransaction();
    verify(databaseFactory, times(1)).withSession(eq(session));
    verify(databaseFactory).withSession(eq(session2));
    verify(db).drop();
    verify(db2).drop();
    verify(session).close();
    verify(session2).close();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

Example 18 with TransactionalOperator

use of org.springframework.transaction.reactive.TransactionalOperator 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)

Example 19 with TransactionalOperator

use of org.springframework.transaction.reactive.TransactionalOperator in project spring-framework by spring-projects.

the class R2dbcTransactionManagerUnitTests method testPropagationNeverWithExistingTransaction.

@Test
void testPropagationNeverWithExistingTransaction() {
    when(connectionMock.rollbackTransaction()).thenReturn(Mono.empty());
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    TransactionalOperator operator = TransactionalOperator.create(tm, definition);
    operator.execute(tx1 -> {
        assertThat(tx1.isNewTransaction()).isTrue();
        definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
        return operator.execute(tx2 -> {
            fail("Should have thrown IllegalTransactionStateException");
            return Mono.empty();
        });
    }).as(StepVerifier::create).verifyError(IllegalTransactionStateException.class);
    verify(connectionMock).rollbackTransaction();
    verify(connectionMock).close();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

Example 20 with TransactionalOperator

use of org.springframework.transaction.reactive.TransactionalOperator in project spring-framework by spring-projects.

the class R2dbcTransactionManagerUnitTests method doesNotSetIsolationLevelIfMatch.

@Test
void doesNotSetIsolationLevelIfMatch() {
    when(connectionMock.getTransactionIsolationLevel()).thenReturn(IsolationLevel.READ_COMMITTED);
    when(connectionMock.commitTransaction()).thenReturn(Mono.empty());
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
    TransactionalOperator operator = TransactionalOperator.create(tm, definition);
    ConnectionFactoryUtils.getConnection(connectionFactoryMock).as(operator::transactional).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    verify(connectionMock).beginTransaction();
    verify(connectionMock, never()).setTransactionIsolationLevel(any());
    verify(connectionMock).commitTransaction();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionalOperator (org.springframework.transaction.reactive.TransactionalOperator)30 Test (org.junit.jupiter.api.Test)25 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)23 StepVerifier (reactor.test.StepVerifier)20 Mono (reactor.core.publisher.Mono)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)8 TransactionDefinition (org.springframework.transaction.TransactionDefinition)7 TransactionSynchronizationManager (org.springframework.transaction.reactive.TransactionSynchronizationManager)7 Connection (io.r2dbc.spi.Connection)6 ConnectionFactory (io.r2dbc.spi.ConnectionFactory)6 R2dbcBadGrammarException (io.r2dbc.spi.R2dbcBadGrammarException)6 Statement (io.r2dbc.spi.Statement)6 ClientSession (com.mongodb.reactivestreams.client.ClientSession)5 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)5 ServerSession (com.mongodb.session.ServerSession)5 IsolationLevel (io.r2dbc.spi.IsolationLevel)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Assertions.fail (org.assertj.core.api.Assertions.fail)5