我如何从容器管理的事务提交中捕获异常? [英] How do i catch exceptions from container managed transaction commit?

查看:354
本文介绍了我如何从容器管理的事务提交中捕获异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 @Stateless EJB与一个 @WebService 接口,使用容器管理的事务,意味着事务自动如果没有引发系统异常,方法被调用后由容器提交。

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.

如果我尝试 EntityManager.persist(。 ..)两个具有相同值的对象,具有唯一约束的列,容器将在提交时向客户端抛出一个 PersistenceException 在我的代码之外。

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. How do i catch this exception so i can rethrow my own application exception?

我必须在我的方法中手动提交事务来捕获提交异常? (And是 EntityManager.flush()正确的方法来做到这一点?)如果是这样,有什么要点进行容器管理的事务?

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?

推荐答案

不幸的是,无法从容器管理的事务失败中捕获异常。正如你所说,最好的选择是使用bean管理的事务。或者,您可以使用实现相同接口的代理bean来包装您的EM EJB。当您的代码不需要响应特定的提交失败时,容器管理的事务是适当的。

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. Alternatively, you could wrap your EM EJB with a proxy bean that implements the same interface. Container-managed transactions are appropriate when your code does not need to respond to specific commit failures.

这篇关于我如何从容器管理的事务提交中捕获异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆