Search in sources :

Example 6 with JMQXid

use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.

the class XAResourceForMC method rollback.

/**
 * @param maxRollbacks maximum number of consecutive rollbacks allowed for active consumers
 * @param dmqOnMaxRollbacks if true, place the consumed message on DMQ if maxRollbacks reached
 */
private synchronized void rollback(Xid foreignXid, int maxRollbacks, boolean dmqOnMaxRollbacks) throws XAException {
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine(_lgrMID_INF + "XAResourceForMC (" + this.hashCode() + ") Rollback  " + printXid(foreignXid) + ")");
    }
    // convert to jmq xid
    JMQXid jmqXid = new JMQXid(foreignXid);
    // System.out.println("MQRA:XAR4MC:rollback():mcId="+mc.getMCId()+":xid="+jmqXid.toString());
    // Debug.println("MQRA:XAR4RA:rollback():tid="+transactionID+" xid="+jmqXid.toString());
    JMSRAXASession xas = null;
    JMSRASessionAdapter sa = null;
    try {
        // send rollback w/redeliver
        if (!epConnection._isClosed()) {
            // System.out.println("MQRA:XAR4MC:rollback:Using epCon");
            sa = mc.getConnectionAdapter().getJMSRASessionAdapter();
            if (epConnection.isConnectedToHABroker()) {
                // handle fail-over for HA connection
                HARollback(jmqXid, maxRollbacks, dmqOnMaxRollbacks);
            } else {
                epConnection.getProtocolHandler().rollbackXA(0L, jmqXid, true, false, maxRollbacks, dmqOnMaxRollbacks);
            }
            if (sa != null) {
                // System.out.println("MQRA:XAR4MC:rollback:sa!=null");
                // sa.getXASession().finishXATransactionForMC();
                sa.getJMSRAXASession().finishXATransactionForMC();
            }
            active = false;
        } else {
            System.err.println("MQRA:XARFMC:rollback:ConnectionClosed:Rollback txn:xid=" + jmqXid.toString());
            // Debug.println("*=*=*=*=*=*=*=*=*=*=XAR:prepare:XAException:RMFAIL");
            XAException xae = new XAException(XAException.XAER_RMFAIL);
            throw xae;
        }
    } catch (Exception jmse) {
        System.err.println("MQRA:XARFMC:rollback:XAException-Exception=" + jmse.getMessage());
        // Debug.println("*=*=*=*=*=*=*=*=*=*=XAR:rollbackXAException");
        Debug.printStackTrace(jmse);
        XAException xae = new XAException(XAException.XAER_RMFAIL);
        xae.initCause(jmse);
        throw xae;
    } finally {
        if (sa != null) {
            xas = sa.getJMSRAXASession();
            if (xas != null) {
                xas.setFailoverOccurred(false);
            }
        }
        // finish up this resource and any others joined to it in this transaction
        boolean throwExceptionIfNotFound = false;
        XidImpl savedXid = this.jmqXid;
        XAResourceForJMQ[] resources = XAResourceMapForRAMC.getXAResources(this.jmqXid, throwExceptionIfNotFound);
        for (int i = 0; i < resources.length; i++) {
            XAResourceForJMQ xari = resources[i];
            try {
                xari.clearTransactionInfo();
            } catch (JMSException jmse) {
                System.err.println("MQRA:XARFMC:rollback:XAException-Exception=" + jmse.getMessage());
                Debug.printStackTrace(jmse);
                XAException xae = new XAException(XAException.XAER_RMFAIL);
                xae.initCause(jmse);
                throw xae;
            }
        }
        XAResourceMapForRAMC.unregister(savedXid);
    }
    this.removeXid(jmqXid);
}
Also used : JMSRASessionAdapter(com.sun.messaging.jms.ra.api.JMSRASessionAdapter) XAException(javax.transaction.xa.XAException) XidImpl(com.sun.messaging.jmq.util.XidImpl) JMSException(jakarta.jms.JMSException) JMQXid(com.sun.messaging.jmq.util.JMQXid) JMSRAXASession(com.sun.messaging.jms.ra.api.JMSRAXASession) JMSException(jakarta.jms.JMSException) XAException(javax.transaction.xa.XAException)

Example 7 with JMQXid

use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.

the class XAResourceForRA method end.

/**
 * Ends the work performed on behalf of a transaction branch. The resource manager disassociates the XA resource from
 * the transaction branch specified and lets the transaction complete.
 *
 * <p>
 * If TMSUSPEND is specified in the flags, the transaction branch is temporarily suspended in an incomplete state. The
 * transaction context is in a suspended state and must be resumed via the <code>start</code> method with TMRESUME
 * specified.
 * </p>
 *
 * <p>
 * If TMFAIL is specified, the portion of work has failed. The resource manager may mark the transaction as
 * rollback-only
 * </p>
 *
 * <p>
 * If TMSUCCESS is specified, the portion of work has completed successfully.
 * </p>
 *
 * @param foreignXid A global transaction identifier that is the same as the identifier used previously in the
 * <code>start</code> method.
 *
 * @param flags One of TMSUCCESS, TMFAIL, or TMSUSPEND.
 *
 * @exception XAException An error has occurred. Possible XAException values are XAER_RMERR, XAER_RMFAILED, XAER_NOTA,
 * XAER_INVAL, XAER_PROTO, or XA_RB*.
 */
@Override
public synchronized void end(Xid foreignXid, int flags) throws XAException {
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine(_lgrMID_INF + "XAResourceForRA (" + this.hashCode() + ") End     " + printXid(foreignXid) + printFlags(flags));
    }
    // convert to jmq xid
    JMQXid jmqXid = new JMQXid(foreignXid);
    if (epConnection._isClosed()) {
        // Debug.println("*=*=*=*=*=*=*=*=*=*=XAR:end:XAException");
        XAException xae = new XAException(XAException.XAER_RMFAIL);
        throw xae;
    }
    // update the resource state
    if (isFail(flags)) {
        resourceState = FAILED;
    } else if (isSuspend(flags)) {
        resourceState = INCOMPLETE;
    } else {
        resourceState = COMPLETE;
    }
    if (JMSRAResourceAdapter.isRevert6882044()) {
        // revert to pre-6882044 behaviour and forward all events to broker
        sendEndToBroker(flags, false, jmqXid);
    } else {
        // END packet, a noop END packet or to ignore the END packet altogether.
        if (resourceState == COMPLETE) {
            // This XAResource is complete. Send a real END packet if all
            // other resources joined to this txn are complete, otherwise
            // send a noop END packet to ensure that work associated with
            // this XAResource has completed on the broker. See bug 12364646.
            boolean allComplete = true;
            XAResourceForJMQ[] resources = XAResourceMapForRAMC.getXAResources(jmqXid, true);
            for (int i = 0; i < resources.length; i++) {
                XAResourceForJMQ xari = resources[i];
                if (!xari.isComplete()) {
                    allComplete = false;
                }
            }
            if (allComplete) {
                // All resources complete. Send real END packet.
                sendEndToBroker(flags, false, jmqXid);
            } else {
                // One or more resources are not complete. Send a noop END
                // packet to the broker.
                sendEndToBroker(flags, true, jmqXid);
            }
        } else if (resourceState == FAILED) {
            // This resource has failed. Send a real END packet regardless
            // of the state of any other joined resources.
            sendEndToBroker(flags, false, jmqXid);
        } else if (resourceState == INCOMPLETE) {
        // Don't send the END to the broker. See Glassfish issue 7118.
        }
    }
    started = false;
    if (this.isXATracking()) {
        xaTable.put(jmqXid, XA_END);
    }
}
Also used : JMQXid(com.sun.messaging.jmq.util.JMQXid)

Example 8 with JMQXid

use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.

the class XAResourceForRA method rollback.

/**
 * @param maxRollbacks maximum number of consecutive rollbacks allowed for active consumers
 * @param dmqOnMaxRollbacks if true, place the consumed message on DMQ if maxRollbacks reached
 */
private synchronized void rollback(Xid foreignXid, int maxRollbacks, boolean dmqOnMaxRollbacks) throws XAException {
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine(_lgrMID_INF + "XAResourceForMC (" + this.hashCode() + ") Rollback  " + printXid(foreignXid) + ")");
    }
    // convert to jmq xid
    JMQXid jmqXid = new JMQXid(foreignXid);
    // Debug.println("MQRA:XAR4RA:rollback():tid="+transactionID+" xid="+jmqXid.toString());
    try {
        // send rollback w/redeliver
        if (!epConnection._isClosed()) {
            if (epConnection.isConnectedToHABroker()) {
                // handle fail-over for HA connection
                HARollback(jmqXid, maxRollbacks, dmqOnMaxRollbacks);
            } else {
                epConnection.getProtocolHandler().rollbackXA(0L, jmqXid, true, false, maxRollbacks, dmqOnMaxRollbacks);
            }
        } else {
            XAException xae = new XAException(XAException.XAER_RMFAIL);
            throw xae;
        }
    } catch (JMSException jmse) {
        // Debug.println("*=*=*=*=*=*=*=*=*=*=XAR:rollbackXAException");
        Debug.printStackTrace(jmse);
        XAException xae = new XAException(XAException.XAER_RMFAIL);
        xae.initCause(jmse);
        throw xae;
    } finally {
        // finish up this resource and any others joined to it in this transaction
        boolean throwExceptionIfNotFound = false;
        XidImpl savedXid = this.jmqXid;
        XAResourceForJMQ[] resources = XAResourceMapForRAMC.getXAResources(this.jmqXid, throwExceptionIfNotFound);
        for (int i = 0; i < resources.length; i++) {
            XAResourceForJMQ xari = resources[i];
            try {
                xari.clearTransactionInfo();
            } catch (JMSException jmse) {
                Debug.printStackTrace(jmse);
                XAException xae = new XAException(XAException.XAER_RMFAIL);
                xae.initCause(jmse);
                throw xae;
            }
        }
        XAResourceMapForRAMC.unregister(savedXid);
    }
    // if (this.isXATracking()) {
    // xaTable.remove(jmqXid);
    // }
    this.removeXid(jmqXid);
}
Also used : XidImpl(com.sun.messaging.jmq.util.XidImpl) JMQXid(com.sun.messaging.jmq.util.JMQXid)

Example 9 with JMQXid

use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.

the class XAResourceForRA method commit.

/**
 * Commits the global transaction specified by xid.
 *
 * @param foreignXid A global transaction identifier
 *
 * @param onePhase If true, the resource manager should use a one-phase commit protocol to commit the work done on
 * behalf of xid.
 *
 * @exception XAException An error has occurred. Possible XAExceptions are XA_HEURHAZ, XA_HEURCOM, XA_HEURRB,
 * XA_HEURMIX, XAER_RMERR, XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO.
 *
 * <P>
 * If the resource manager did not commit the transaction and the parameter onePhase is set to true, the resource
 * manager may throw one of the XA_RB* exceptions. Upon return, the resource manager has rolled back the branch's work
 * and has released all held resources.
 */
@Override
public synchronized void commit(Xid foreignXid, boolean onePhase) throws XAException {
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine(_lgrMID_INF + "XAResourceForMC (" + this.hashCode() + ") Commit  " + printXid(foreignXid) + " (onePhase=" + onePhase + ")");
    }
    // convert to jmq xid
    JMQXid jmqXid = new JMQXid(foreignXid);
    // Debug.println("MQRA:XAR4RA:commit():onePhase="+onePhase+" tid="+transactionID+" xid="+jmqXid.toString());
    if (onePhase && setRollback) {
        // Debug.println("*=*=*=*=*=*=*=*=*=*=XAR:commit1p:forcing Rollback due to:" + rollbackCause.getMessage());
        Debug.printStackTrace(rollbackCause);
        XAException xae = new XAException(XAException.XAER_RMFAIL);
        xae.initCause(rollbackCause);
        throw xae;
    }
    boolean checkrollback = false;
    boolean rbrollback = false;
    Exception rbrollbackex = null;
    try {
        if (!epConnection._isClosed()) {
            if (onePhase) {
                // one phase commit
                if (epConnection.isConnectedToHABroker()) {
                    // use two phase protocol for HA
                    HAOnePhaseCommit(foreignXid, jmqXid);
                } else {
                    try {
                        epConnection.getProtocolHandler().commit(0L, XAResource.TMONEPHASE, jmqXid);
                    } catch (JMSException e) {
                        checkrollback = true;
                        throw e;
                    }
                }
            } else {
                // two phase commit
                if (epConnection.isConnectedToHABroker()) {
                    // HA case
                    this.HATwoPhaseCommit(jmqXid);
                } else {
                    // non-HA
                    epConnection.getProtocolHandler().commit(0L, XAResource.TMNOFLAGS, jmqXid);
                }
            }
        } else {
            System.err.println("MQRA:XARFRA:commit:ConnectionClosed:throw XAExc txn:1p=" + onePhase + ":xid=" + jmqXid.toString());
            // Debug.println("*=*=*=*=*=*=*=*=*=*=XAR:commit:XAException");
            XAException xae = new XAException(XAException.XAER_RMFAIL);
            throw xae;
        }
    } catch (Exception jmse) {
        // check remote exception.
        if (jmse instanceof RemoteAcknowledgeException) {
            // notify session that re-create consumer is required.
            RemoteAcknowledgeException rae = (RemoteAcknowledgeException) jmse;
            XASessionImpl session = (XASessionImpl) omr.getEndpointConsumer().getXASession();
            session.notifyRemoteAcknowledgeException(rae);
            if (checkrollback) {
                rbrollbackex = jmse;
                rbrollback = true;
            }
        }
        this.epConnection.waitForReconnecting(jmse);
        // Debug.println("*=*=*=*=*=*=*=*=*=*=XAR:commitXAException");
        Debug.printStackTrace(jmse);
        if (!rbrollback && checkrollback && (jmse instanceof JMSException)) {
            if (((JMSException) jmse).getErrorCode().equals(ClientResources.X_SERVER_ERROR)) {
                Exception e1 = ((JMSException) jmse).getLinkedException();
                if (e1 instanceof JMSException && !((JMSException) e1).getErrorCode().equals(Status.getString(Status.NOT_FOUND))) {
                    SessionImpl.sessionLogger.log(Level.WARNING, "Exception on 1-phase commit transaction " + jmqXid + ", will rollback", jmse);
                    rbrollbackex = jmse;
                    rbrollback = true;
                }
            }
        }
        if (!rbrollback) {
            XAException xae = new XAException(XAException.XAER_RMFAIL);
            xae.initCause(jmse);
            throw xae;
        }
    } finally {
        if (!rbrollback) {
            // finish up this resource and any others joined to it in this transaction
            boolean throwExceptionIfNotFound = false;
            XidImpl savedXid = this.jmqXid;
            XAResourceForJMQ[] resources = XAResourceMapForRAMC.getXAResources(this.jmqXid, throwExceptionIfNotFound);
            for (int i = 0; i < resources.length; i++) {
                XAResourceForJMQ xari = resources[i];
                try {
                    xari.clearTransactionInfo();
                } catch (JMSException jmse) {
                    Debug.printStackTrace(jmse);
                    XAException xae = new XAException(XAException.XAER_RMFAIL);
                    xae.initCause(jmse);
                    throw xae;
                }
            }
            XAResourceMapForRAMC.unregister(savedXid);
        }
    }
    // }
    if (!rbrollback) {
        this.removeXid(jmqXid);
        return;
    }
    XAException xae;
    try {
        rollback(foreignXid, XAResourceMap.MAXROLLBACKS, XAResourceMap.DMQ_ON_MAXROLLBACKS);
        xae = new XAException(XAException.XA_RBROLLBACK);
        xae.initCause(rbrollbackex);
    } catch (Exception e) {
        SessionImpl.sessionLogger.log(Level.SEVERE, "Exception on rollback transaction " + jmqXid + " after 1-phase-commit failure", e);
        xae = new XAException(XAException.XAER_RMFAIL);
        xae.initCause(rbrollbackex);
    }
    throw xae;
}
Also used : XidImpl(com.sun.messaging.jmq.util.XidImpl) JMQXid(com.sun.messaging.jmq.util.JMQXid)

Example 10 with JMQXid

use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.

the class XAResourceForRA method prepare.

/**
 * Ask the resource manager to prepare for a transaction commit of the transaction specified in xid.
 *
 * @param foreignXid A global transaction identifier.
 *
 * @exception XAException An error has occurred. Possible exception values are: XA_RB*, XAER_RMERR, XAER_RMFAIL,
 * XAER_NOTA, XAER_INVAL, or XAER_PROTO.
 *
 * @return A value indicating the resource manager's vote on the outcome of the transaction. The possible values are:
 * XA_RDONLY or XA_OK. If the resource manager wants to roll back the transaction, it should do so by raising an
 * appropriate XAException in the prepare method.
 */
@Override
public synchronized int prepare(Xid foreignXid, boolean onePhase) throws XAException {
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine(_lgrMID_INF + "XAResourceForRA (" + this.hashCode() + ") Prepare     " + printXid(foreignXid));
    }
    // JMS does not do RDONLY transactions - right?
    int result = XA_OK;
    // convert to jmq xid
    JMQXid jmqXid = new JMQXid(foreignXid);
    // Debug.println("*=*=*=*=*=*=*=*=*=*=XAR:prepare:txid=\n"+jmqXid.toString());
    if (setRollback) {
        // Debug.println("*=*=*=*=*=*=*=*=*=*=XAR:prepare:forcing Rollback due to:" + rollbackCause.getMessage());
        Debug.printStackTrace(rollbackCause);
        XAException xae = new XAException(XAException.XAER_RMFAIL);
        xae.initCause(rollbackCause);
        throw xae;
    }
    try {
        if (!epConnection._isClosed()) {
            epConnection.getProtocolHandler().prepare(0L, jmqXid, onePhase);
        } else {
            XAException xae = new XAException(XAException.XAER_RMFAIL);
            throw xae;
        }
    } catch (Exception jmse) {
        // check remote exception.
        if (jmse instanceof RemoteAcknowledgeException) {
            // notify session that re-create consumer is required.
            RemoteAcknowledgeException rae = (RemoteAcknowledgeException) jmse;
            ((XASessionImpl) omr.getEndpointConsumer().getXASession()).notifyRemoteAcknowledgeException(rae);
        }
        // this.epConnection.waitForReconnecting ( jmse );
        // Debug.println("*=*=*=*=*=*=*=*=*=*=XAR:prepareXAException");
        Debug.printStackTrace(jmse);
        XAException xae = new XAException(XAException.XAER_RMFAIL);
        xae.initCause(jmse);
        throw xae;
    }
    // update the resource state
    resourceState = PREPARED;
    return result;
}
Also used : JMQXid(com.sun.messaging.jmq.util.JMQXid)

Aggregations

JMQXid (com.sun.messaging.jmq.util.JMQXid)39 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)13 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)9 TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)6 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)6 XidImpl (com.sun.messaging.jmq.util.XidImpl)4 JMSException (jakarta.jms.JMSException)4 Hashtable (java.util.Hashtable)4 XAException (javax.transaction.xa.XAException)4 JMSRASessionAdapter (com.sun.messaging.jms.ra.api.JMSRASessionAdapter)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3 Map (java.util.Map)3 BrokerAddress (com.sun.messaging.jmq.jmsserver.core.BrokerAddress)2 TransactionBroker (com.sun.messaging.jmq.jmsserver.data.TransactionBroker)2 TransactionHandler (com.sun.messaging.jmq.jmsserver.data.handlers.TransactionHandler)2 PartitionedStore (com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)2 UnknownTransactionException (com.sun.messaging.jmq.jmsserver.util.UnknownTransactionException)2 SelectorFormatException (com.sun.messaging.jmq.util.selector.SelectorFormatException)2