use of org.springframework.transaction.reactive.TransactionalOperator in project spring-framework by spring-projects.
the class R2dbcTransactionManagerUnitTests method testRollbackFails.
@Test
@SuppressWarnings("unchecked")
void testRollbackFails() {
when(connectionMock.rollbackTransaction()).thenReturn(Mono.defer(() -> Mono.error(new R2dbcBadGrammarException("Commit should fail"))), Mono.empty());
TransactionalOperator operator = TransactionalOperator.create(tm);
operator.execute(reactiveTransaction -> {
reactiveTransaction.setRollbackOnly();
return ConnectionFactoryUtils.getConnection(connectionFactoryMock).doOnNext(connection -> connection.createStatement("foo")).then();
}).as(StepVerifier::create).verifyError(IllegalTransactionStateException.class);
verify(connectionMock).isAutoCommit();
verify(connectionMock).beginTransaction();
verify(connectionMock).createStatement("foo");
verify(connectionMock, never()).commitTransaction();
verify(connectionMock).rollbackTransaction();
verify(connectionMock).close();
verifyNoMoreInteractions(connectionMock);
}
use of org.springframework.transaction.reactive.TransactionalOperator in project spring-framework by spring-projects.
the class TransactionAwareConnectionFactoryProxyUnitTests method shouldEmitBoundConnection.
@Test
void shouldEmitBoundConnection() {
when(connectionMock1.beginTransaction()).thenReturn(Mono.empty());
when(connectionMock1.commitTransaction()).thenReturn(Mono.empty());
when(connectionMock1.close()).thenReturn(Mono.empty());
TransactionalOperator rxtx = TransactionalOperator.create(tm);
AtomicReference<Connection> transactionalConnection = new AtomicReference<>();
TransactionAwareConnectionFactoryProxy proxyCf = new TransactionAwareConnectionFactoryProxy(connectionFactoryMock);
ConnectionFactoryUtils.getConnection(connectionFactoryMock).doOnNext(transactionalConnection::set).flatMap(connection -> proxyCf.create().doOnNext(wrappedConnection -> assertThat(((Wrapped<?>) wrappedConnection).unwrap()).isSameAs(connection))).as(rxtx::transactional).flatMapMany(Connection::close).as(StepVerifier::create).verifyComplete();
verifyNoInteractions(connectionMock2);
verifyNoInteractions(connectionMock3);
verify(connectionFactoryMock, times(1)).create();
}
use of org.springframework.transaction.reactive.TransactionalOperator in project spring-cloud-gcp by spring-cloud.
the class ReactiveFirestoreTransactionManagerTest method triggerCommitCorrectly.
@Test
public void triggerCommitCorrectly() {
FirestoreTemplate template = getFirestoreTemplate();
ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent);
TransactionalOperator operator = TransactionalOperator.create(txManager);
template.findById(Mono.just("e1"), FirestoreTemplateTests.TestEntity.class).concatWith(template.findById(Mono.just("e2"), FirestoreTemplateTests.TestEntity.class)).as(operator::transactional).as(StepVerifier::create).expectNext(new FirestoreTemplateTests.TestEntity("e1", 100L), new FirestoreTemplateTests.TestEntity("e2", 100L)).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());
GetDocumentRequest request2 = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/" + "e2").setTransaction(ByteString.copyFromUtf8("transaction1")).build();
verify(this.firestoreStub, times(1)).getDocument(eq(request2), any());
}
use of org.springframework.transaction.reactive.TransactionalOperator in project spring-cloud-gcp by spring-cloud.
the class ReactiveFirestoreTransactionManagerTest method triggerRollbackCorrectly.
@Test
public void triggerRollbackCorrectly() {
FirestoreTemplate template = getFirestoreTemplate();
ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent);
TransactionalOperator operator = TransactionalOperator.create(txManager);
template.findById(Mono.defer(() -> {
throw new FirestoreDataException("BOOM!");
}), FirestoreTemplateTests.TestEntity.class).as(operator::transactional).as(StepVerifier::create).expectError().verify();
verify(this.firestoreStub, times(1)).beginTransaction(any(), any());
verify(this.firestoreStub, times(0)).commit(any(), any());
verify(this.firestoreStub, times(1)).rollback(any(), any());
}
use of org.springframework.transaction.reactive.TransactionalOperator in project spring-cloud-gcp by spring-cloud.
the class FirestoreRepositoryIntegrationTests method transactionalOperatorTest.
// end::repository_built_in[]
@Test
public void transactionalOperatorTest() {
// tag::repository_transactional_operator[]
DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
transactionDefinition.setReadOnly(false);
TransactionalOperator operator = TransactionalOperator.create(this.txManager, transactionDefinition);
// end::repository_transactional_operator[]
// tag::repository_operations_in_a_transaction[]
User alice = new User("Alice", 29);
User bob = new User("Bob", 60);
this.userRepository.save(alice).then(this.userRepository.save(bob)).as(operator::transactional).block();
this.userRepository.findAll().flatMap(a -> {
a.setAge(a.getAge() - 1);
return this.userRepository.save(a);
}).as(operator::transactional).collectList().block();
assertThat(this.userRepository.findAll().map(User::getAge).collectList().block()).containsExactlyInAnyOrder(28, 59);
// end::repository_operations_in_a_transaction[]
}
Aggregations