MQ队列事务未在两阶段事务中回滚

时间:2015-09-18 09:04:15

标签: java ejb-2.x 2phase-commit bean-managed-transactions ejb-timer

我有一个EJB计时器(EJB 2.1),它有bean管理的事务。

计时器代码调用一个业务方法,该方法处理单个事务中的2个资源。一个是数据库,另一个是MQ队列服务器。

使用的应用程序服务器是Websphere Application Server 7(WAS)。为了确保两个资源(数据库和队列管理器)之间的一致性,我们启用了选项以支持WAS中的两阶段提交。这是为了确保在数据库操作期间发生任何异常时,队列中发布的消息将与数据库回滚一起回滚,反之亦然。

以下是解释的流程:

当Timer代码发生超时时,调用DirectProcessor中的startProcess(),这是我们的业务方法。此方法有一个try块,其中在同一个类中有一个createPostXMLMessage()方法调用。这反过来调用PostMsg类中的另一个方法postMessage()。

问题是当我们在createPostXMLMessage()方法中遇到任何数据库异常时,尽管数据库部分已成功回滚,但先前发布的消息不会回滚。请帮忙。

在ejb-jar.xml

<session id="Transmit">
    <ejb-name>Transmit</ejb-name>
    <home>com.TransmitHome</home>
    <remote>com.Transmit</remote>
    <ejb-class>com.TransmitBean</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Bean</transaction-type>       
</session>

public class TransmitBean implements javax.ejb.SessionBean, javax.ejb.TimedObject {
    public void ejbTimeout(Timer arg0) {
         ....

         new DIRECTProcessor().startProcess(mySessionCtx);

    }
}

public class DIRECTProcessor {

    public String startProcess(javax.ejb.SessionContext mySessionCtx) {

        ....

        UserTransaction ut= null;
        ut = mySessionCtx.getUserTransaction();

        try {
            ut.begin();
            createPostXMLMessage(interfaceObj, btch_id, dpId, errInd);
            ut.commit();
        } 

        catch (Exception e) {                      
             ut.rollback();
             ut=null;        
        }
    }

    public void createPostXMLMessage(ArrayList<InstrInterface> arr_instrObj, String batchId, String dpId,int errInd) throws Exception {
      ...

      PostMsg pm = new PostMsg();
      try {
            pm.postMessage( q_name, final_msg.toString());

           // database update operations using jdbc

      }

      catch (Exception e) {
        throw e;      
      }

    }
}

public class PostMsg {

    public String postMessage(String qName, String message) throws Exception {
        QueueConnectionFactory qcf = null;
        Queue que = null;
        QueueSession qSess = null;
        QueueConnection qConn = null;
        QueueSender qSender = null;
        que = ServiceLocator.getInstance().getQ(qName);

        try {
            qConn = (QueueConnection) qcf.createQueueConnection(
                    Constants.QCONN_USER, Constants.QCONN_PSWD);
            qSess = qConn.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
            qSender = qSess.createSender(que);

            TextMessage txt = qSess.createTextMessage();
            txt.setJMSDestination(que);
            txt.setText(message);
            qSender.send(txt);

        } catch (Exception e) {

            retval = Constants.ERROR;
            e.printStackTrace();
            throw e;

        } finally {

            closeQSender(qSender);
            closeQSession(qSess);
            closeQConn(qConn);
        }

        return retval;

    }

}

0 个答案:

没有答案
相关问题