Search in sources :

Example 6 with TransactionalOperator

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

the class TransactionAutoConfigurationTests method whenThereIsASingleReactiveTransactionManagerATransactionalOperatorIsAutoConfigured.

@Test
void whenThereIsASingleReactiveTransactionManagerATransactionalOperatorIsAutoConfigured() {
    this.contextRunner.withUserConfiguration(SingleReactiveTransactionManagerConfiguration.class).run((context) -> {
        ReactiveTransactionManager transactionManager = context.getBean(ReactiveTransactionManager.class);
        TransactionalOperator transactionalOperator = context.getBean(TransactionalOperator.class);
        assertThat(transactionalOperator).extracting("transactionManager").isSameAs(transactionManager);
    });
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) Test(org.junit.jupiter.api.Test)

Example 7 with TransactionalOperator

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

the class R2dbcTransactionManagerUnitTests method testSimpleTransaction.

@Test
void testSimpleTransaction() {
    TestTransactionSynchronization sync = new TestTransactionSynchronization(TransactionSynchronization.STATUS_COMMITTED);
    AtomicInteger commits = new AtomicInteger();
    when(connectionMock.commitTransaction()).thenReturn(Mono.fromRunnable(commits::incrementAndGet));
    TransactionalOperator operator = TransactionalOperator.create(tm);
    ConnectionFactoryUtils.getConnection(connectionFactoryMock).flatMap(connection -> TransactionSynchronizationManager.forCurrentTransaction().doOnNext(synchronizationManager -> synchronizationManager.registerSynchronization(sync))).as(operator::transactional).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    assertThat(commits).hasValue(1);
    verify(connectionMock).isAutoCommit();
    verify(connectionMock).beginTransaction();
    verify(connectionMock).commitTransaction();
    verify(connectionMock).close();
    verifyNoMoreInteractions(connectionMock);
    assertThat(sync.beforeCommitCalled).isTrue();
    assertThat(sync.afterCommitCalled).isTrue();
    assertThat(sync.beforeCompletionCalled).isTrue();
    assertThat(sync.afterCompletionCalled).isTrue();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) BDDMockito.when(org.mockito.BDDMockito.when) BDDMockito.verify(org.mockito.BDDMockito.verify) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionSynchronization(org.springframework.transaction.reactive.TransactionSynchronization) BDDMockito.never(org.mockito.BDDMockito.never) R2dbcBadGrammarException(io.r2dbc.spi.R2dbcBadGrammarException) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Mono(reactor.core.publisher.Mono) BDDMockito.verifyNoMoreInteractions(org.mockito.BDDMockito.verifyNoMoreInteractions) Connection(io.r2dbc.spi.Connection) Test(org.junit.jupiter.api.Test) TransactionSynchronizationManager(org.springframework.transaction.reactive.TransactionSynchronizationManager) IsolationLevel(io.r2dbc.spi.IsolationLevel) Assertions.fail(org.assertj.core.api.Assertions.fail) BDDMockito.mock(org.mockito.BDDMockito.mock) Statement(io.r2dbc.spi.Statement) BDDMockito.reset(org.mockito.BDDMockito.reset) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 8 with TransactionalOperator

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

the class R2dbcTransactionManagerUnitTests method appliesIsolationLevel.

@Test
void appliesIsolationLevel() {
    when(connectionMock.commitTransaction()).thenReturn(Mono.empty());
    when(connectionMock.getTransactionIsolationLevel()).thenReturn(IsolationLevel.READ_COMMITTED);
    when(connectionMock.setTransactionIsolationLevel(any())).thenReturn(Mono.empty());
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
    TransactionalOperator operator = TransactionalOperator.create(tm, definition);
    ConnectionFactoryUtils.getConnection(connectionFactoryMock).as(operator::transactional).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    verify(connectionMock).beginTransaction();
    verify(connectionMock).setTransactionIsolationLevel(IsolationLevel.READ_COMMITTED);
    verify(connectionMock).setTransactionIsolationLevel(IsolationLevel.SERIALIZABLE);
    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 9 with TransactionalOperator

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

the class R2dbcTransactionManagerUnitTests method doesNotSetAutoCommitDisabled.

@Test
void doesNotSetAutoCommitDisabled() {
    when(connectionMock.isAutoCommit()).thenReturn(false);
    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, never()).setAutoCommit(anyBoolean());
    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)

Example 10 with TransactionalOperator

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

the class R2dbcTransactionManagerUnitTests method appliesReadOnly.

@Test
void appliesReadOnly() {
    when(connectionMock.commitTransaction()).thenReturn(Mono.empty());
    when(connectionMock.setTransactionIsolationLevel(any())).thenReturn(Mono.empty());
    Statement statement = mock(Statement.class);
    when(connectionMock.createStatement(anyString())).thenReturn(statement);
    when(statement.execute()).thenReturn(Mono.empty());
    tm.setEnforceReadOnly(true);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setReadOnly(true);
    TransactionalOperator operator = TransactionalOperator.create(tm, definition);
    ConnectionFactoryUtils.getConnection(connectionFactoryMock).as(operator::transactional).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    verify(connectionMock).isAutoCommit();
    verify(connectionMock).beginTransaction();
    verify(connectionMock).createStatement("SET TRANSACTION READ ONLY");
    verify(connectionMock).commitTransaction();
    verify(connectionMock).close();
    verifyNoMoreInteractions(connectionMock);
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Statement(io.r2dbc.spi.Statement) 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