EJB3 transaction rollback

后端 未结 2 1961
离开以前
离开以前 2021-02-01 02:56

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

  1. Do I have

相关标签:
2条回答
  • 2021-02-01 03:01

    The question of how to prevent checked exceptions annotationally declared to cause a rollback on throwing from being propagated to the "upper layer" is not yet answered here.

    I think that this will require a wrapper around the EJB in question which swallows the thrown exception. (In other words: I think that the custom exception MUST be thrown against method boundary (and thus not catched & processed inside the method) AND propagated to take transactional effect -- and also will in turn cause destruction of the EJB instance.)

    0 讨论(0)
  • 2021-02-01 03:18

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

    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.

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

    0 讨论(0)
提交回复
热议问题