gpt4 book ai didi

java - 如何在 JavaEE 6 中捕获 OptimisticLockException?

转载 作者:行者123 更新时间:2023-11-30 07:20:27 26 4
gpt4 key购买 nike

我想知道在 JavaEE 6 中捕获 OptimisticLockException 的最佳方法是什么。我有以下 EJB:

@Stateless
public class SeminarBooking {

public void bookSeminar(Long seminarId, int numberOfPersons) {
...
//check capacity & do booking
//OptimisticLockException can occur in this method
}

这是我的 REST 界面:

@Path("/seminars")
@Produces("application/xml")
@Stateless
public class SeminarResource {

@GET
@Path("{id}/book")
public Seminar bookSeminar(@PathParam("id") Long id, @QueryParam("persons") Integer persons) {
try {
seminarBooking.bookSeminar(id, persons);
return seminarBooking.getSeminar(id);
}
catch(Exception e) {
//why is this never called?
logger.error(This will never happen, e);
throw new WebApplicationException(e);
}
}

在 REST 接口(interface)中,我捕获了所有异常,此外,如果我从浏览器调用接口(interface),我会看到 OptimisticLockException,那么为什么 catch-Block 从未执行过?

最佳答案

显而易见的答案是,所讨论的异常不是在该 try block 中引发的。尝试阅读堆栈跟踪以查看它是从哪里抛出的。鉴于它与持久性有关,它很可能会在您的事务边界处抛出,而不是从您认为的位置抛出。

关于java - 如何在 JavaEE 6 中捕获 OptimisticLockException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14006882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com