use of org.springframework.transaction.reactive.TransactionalOperator in project spring-cloud-gcp by spring-cloud.
the class FirestoreIntegrationTests method transactionTest.
@Test
public void transactionTest() {
User alice = new User("Alice", 29);
User bob = new User("Bob", 60);
User user = new User(null, 40);
DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
transactionDefinition.setReadOnly(false);
TransactionalOperator operator = TransactionalOperator.create(this.txManager, transactionDefinition);
reset(this.txManager);
this.firestoreTemplate.save(alice).then(this.firestoreTemplate.save(bob)).then(this.firestoreTemplate.save(user)).as(operator::transactional).block();
assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).containsExactlyInAnyOrder(bob, alice, user);
verify(this.txManager, times(1)).commit(any());
verify(this.txManager, times(0)).rollback(any());
verify(this.txManager, times(1)).getReactiveTransaction(any());
reset(this.txManager);
// test rollback
this.firestoreTemplate.saveAll(Mono.defer(() -> {
throw new FirestoreDataException("BOOM!");
})).then(this.firestoreTemplate.deleteAll(User.class)).as(operator::transactional).onErrorResume(throwable -> Mono.empty()).block();
verify(this.txManager, times(0)).commit(any());
verify(this.txManager, times(1)).rollback(any());
verify(this.txManager, times(1)).getReactiveTransaction(any());
assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(3);
this.firestoreTemplate.findAll(User.class).flatMap(a -> {
a.setAge(a.getAge() - 1);
return this.firestoreTemplate.save(a);
}).as(operator::transactional).collectList().block();
List<User> users = this.firestoreTemplate.findAll(User.class).collectList().block();
assertThat(users).extracting("age").containsExactlyInAnyOrder(28, 59, 39);
assertThat(users).extracting("name").doesNotContainNull();
this.firestoreTemplate.deleteAll(User.class).as(operator::transactional).block();
assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).isEmpty();
}
use of org.springframework.transaction.reactive.TransactionalOperator in project spring-cloud-gcp by spring-cloud.
the class ReactiveFirestoreTransactionManagerTest method writeTransaction.
@Test
public void writeTransaction() {
FirestoreTemplate template = getFirestoreTemplate();
ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent);
TransactionalOperator operator = TransactionalOperator.create(txManager);
doAnswer(invocation -> {
CommitRequest commitRequest = invocation.getArgument(0);
StreamObserver<CommitResponse> streamObserver = invocation.getArgument(1);
assertThat(commitRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
assertThat(commitRequest.getWritesList().get(0).getUpdate().getName()).isEqualTo(this.parent + "/testEntities/" + "e2");
assertThat(commitRequest.getWritesList().get(1).getUpdate().getName()).isEqualTo(this.parent + "/testEntities/" + "e3");
assertThat(commitRequest.getWritesList().get(2).getDelete()).isEqualTo(this.parent + "/testEntities/" + "e3");
streamObserver.onNext(CommitResponse.newBuilder().build());
streamObserver.onCompleted();
return null;
}).when(this.firestoreStub).commit(any(), any());
template.findById(Mono.just("e1"), FirestoreTemplateTests.TestEntity.class).flatMap(testEntity -> template.save(new FirestoreTemplateTests.TestEntity("e2", 100L))).flatMap(testEntity -> template.save(new FirestoreTemplateTests.TestEntity("e3", 100L))).flatMap(testEntity -> template.delete(Mono.just(testEntity))).then().as(operator::transactional).as(StepVerifier::create).verifyComplete();
verify(this.firestoreStub).beginTransaction(any(), any());
verify(this.firestoreStub).commit(any(), any());
GetDocumentRequest request1 = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/" + "e1").setTransaction(ByteString.copyFromUtf8("transaction1")).build();
verify(this.firestoreStub, times(1)).getDocument(eq(request1), any());
}
use of org.springframework.transaction.reactive.TransactionalOperator 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();
}
use of org.springframework.transaction.reactive.TransactionalOperator in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTransactionManagerUnitTests method suspendTransactionWhilePropagationNotSupported.
// DATAMONGO-2265
@Test
void suspendTransactionWhilePropagationNotSupported() {
ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
ReactiveMongoTemplate template = new ReactiveMongoTemplate(databaseFactory);
when(session.commitTransaction()).thenReturn(Mono.empty());
TransactionalOperator outer = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
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, never()).startTransaction();
verify(databaseFactory, times(1)).withSession(eq(session));
verify(databaseFactory, never()).withSession(eq(session2));
verify(db, times(2)).drop();
verify(session2, never()).close();
}
use of org.springframework.transaction.reactive.TransactionalOperator in project spring-boot by spring-projects.
the class TransactionAutoConfigurationTests method whenThereAreBothReactiveAndPlatformTransactionManagersATemplateAndAnOperatorAreAutoConfigured.
@Test
void whenThereAreBothReactiveAndPlatformTransactionManagersATemplateAndAnOperatorAreAutoConfigured() {
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class)).withUserConfiguration(SinglePlatformTransactionManagerConfiguration.class, SingleReactiveTransactionManagerConfiguration.class).withPropertyValues("spring.datasource.url:jdbc:h2:mem:" + UUID.randomUUID()).run((context) -> {
PlatformTransactionManager platformTransactionManager = context.getBean(PlatformTransactionManager.class);
TransactionTemplate transactionTemplate = context.getBean(TransactionTemplate.class);
assertThat(transactionTemplate.getTransactionManager()).isSameAs(platformTransactionManager);
ReactiveTransactionManager reactiveTransactionManager = context.getBean(ReactiveTransactionManager.class);
TransactionalOperator transactionalOperator = context.getBean(TransactionalOperator.class);
assertThat(transactionalOperator).extracting("transactionManager").isSameAs(reactiveTransactionManager);
});
}
Aggregations