EJB: Avoid Transaction rollback

只愿长相守 提交于 2019-12-03 04:59:56

Transaction is rolled back in case you throw a RuntimeException or any Exception which have @ApplicationException annotation with rollback attribute set to true, so:

@ApplicationException(rollback=true)
public class MyException extends Exception {
    // ...
}

will rollback the current transaction.

By default ApplicationException doesn't rollback your transaction.

If you don't want to methodB to rollback your transaction you can either change the rollback behavior of your ApplicationException or prevent the transaction sharing.

The latter is achievable by changing the TransactionAttribute of methodB i.e. to RequiresNew. Then methodA transaction (Tx1) will be suspendend and in case methodB throws an exception which results in rollback of its transaction (Tx2), you can still catch it in methodA and prevent rollback of your methodA transaction (Tx1).

Yes, it's true, if the exception is a runtime exception. Checked exceptions don't cause a transaction rollback.

To avoid it, just make sure that the code in methodB doesn't throw any runtime exception. A runtime exception normally indicates a bug, or a state which doesn't allow for continuing the work.

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