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();
}
相关文章
|
3月前
|
编解码 人工智能 运维
阿里云Wan3.0‑Video深度解析:全能视频生成模型能力、场景落地、计费规则与API接入实操选型指南
随着AIGC视频技术快速迭代,视频生成不再局限于数秒短片段,行业对长叙事片段、多素材参考、角色风格一致性、原生音画同步提出越来越高的要求。传统视频生成工具大多只能实现单一的文生视频,参考素材数量有限,人物容易出现五官崩坏、角色前后形象跳变,很难直接用于商业生产。Wan3.0‑Video作为All‑in‑One全能参考式视频生成模型,打通文本、图片、音频、视频、文档、网页多类输入源,原生支持最长30秒1080P高清视频输出,实现文生视频、图生视频、首尾帧过渡、参考素材驱动生成等多种能力,为短剧、广告营销、内容平台、文旅宣传、产品演示等业务提供可规模化调用的API服务。本文将从模型技术特性、核心能
1379 0
|
3月前
|
人工智能 编解码 数据可视化
Wan3.0‑Video深度解析:全能视频生成模型能力、场景落地、计费规则与API接入实操与选型指南
人工智能技术持续演进,内容生产正在经历重大变革,AI不再局限于文本对话、图片生成,视频生成能力逐步成为数字化生产力的关键组成部分。过去大部分视频生成模型只能产出数秒短镜头片段,只能实现简单画面效果,很难承载完整叙事逻辑,更多偏向演示效果,难以投入真实业务生产。2026年8月推出的Wan3.0‑Video(万相3.0)全能视频生成大模型,打破传统多模态模型的输入边界,将单次视频生成时长提升至30秒,除文本、图像、音视频输入以外,首次支持doc、xls、ppt、pdf、md等办公文档格式直接输入生成视频,真正推进AI视频生成技术从“生成单镜头画面”走向“讲述完整故事”的生产力阶段。
1765 0
|
5月前
|
人工智能 自然语言处理 安全
2026企业级Agent平台推荐:核心功能与应用场景解析
2026年企业AI智能体进入规模化落地关键期,但仅17%企业完成实质性部署。瓴羊AgentOne以“Data×AI”双轮驱动,打造企业级智能体调度中枢,支持NL2Data混合技术、多模态协同与全链路闭环,已助力安防、能源、农牧等行业实现数据智能到业务执行的跨越。
|
5月前
|
人工智能 Ubuntu API
Ollama的安装和升级使用实践
Ollama 是一款开源本地LLM运行框架,让在Ubuntu等系统上一键部署、下载(支持国内镜像)和运行大模型(如DeepSeek-R1)变得简单。支持命令行交互、端口配置、服务管理,并可对接Open WebUI与OpenClaw。
1965 0
Ollama的安装和升级使用实践
【Groovy】Groovy 环境搭建 ( 下载 Groovy | 安装 Groovy | 配置 Groovy 环境变量 )
【Groovy】Groovy 环境搭建 ( 下载 Groovy | 安装 Groovy | 配置 Groovy 环境变量 )
1191 0
【Groovy】Groovy 环境搭建 ( 下载 Groovy | 安装 Groovy | 配置 Groovy 环境变量 )
|
5月前
|
人工智能 自然语言处理 数据挖掘
Qwen3.8-Max-Preview深度全解析:2.4万亿参数旗舰MoE模型+Token Plan限时优惠完整落地指南
2026年7月,全新旗舰级混合专家大模型Qwen3.8-Max-Preview正式开放抢先体验,作为通义千问Qwen3系列规格最高、综合推理能力顶尖的新一代模型,该模型总参数量达到2.4万亿(2.4T),是当前线上可调用的原生多模态旗舰模型,综合推理水准对标海外顶级Fable 5模型,在复杂工程开发、长文档深度分析、多步骤智能体自治、跨境多语言创作、海量数据挖掘五大高难度业务场景实现跨越式性能提升。
2645 3
|
5月前
|
存储 人工智能 Linux
CodeGraph全解:为Claude Code降七成工具调用,本地代码图谱完整实操教程
当下Claude Code、Cursor、Codex CLI这类AI编程智能体已经成为全栈、后端、运维开发者日常刚需,不管是项目重构、接口链路排查、线上漏洞定位还是批量单元测试生成,都能大幅缩减开发工时。但绝大多数开发者长期使用后都会遇到同一个核心瓶颈:AI在陌生代码仓库里只能依靠全局关键词检索、逐文件读取的方式摸索逻辑,陷入大量无效工具调用循环。
1622 0
|
5月前
|
安全 Linux API
在 OpenCode 中接入本地模型:Ollama 部署与配置完全指南
OpenCode火爆GitHub(17万星),但配置模型常卡壳。本文直击痛点,手把手教你15分钟用Ollama本地部署Qwen/Llama/CodeLlama等开源模型,并完成OpenCode对接——零API密钥、全离线运行,安全高效适配敏感开发场景。
IDEA——untrusted server's certificate
IDEA——untrusted server's certificate
871 0