英文:
Quarkus Hibernate Reactive - Transaction not executed (subscribed to) inside an event handler
问题
我正在使用带有Panache的Hibernate Reactive。
我有以下事件处理程序:
@ConsumeEvent(value = "save-result")
@ReactiveTransactional
public Uni<ResultEntity> saveResult(ResultMessage msg) {
ResultEntity result = msg.getResultEntity();
result.setComplete(true);
LOGGER.log(Level.INFO, "======> About to save Result: {0}", result);
return resultRepository.persistAndFlush(result)
.onItem().invoke(() -> LOGGER.log(Level.INFO,
"Result has been saved"));
}
事件已被消耗(日志"About to save..."已打印),但实体未被保存。就好像事务未被订阅一样(日志消息"Result has been saved"也未被打印)。
我正在运行Quarkus 2.16.7(在此之前,我尝试过2.12.x,但没有运气)。
请注意,我有另一个ApplicationScoped bean,在其中类似的操作(虽然更复杂)运行得很好。
我对Quarkus不陌生(我已经使用了几个月),但我也不是专家,但我很困惑为什么这不起作用。
为什么事务没有执行?我漏掉了什么吗?
更新:
出于测试目的,我稍微更改了代码,如下所示,添加了另一行日志,以检查Uni是否被订阅,显然它被订阅了(因为"----> in flow"被输出),但实体不会被持久化,并且"Result has been saved"也从未被打印出来。
@ConsumeEvent(value = "save-result")
@ReactiveTransactional
public Uni<ResultEntity> saveResult(ResultMessage msg) {
ResultEntity result = msg.getResultEntity();
result.setComplete(true);
LOGGER.log(Level.INFO, "======> About to save Result: {0}", result);
return Uni.createFrom().voidItem().onItem()
.invoke(() -> LOGGER.log(Level.INFO,"----> in flow"))
.onItem().transformToUni(unused -> resultRepository.persistAndFlush(result)
.onItem().invoke(() -> LOGGER.log(Level.INFO,
"Result has been saved")));
}
我怀疑这可能类似于https://github.com/quarkusio/quarkus/issues/12925或https://github.com/quarkusio/quarkus/issues/14591的问题,但我无法确认。
英文:
I am using Hibernate Reactive with Panache.
I have the following event handler:
@ConsumeEvent(value = "save-result")
@ReactiveTransactional
public Uni<ResultEntity> saveResult(ResultMessage msg) {
ResultEntity result= msg.getResultEntity();
result.setComplete(true);
LOGGER.log(Level.INFO, "======> About to save Result: {0}", result);
return resultRepository.persistAndFlush(result)
.onItem().invoke(() -> LOGGER.log(Level.INFO,
"Result has been saved");
}
The event is being consumed (the log "About to save..." is printed), but the entity is not being saved. It is as if the transaction is not being subscribed to. (the log message "Result has been saved" is not being printed either)
I'm running on Quarkus 2.16.7 (prior to that, I tried with 2.12.x with no luck).
Note that I have another ApplicationScoped bean where a similar operation (though more complex) works just fine.
I'm not new to Quarkus (I've been working with it for a couple of months), but I'm not an expert either, and yet I am dumbfounded that this does not work.
Why is the transaction not being executed? Am I missing something?
UPDATE:
For testing purposes, I have changed the code a bit, by adding another log line as seen below, to check whether the Uni is being subscribed and apparently it is (since "----> in flow" is being outputted), yet the entity is not persisted and "Result has been saved" is never printed either.
@ConsumeEvent(value = "save-result")
@ReactiveTransactional
public Uni<ResultEntity> saveResult(ResultMessage msg) {
ResultEntity result= msg.getResultEntity();
result.setComplete(true);
LOGGER.log(Level.INFO, "======> About to save Result: {0}", result);
return Uni.createFrom().voidItem().onItem()
.invoke(() -> LOGGER.log(Level.INFO,"----> in flow"))
.onItem().transformToUni(unused -> resultRepository.persistAndFlush(result)
.onItem().invoke(() -> LOGGER.log(Level.INFO,
"Result has been saved")));
}
I suspect that it might be an issue similar to https://github.com/quarkusio/quarkus/issues/12925 or to https://github.com/quarkusio/quarkus/issues/14591 but I cannot confirm that.
答案1
得分: 1
所以我在persistAndFlush
之后立即添加了.onFailure().invoke(Throwable::printStackTrace)
,并且发现操作抛出了一个Hibernate异常。
这意味着当在@ConsumeEvent
方法内执行时,异常被悄悄捕获并忽略,因此事务从未完成。(类似于这里和这里描述的已关闭问题)。
似乎需要为此问题打开另一个问题。
英文:
So I added .onFailure().invoke(Throwable::printStackTrace)
right after persistAndFlush
and saw that the operation threw a HibernateException.
This means that the exceptions are silently captured and ignored when executed within a @ConsumeEvent
method, and therefore the transaction never completes. (similar to the closed issues described here and here).
Seems like another issue needs to be opened for this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论