You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarkus Mutiny重试因线程不一致失败问题求助

解决方案:Quarkus Reactive Hibernate重试时Session跨线程问题

问题根源

Reactive Hibernate的Session绑定到创建它的Vert.x EventLoop线程,你的重试逻辑默认切换到了executor线程,导致Session跨线程使用触发HR000069错误;同时@WithSession在方法启动时创建Session,重试时Session已绑定到旧线程,无法复用。

具体修复步骤

1. 替换@WithSession为手动获取Session

不要用@WithSession注解修饰整个方法,改用ReactiveSessionFactory在重试逻辑内部每次创建新Session,确保Session和当前线程绑定。

2. 强制重试流程在EventLoop线程执行

通过Mutiny的runSubscriptionOn指定Vert.x EventLoop线程作为调度器,避免重试时切换到worker线程。

3. 调整代码结构

将外部DB连接、数据处理、本地持久化逻辑封装到重试块内,确保每次重试都重新初始化Session相关资源。

示例代码

@Inject
ReactiveSessionFactory sessionFactory;

@Inject
DbService dbService;

public Uni<Void> retrieveDataAndSaveInLocalDB() {
    return Uni.createFrom().voidItem()
            // 触发外部DB连接
            .onItem().transformToUni(v -> dbService.connectToDB())
            // 处理数据并持久化到本地库
            .onItem().transformToUni(externalData -> {
                List<LocalEntity> entities = convertToLocalEntities(externalData);
                // 每次重试都创建新Session,且在当前EventLoop线程绑定
                return sessionFactory.withSession(session -> 
                        session.persistAll(entities).onItem().ignore().andContinueWithVoid()
                );
            })
            // 配置重试策略,指定在EventLoop线程执行
            .onFailure().retry()
            .withBackoff(Duration.ofSeconds(1), Duration.ofSeconds(5))
            .atMost(3)
            .runSubscriptionOn(Vertx.currentContext().executor());
}

// 数据转换逻辑示例
private List<LocalEntity> convertToLocalEntities(ExternalData externalData) {
    // 实现外部数据到本地实体的转换逻辑
    return new ArrayList<>();
}

关键细节说明

  • sessionFactory.withSession()会在回调内部创建新的Session,并且自动管理其生命周期(创建、关闭),每次重试都会生成绑定到当前EventLoop线程的Session。
  • runSubscriptionOn(Vertx.currentContext().executor())强制整个Uni流在Vert.x EventLoop线程执行,避免重试时切换到其他线程池。
  • 如果dbService.connectToDB()内部涉及线程切换,需要在该方法内同样添加runSubscriptionOn(Vertx.currentContext().executor()),确保外部DB连接操作也在EventLoop线程执行。

内容的提问来源于stack exchange,提问作者Slawomir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 07:17:42