Search in sources :

Example 11 with TransactionalOperator

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

the class R2dbcTransactionManagerUnitTests method testPropagationSupportsAndRequiresNew.

@Test
void testPropagationSupportsAndRequiresNew() {
    when(connectionMock.commitTransaction()).thenReturn(Mono.empty());
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    TransactionalOperator operator = TransactionalOperator.create(tm, definition);
    operator.execute(tx1 -> {
        assertThat(tx1.isNewTransaction()).isFalse();
        DefaultTransactionDefinition innerDef = new DefaultTransactionDefinition();
        innerDef.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionalOperator inner = TransactionalOperator.create(tm, innerDef);
        return inner.execute(tx2 -> {
            assertThat(tx2.isNewTransaction()).isTrue();
            return Mono.empty();
        });
    }).as(StepVerifier::create).verifyComplete();
    verify(connectionMock).commitTransaction();
    verify(connectionMock).close();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

Example 12 with TransactionalOperator

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

the class R2dbcTransactionManagerUnitTests method restoresAutoCommit.

@Test
void restoresAutoCommit() {
    when(connectionMock.isAutoCommit()).thenReturn(true);
    when(connectionMock.setAutoCommit(anyBoolean())).thenReturn(Mono.empty());
    when(connectionMock.commitTransaction()).thenReturn(Mono.empty());
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    TransactionalOperator operator = TransactionalOperator.create(tm, definition);
    ConnectionFactoryUtils.getConnection(connectionFactoryMock).as(operator::transactional).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    verify(connectionMock).beginTransaction();
    verify(connectionMock).setAutoCommit(false);
    verify(connectionMock).setAutoCommit(true);
    verify(connectionMock).commitTransaction();
    verify(connectionMock).close();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 13 with TransactionalOperator

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

the class ReactiveMongoTransactionManagerUnitTests method readonlyShouldInitiateASessionStartAndCommitTransaction.

// DATAMONGO-2265
@Test
void readonlyShouldInitiateASessionStartAndCommitTransaction() {
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    ReactiveMongoTemplate template = new ReactiveMongoTemplate(databaseFactory);
    when(session.commitTransaction()).thenReturn(Mono.empty());
    DefaultTransactionDefinition readonlyTxDefinition = new DefaultTransactionDefinition();
    readonlyTxDefinition.setReadOnly(true);
    TransactionalOperator operator = TransactionalOperator.create(txManager, readonlyTxDefinition);
    template.execute(db -> {
        db.drop();
        return Mono.empty();
    }).as(// 
    operator::transactional).as(// 
    StepVerifier::create).verifyComplete();
    verify(databaseFactory).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 14 with TransactionalOperator

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

the class ReactiveMongoTransactionManagerUnitTests method triggerCommitCorrectly.

// DATAMONGO-2265
@Test
void triggerCommitCorrectly() {
    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(// 
    operator::transactional).as(// 
    StepVerifier::create).verifyComplete();
    verify(databaseFactory).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 15 with TransactionalOperator

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

the class ReactiveMongoTransactionManagerUnitTests method participateInOnGoingTransactionWithRollbackOnly.

// DATAMONGO-2265
@Test
void participateInOnGoingTransactionWithRollbackOnly() {
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    ReactiveMongoTemplate template = new ReactiveMongoTemplate(databaseFactory);
    when(session.abortTransaction()).thenReturn(Mono.empty());
    TransactionalOperator operator = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
    operator.execute(tx -> {
        return template.execute(db -> {
            db.drop();
            tx.setRollbackOnly();
            return Mono.empty();
        });
    }).as(StepVerifier::create).verifyComplete();
    verify(databaseFactory, times(1)).withSession(eq(session));
    verify(session).startTransaction();
    verify(session).abortTransaction();
    verify(session).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)

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