【问题标题】:Handle exception in EJB在 EJB 中处理异常
【发布时间】:2017-11-09 08:28:23
【问题描述】:

我有一个执行一些 DB 操作的 EJB,然后在执行更多 DB 操作的 JNDI 查找之后调用另一个 EJB。这第二个 EJB 调用可能会引发一些异常,即 PersistenceException(我查看了此异常将事务标记为仅回滚的代码)。有什么办法可以避免事务回滚?下面是总结问题的代码。预先感谢。

public class Test {

    ejbMthod1() {
        // This method run under transaction
        // Do some DB operation like update\delete
        if(somecondition) {
            try {
                // JNDI lookup for ejb2
                ejbMethod2();
            }
            catch(Exception ex){
                // Will catching the exception here save the Transaction from rollback ?
                // Is there any way I can avoid this rollback just by handling proper exceptions ?
            }
        }   
    }
}

class Test2 {
    ejbMethod2() {
        // Run under same transaction as caller
        // do some DB operation which can throw many exception i.e. PersistenceException.
    }
}

【问题讨论】:

  • 提示:如果您的问题已解决,请考虑接受。

标签: java exception transactions ejb ejb-3.0


【解决方案1】:

抛出异常的方法必须注释为:

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

在这个主题中还有更多要了解的内容。我强烈推荐阅读this great article

【讨论】:

    猜你喜欢
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    相关资源
    最近更新 更多