How do i catch exceptions from container managed transaction commit?

泄露秘密 提交于 2019-12-06 03:17:53

问题


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.

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?

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?


回答1:


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.



来源:https://stackoverflow.com/questions/3726151/how-do-i-catch-exceptions-from-container-managed-transaction-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!