简体   繁体   English

如何从容器管理的事务提交中捕获异常?

[英]How do i catch exceptions from container managed transaction commit?

I have a @Stateless EJB with a @WebService interface, using container managed transactions, meaning transactions are automatically committed by the container after a method has been called if it doesn't throw a system exception. 我有一个带有@WebService接口的@Stateless EJB,使用容器管理的事务,这意味着如果没有引发系统异常,则在调用方法后,容器会自动提交事务。

If i try to EntityManager.persist(...) two objects with the same value for a column with a unique constraint on it, the container will throw a PersistenceException to the client on commit outside my code. 如果我尝试将EntityManager.persist(...)两个具有相同值的对象用于具有唯一约束的列,则容器将在我的代码之外的提交时向客户端抛出PersistenceException How do i catch this exception so i can rethrow my own application exception? 我如何捕获此异常,以便我可以重新抛出自己的应用程序异常?

Do i have to commit the transaction manually in my methods to catch exceptions on commit? 我是否必须在我的方法中手动提交事务以捕获提交中的异常? (And is EntityManager.flush() the correct way to do that?) If so, what's the point of having container managed transactions? (并且EntityManager.flush()是正确的方法吗?)如果是这样,那么容器管理事务的重点是什么?

It is unfortunately not possible to catch exceptions from container-managed transaction failure. 遗憾的是,不可能从容器管理的事务失败中捕获异常。 As you stated, your best option is to use bean-managed transactions. 如您所述,您最好的选择是使用bean管理的事务。 Alternatively, you could wrap your EM EJB with a proxy bean that implements the same interface. 或者,您可以使用实现相同接口的代理bean来包装EM EJB。 Container-managed transactions are appropriate when your code does not need to respond to specific commit failures. 当您的代码不需要响应特定的提交失败时,容器管理的事务是适当的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM