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();
}
相关文章
|
存储 NoSQL Java
Spring Session分布式会话管理
Spring Session分布式会话管理
465 0
使用markdown-it对markdown进行一个实时解析
# 引言 大家应该都接触过markdown笔记吧,我们常常见到很多能写文章的网页提供了一个实时的markdown解析的功能----即我们在一侧写下markdown语法,右边能够实时解析,并将解析后的markdown语法渲染到右侧页面上。非常炫酷实用,那么这是如何实现的呢?今天我们来一起探究一下。 # 开始 首先,我们这个演示学习在Vue3+TypeScript下使用的,所以我们预先建议准备一个Vue3+Typescript工程。 对于markdown的解析,我们可以使用`markdown-it`来进行解析。 比如,我们在左侧设置一个输入框,接收用户markdown输入,我们通过`ma
|
分布式计算 Java 数据库连接
了解Spring R2DBC的声明式事务实现机制
# Spring非反应式事务实现原理 Spring基于注解和AOP的声明式事务(@Transactional)已经是业务开发的常用工具,默认是采用同步的方式基于ThreadLocal(保存连接信息和会话信息等)实现,在具体数据库操作时就使用同一个数据库连接,并手动提交事务,保证数据正确性。 # 基于反应式的Spring事务有何不同 Spring的反应式实现是基于Reactor框架,该框架
3111 0
|
安全 Java 数据安全/隐私保护
SpringSecurity6从入门到实战之引言和基本概念
《SpringSecurity6从入门到实战》介绍了Spring Security这一强大的Java安全框架,主要用于保护Spring应用程序的安全。它提供认证和授权功能,支持多种认证方式,并具备高度可定制性。文章阐述了权限管理的重要性,包括用户认证(验证用户身份)和用户授权(控制用户访问权限)。相较于其他如Shiro和Sa-Token的安全框架,Spring Security因与Spring生态的深度整合及对OAuth2的支持,常成为微服务项目的选择。
|
12月前
|
人工智能 前端开发 Serverless
阿里云《AI 剧本生成与动画创作》解决方案技术评测
随着人工智能技术的发展,越来越多的工具和服务被应用于内容创作领域。阿里云推出的《AI 剧本生成与动画创作》解决方案,利用函数计算 FC 构建 Web 服务,结合百炼模型服务和 ComfyUI 工具,实现了从故事剧本撰写、插图设计、声音合成和字幕添加到视频合成的一站式自动化流程。本文将对该方案进行全面的技术评测,包括实现原理及架构介绍、部署文档指引、具体耗时分析以及实际使用体验。
852 16
|
11月前
|
运维 安全 数据建模
阿里云数字证书管理服务免费版和收费版SSL证书区别、收费标准、申请及部署教程参考
阿里云数字证书管理服务提供多种SSL证书类型和品牌,适用于不同规模的网站,包括但不限于电商、小型企业、大型企业或个人等。阿里云SSL证书有收费版的也有免费版的,有的新手用户由于是初次在阿里云申请SSL证书,可能不是很清楚免费版证书的申请和部署流程,本文为以图文形式为大家展示阿里云免费版SSL证书最新的申请及部署教程,以供参考。
|
前端开发 JavaScript 测试技术
Google提出的网页性能评价指标
Google推出的“网页指标”计划旨在优化网页用户体验,其中Core Web Vitals为核心指标,包括Largest Contentful Paint (LCP)、Interaction to Next Paint (INP)和Cumulative Layout Shift (CLS),分别衡量加载速度、互动性和视觉稳定性。这些指标采用第75百分位数评估,确保在不同设备和网络环境下提供一致的用户体验。
683 5
Google提出的网页性能评价指标
|
Java
Velocity简明教程(1)
Velocity简明教程(1)
499 0
|
弹性计算 缓存 大数据
2024年阿里云服务器2核8G、4核16G、8核32G配置最新收费标准及活动价格信息汇总
2024年阿里云服务器价格来了,2核8G、4核16G、8核32G配置的云服务器处理器与内存比为1:4,这种配比的云服务器一般适用于中小型数据库系统、缓存、搜索集群和企业办公类应用等通用型场景,因此,多为企业级用户选择,目前云服务器ECS经济型e实例2核2G、3M固定带宽99元一年、ECS u1实例2核4G、5M固定带宽、80G ESSD Entry盘优惠价格199元一年,轻量应用服务器2核2G3M带宽轻量服务器一年61元、2核4G4M带宽轻量服务器一年165元12个月、2核4G服务器30元3个月。
949 0
|
运维 数据安全/隐私保护 网络协议
【网络建设与运维】2024年河北省职业院校技能大赛中职组“网络建设与运维”赛项例题(十)
【网络建设与运维】2024年河北省职业院校技能大赛中职组“网络建设与运维”赛项例题(十)
【网络建设与运维】2024年河北省职业院校技能大赛中职组“网络建设与运维”赛项例题(十)