使用Hibernate Reactive Panache和sdk来切换线程



我正在为Postgresql使用Reactive Panache。我需要一个应用级锁(redis),我需要在其中执行某些操作。但是,panache库会抛出以下错误:

java.lang.IllegalStateException: HR000069: Detected use of the reactive Session from a different Thread than the one which was used to open the reactive Session - this suggests an invalid integration; original thread [222]: 'vert.x-eventloop-thread-3' current Thread [154]: 'vert.x-eventloop-thread-2'

我的代码看起来像这样:

redissonClient.getLock("lock").lock(this.leaseTimeInMillis, TimeUnit.MILLISECONDS, this.lockId)
.chain(() ->  return Panache.withTransaction(() -> Uni.createFrom.nullItem())
.eventually(lock::release);
)

本问题中提到的解决方案显示AWS SDK的正确使用,但与redisson等结合使用时则不正确。有人在和瑞迪森一起工作吗?

更新:

我尝试了以下锁获取和释放:

.runSubscriptionOn(MutinyHelper.executor(Vertx.currentContext())

即使我添加了quarkus-vertx依赖项,也会出现以下错误:

Cannot invoke "io.vertx.core.Context.runOnContext(io.vertx.core.Handler)" because "context" is null

在这种情况下,Panache可能不是最佳选择。

我会尝试直接使用Hibernate Reactive:

@Inject
Mutiny.SessionFactory factory;
...
redissonClient.getLock("lock")
.lock(this.leaseTimeInMillis,TimeUnit.MILLISECONDS, this.lockId)
.chain(() -> factory.withTransaction(session -> Uni.createFrom.nullItem())
.eventually(lock::release))

最新更新