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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations