/* Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.qpid.client; import javax.transaction.xa.XAException; import javax.transaction.xa.XAResource; import javax.transaction.xa.Xid; import org.apache.qpid.AMQInvalidArgumentException; import org.apache.qpid.dtx.XidImpl; import org.apache.qpid.transport.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * This is an implementation of javax.njms.XAResource. */ public class XAResourceImpl implements XAResource { /** * this XAResourceImpl's logger */ private static final Logger _logger = LoggerFactory.getLogger(XAResourceImpl.class); /** * Reference to the associated XASession */ private XASessionImpl _xaSession = null; /** * The XID of this resource */ private Xid _xid; /** * The time for this resource */ private int _timeout; //--- constructor /** * Create an XAResource associated with a XASession * * @param xaSession The session XAresource */ protected XAResourceImpl(XASessionImpl xaSession) { _xaSession = xaSession; } //--- The XAResource /** * Commits the global transaction specified by xid. * * @param xid A global transaction identifier * @param b If true, use a one-phase commit protocol to commit the work done on behalf of xid. * @throws XAException An error has occurred. 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. */ public void commit(Xid xid, boolean b) throws XAException { if (_logger.isDebugEnabled()) { _logger.debug("commit tx branch with xid: ", xid); } Future future = _xaSession.getQpidSession().dtxCommit(convertXid(xid), b ? Option.ONE_PHASE : Option.NONE); // now wait on the future for the result XaResult result = null; try { result = future.get(); } catch (SessionException e) { // we need to restore the qpid session that has been closed _xaSession.createSession(); convertExecutionErrorToXAErr(e.getException().getErrorCode()); } finally { _xid = null; } checkStatus(result.getStatus()); } /** * 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. *