EJB3事务回滚 [英] EJB3 transaction rollback

查看:213
本文介绍了EJB3事务回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在EJB3无状态会话bean中使用CMT。我也创建了自己的Exception,其注释为@ApplicationException(rollback = true)。

I'm using CMT in EJB3 state-less session beans. Also I've created my own Exception having the annotation "@ApplicationException (rollback=true)".


  1. 我是否必须当我想回滚事务时使用context.setRollbackOnly()?

  1. Do I have to use "context.setRollbackOnly()" when I want to rollback the transaction?

我可以通过在bean中的public方法中抛出异常来回滚事务吗? ?

Can I just rollback the transaction by throwing an exception inside public method in the bean?

如果是这样(Q#2的答案是肯定的)我是否必须通过在方法中声明异常或者方法中的异常来抛出异常仅仅在方法中抛出一个异常并在同一个方法中处理它就足够了吗? (我不想将异常传播到下一个级别。我只想回滚异常。)

If so (the answer to Q#2 is yes) do I have to throw the exception out of the method by declaring the exception in the method or will it be sufficient to just throw an exception inside the method and handle it inside the same method itself? (I don't want to propagate the exception to the next level. I just want to rollback the exception.)

提前致谢。 ;)

推荐答案

首先,没有回滚异常,它是事务的回滚。

First of all, there is no rollback of an exception, it's a rollback of a transaction.


  1. 如果使用 @ApplicationException(rollback = true)抛出异常,不必手动回滚事务。 Context.setRollbackOnly()强制容器回滚事务,如果没有异常也是如此。

  2. 已检查的异常本身并非如此t回滚一个事务。它需要有注释 @ApplicationException(rollback = true)。如果异常是 RuntimeException 并且未捕获异常,则会强制容器回滚事务。但请注意,容器在这种情况下将丢弃EJB实例。

  3. 如2.)中所述,如果抛出 RuntimeException ,交易将自动回滚。如果在代码中捕获已检查的异常,则必须使用 setRollbackOnly 来回滚事务。

  1. If you throw your exception with @ApplicationException(rollback=true), you don't have to rollback the transaction manually. Context.setRollbackOnly() forces the container to rollback the transaction, also if there is no exception.
  2. A checked exception itself doesn't rollback a transaction. It needs to have the annotation @ApplicationException(rollback=true). If the exception is a RuntimeException and the exception isn't caught, it forces the container to rollback the transaction. But watch out, the container will in this case discard the EJB instance.
  3. As mentioned in 2.), if you throw a RuntimeException, the transaction will be rolled back automatically. If you catch an checked exception inside the code, you have to use setRollbackOnly to rollback the transaction.

如需了解更多信息,请查看免费书籍掌握EJB 。它很好地描述了回滚场景,并且可以免费用于下载

For further information, check out the free book Mastering EJB. It describes the rollback scenarios very well and is free for download.

这篇关于EJB3事务回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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