r2dbc事务处理

简介: r2dbc事务处理

官方demo:

https://github.com/spring-projects/spring-data-examples/blob/main/r2dbc/example/src/main/java/example/springdata/r2dbc/basics/TransactionalService.java

/*
 * Copyright 2019-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package example.springdata.r2dbc.basics;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import reactor.core.publisher.Mono;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Oliver Drotbohm
 */
@Component
@RequiredArgsConstructor
class TransactionalService {

  private final @NonNull CustomerRepository repository;

  /**
   * Saves the given {@link Customer} unless its firstname is "Dave".
   *
   * @param customer must not be {@literal null}.
   * @return
   */
  @Transactional
  public Mono<Customer> save(Customer customer) {

    return repository.save(customer).map(it -> {

      if (it.firstname().equals("Dave")) {
        throw new IllegalStateException();
      } else {
        return it;
      }
    });
  }
}

可以看到是支持Transactional的

当然我们可以手动回滚事务,配置:

import io.r2dbc.spi.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.r2dbc.connection.R2dbcTransactionManager;
import org.springframework.transaction.ReactiveTransactionManager;
import org.springframework.transaction.reactive.TransactionalOperator;

@Configuration
public class R2dbcConfig {

    @Bean
    public ReactiveTransactionManager reactiveTransactionManager(ConnectionFactory connectionFactory) {
        return new R2dbcTransactionManager(connectionFactory);
    }

    @Bean
    public TransactionalOperator transactionalOperator(ReactiveTransactionManager txm) {
        return TransactionalOperator.create(txm);
    }

使用:

@Resource
private TransactionalOperator transactionalOperator;

@Test
void test() {
    var user = new User();
    user.setId(1L);
    user.setName("test");
    transactionalOperator.execute(transactionStatus -> {
                transactionStatus.setRollbackOnly();
                return r2dbcEntityOperations.insert(user)
                        .thenMany(userRepository.findAll());
            }).as(StepVerifier::create)
            .expectNextMatches(e -> true).verifyComplete();
}
相关文章
|
分布式计算 Java 数据库连接
了解Spring R2DBC的声明式事务实现机制
# Spring非反应式事务实现原理 Spring基于注解和AOP的声明式事务(@Transactional)已经是业务开发的常用工具,默认是采用同步的方式基于ThreadLocal(保存连接信息和会话信息等)实现,在具体数据库操作时就使用同一个数据库连接,并手动提交事务,保证数据正确性。 # 基于反应式的Spring事务有何不同 Spring的反应式实现是基于Reactor框架,该框架
3361 0
|
Kubernetes 数据安全/隐私保护 Docker
k8s使用私有镜像仓库的访问凭据配置
k8s使用私有镜像仓库的访问凭据配置
1076 1
|
2月前
|
SQL 人工智能 前端开发
Vue3+NestJS实战:纺织行业AI进销存系统架构设计
本文介绍面向纺织行业的AI进销存系统“轻账LightLedger”架构设计:基于Vue3+Element Plus前端与NestJS后端,深度集成批号追溯、多单位换算、委外加工管理,并创新融合DeepSeek大模型实现自然语言智能查询,助力传统ERP升级。
262 2
|
存储 缓存 Java
Infinispan篇(一):一个被遗忘了的分布式集群缓存系统
Infinispan 是一个开源内存数据网格,提供灵活的部署选项和强大的数据存储、管理和处理功能。
3507 0
|
存储 域名解析 缓存
|
存储 缓存 JavaScript
国内开源规则引擎牛起来
国内开源规则引擎牛起来
2048 0
|
消息中间件 存储 NoSQL
深入Redis消息队列:Pub/Sub和Stream的对决【redis第六部分】
深入Redis消息队列:Pub/Sub和Stream的对决【redis第六部分】
1594 0
|
3月前
|
人工智能 自然语言处理
【学生必看】免费领价值 59 元的 1 个月 Qoder CN 专业版
Qoder是阿里推出的国产智能体工作台,像一位全能AI助手:听懂你的自然语言指令,自动做PPT、写文档、查资料、处理表格、操作网页等。高校学生认证后,可0元领取1个月专业版(含Qwen3.8等多模型),限时福利!
2966 30
【学生必看】免费领价值 59 元的 1 个月 Qoder CN 专业版
|
3月前
建议大家放弃qoder
用户吐槽Qoder积分机制愈发苛刻:早期2000积分可享1倍速,如今6000积分仅0.5倍速,体验严重倒退。自国际版起长期使用,却越用越差,呼吁大家放弃该平台。
777 0
|
安全 算法 网络安全
一文读懂 RSA 加密:非对称加密的基石
RSA是应用最广泛的非对称加密算法,由Rivest、Shamir和Adleman于1977年提出。它基于大数分解难题,使用公钥加密、私钥解密,解决密钥分发问题,广泛用于HTTPS、数字签名等安全通信场景,是现代网络安全的基石之一。
5664 0