MDB异常处理

时间:2014-03-15 07:47:07

标签: prepared-statement message-driven-bean

我有一个在其onMessage()方法中调用一些方法的MDB。在onMessage()的catch块中,我使用preparestatement运行更新查询。我注意到如果流到达catch块,它不会提交更新声明。在MDB的catch块中是不是可以这样做?我的onMessage()方法是这样的

public void onMessage(Message message) {

try{ 的someMethod() } catch(Throwable o) { someUpdateMethod()//update query runs here } }

1 个答案:

答案 0 :(得分:1)

无论是什么引起的异常都会导致事务上下文进入仅回滚模式。 当onMessage返回时,将调用所有事务资源进行回滚。这包括someUpdateMethod()中用于准备语句的数据源。

要提交更新,必须在单独的事务中执行。通过在方法上使用@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)调用另一个无状态会话bean来执行此操作。

@MessageDriven(...
public class MyMdb inpelements MessageListener
    @EJB
    Updater updater;

    @Override
    public void onMessage(Message message) {
      try {
          someMethod();
      }
      catch(Throwable o) {
         updater.someUpdateMethod();
      } 
    }
}

在单独的事务中执行更新的无状态会话EJB。

@Stateless
public class Updater {
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public String someUpdateMethod() {
        //update query runs here
    }
}