Java Code Examples for javax.transaction.xa.XAException#XAER_PROTO

The following examples show how to use javax.transaction.xa.XAException#XAER_PROTO . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalXAResourceImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void commit(Xid xid, boolean onePhase) throws XAException
{
   if (!xid.equals(currentXid))
   {
      throw new LocalXAException(bundle.wrongXidInCommit(currentXid, xid), XAException.XAER_PROTO);
      
   }
   
   currentXid = null;

   try
   {
      cl.getManagedConnection().getLocalTransaction().commit();
   }
   catch (LocalResourceException lre)
   {
      connectionManager.returnManagedConnection(cl, true);
      throw new LocalXAException(bundle.couldNotCommitLocalTx(), XAException.XAER_RMFAIL, lre);
   }
   catch (ResourceException re)
   {
      connectionManager.returnManagedConnection(cl, true);
      throw new LocalXAException(bundle.couldNotCommitLocalTx(), XAException.XA_RBROLLBACK, re);
   }
}
 
Example 2
Source File: FBManagedConnection.java    From jaybird with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Dissociates a resource from a global transaction.
 *
 * @throws XAException
 *         Occurs when the state was not correct (end called twice), or the transaction ID is wrong.
 */
private void end(Xid id, int flags) throws XAException {
    if (flags != XAResource.TMSUCCESS && flags != XAResource.TMFAIL && flags != XAResource.TMSUSPEND)
        throw new FBXAException("flag not allowed in this context: " + flags + ", valid flags are TMSUCCESS, TMFAIL, TMSUSPEND", XAException.XAER_PROTO);
    internalEnd(id, flags);
    mcf.notifyEnd(this, id);
    inDistributedTransaction = false;

    try {
        // This will reset the managed environment of the associated connections and set the transaction coordinator to local
        // TODO This is a bit of a hack; need to find a better way
        setManagedEnvironment(isManagedEnvironment());
    } catch (SQLException ex) {
        throw new FBXAException("Reset of managed state failed", XAException.XAER_RMERR, ex);
    }
}
 
Example 3
Source File: CompensableManagerImpl.java    From ByteTCC with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void begin() throws NotSupportedException, SystemException {
	XidFactory transactionXidFactory = this.beanFactory.getTransactionXidFactory();

	CompensableTransaction compensable = this.getCompensableTransactionQuietly();
	if (compensable == null || compensable.getTransaction() != null) {
		throw new SystemException(XAException.XAER_PROTO);
	}

	CompensableArchive archive = compensable.getCompensableArchive();

	// The current confirm/cancel operation has been assigned an xid.
	TransactionXid compensableXid = archive == null ? null : (TransactionXid) archive.getCompensableXid();
	TransactionXid transactionXid = compensableXid != null //
			? transactionXidFactory.createGlobalXid(compensableXid.getGlobalTransactionId())
			: transactionXidFactory.createGlobalXid();

	TransactionContext compensableContext = compensable.getTransactionContext();
	TransactionContext transactionContext = compensableContext.clone();
	transactionContext.setXid(transactionXid);

	this.invokeBegin(transactionContext, false);
}
 
Example 4
Source File: XATestUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Return a string for the error code of the XAException.
*/
public static String errorCode(XAException e)
{
    String error;
    switch(e.errorCode)
    {
    case XAException.XA_HEURCOM : error = "XA_HEURCOM "; break;
    case XAException.XA_HEURHAZ : error = "XA_HEURHAZ"; break;
    case XAException.XA_HEURMIX : error = "XA_HEURMIX"; break;
    case XAException.XA_HEURRB : error = "XA_HEURRB "; break;
    case XAException.XA_NOMIGRATE : error = "XA_NOMIGRATE "; break;
    case XAException.XA_RBCOMMFAIL : error = "XA_RBCOMMFAIL "; break;
    case XAException.XA_RBDEADLOCK : error = "XA_RBDEADLOCK "; break;
    case XAException.XA_RBINTEGRITY : error = "XA_RBINTEGRITY "; break;
    case XAException.XA_RBOTHER : error = "XA_RBOTHER "; break;
    case XAException.XA_RBPROTO : error = "XA_RBPROTO "; break;
    case XAException.XA_RBROLLBACK : error = "XA_RBROLLBACK "; break;
    case XAException.XA_RBTIMEOUT : error = "XA_RBTIMEOUT "; break;
    case XAException.XA_RBTRANSIENT : error = "XA_RBTRANSIENT "; break;
    case XAException.XA_RDONLY : error = "XA_RDONLY "; break;
    case XAException.XA_RETRY : error = "XA_RETRY "; break;
    case XAException.XAER_ASYNC : error = "XAER_ASYNC "; break;
    case XAException.XAER_DUPID : error = "XAER_DUPID "; break;
    case XAException.XAER_INVAL : error = "XAER_INVAL "; break;
    case XAException.XAER_NOTA : error = "XAER_NOTA "; break;
    case XAException.XAER_OUTSIDE : error = "XAER_OUTSIDE "; break;
    case XAException.XAER_PROTO : error = "XAER_PROTO "; break;
    case XAException.XAER_RMERR : error = "XAER_RMERR "; break;
    case XAException.XAER_RMFAIL : error = "XAER_RMFAIL "; break;
    default: error = Integer.toString(e.errorCode); break;
    }        
    return error;
}
 
Example 5
Source File: GenericXaResource.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * @see javax.transaction.xa.XAResource#end(javax.transaction.xa.Xid xid, int flag)
 */
public void end(Xid xid, int flag) throws XAException {
    if (!this.active) {
        throw new XAException(XAException.XAER_PROTO);
    }

    if (this.xid == null || !this.xid.equals(xid)) {
        throw new XAException(XAException.XAER_NOTA);
    }
    this.active = false;
}
 
Example 6
Source File: LocalXAResourceImpl.java    From ironjacamar with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void start(Xid xid, int flags) throws XAException
{
   if (trace)
      log.tracef("start(%s, %s)", xid, flags);  
   
   if (currentXid != null && flags == XAResource.TMNOFLAGS)
   {
      throw new LocalXAException(bundle.tryingStartNewTxWhenOldNotComplete(
            currentXid, xid, flags), XAException.XAER_PROTO);
   }
   
   if (currentXid == null && flags != XAResource.TMNOFLAGS)
   {
      throw new LocalXAException(bundle.tryingStartNewTxWithWrongFlags(xid, flags), XAException.XAER_PROTO);
   }

   if (currentXid == null)
   {
      try
      {
         cl.getManagedConnection().getLocalTransaction().begin();
      }
      catch (ResourceException re)
      {
         throw new LocalXAException(bundle.errorTryingStartLocalTx(), XAException.XAER_RMERR, re);
      }
      catch (Throwable t)
      {
         throw new LocalXAException(bundle.throwableTryingStartLocalTx(), XAException.XAER_RMERR, t);
      }

      currentXid = xid;
   }
}
 
Example 7
Source File: XATestUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Return a string for the error code of the XAException.
*/
public static String errorCode(XAException e)
{
    String error;
    switch(e.errorCode)
    {
    case XAException.XA_HEURCOM : error = "XA_HEURCOM "; break;
    case XAException.XA_HEURHAZ : error = "XA_HEURHAZ"; break;
    case XAException.XA_HEURMIX : error = "XA_HEURMIX"; break;
    case XAException.XA_HEURRB : error = "XA_HEURRB "; break;
    case XAException.XA_NOMIGRATE : error = "XA_NOMIGRATE "; break;
    case XAException.XA_RBCOMMFAIL : error = "XA_RBCOMMFAIL "; break;
    case XAException.XA_RBDEADLOCK : error = "XA_RBDEADLOCK "; break;
    case XAException.XA_RBINTEGRITY : error = "XA_RBINTEGRITY "; break;
    case XAException.XA_RBOTHER : error = "XA_RBOTHER "; break;
    case XAException.XA_RBPROTO : error = "XA_RBPROTO "; break;
    case XAException.XA_RBROLLBACK : error = "XA_RBROLLBACK "; break;
    case XAException.XA_RBTIMEOUT : error = "XA_RBTIMEOUT "; break;
    case XAException.XA_RBTRANSIENT : error = "XA_RBTRANSIENT "; break;
    case XAException.XA_RDONLY : error = "XA_RDONLY "; break;
    case XAException.XA_RETRY : error = "XA_RETRY "; break;
    case XAException.XAER_ASYNC : error = "XAER_ASYNC "; break;
    case XAException.XAER_DUPID : error = "XAER_DUPID "; break;
    case XAException.XAER_INVAL : error = "XAER_INVAL "; break;
    case XAException.XAER_NOTA : error = "XAER_NOTA "; break;
    case XAException.XAER_OUTSIDE : error = "XAER_OUTSIDE "; break;
    case XAException.XAER_PROTO : error = "XAER_PROTO "; break;
    case XAException.XAER_RMERR : error = "XAER_RMERR "; break;
    case XAException.XAER_RMFAIL : error = "XAER_RMFAIL "; break;
    default: error = Integer.toString(e.errorCode); break;
    }        
    return error;
}
 
Example 8
Source File: TransactionCoordinator.java    From ByteJTA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** supports resume only, for tcc transaction manager. */
public void start(Xid xid, int flags) throws XAException {
	if (XAResource.TMRESUME != flags) {
		throw new XAException(XAException.XAER_INVAL);
	}
	TransactionManager transactionManager = this.beanFactory.getTransactionManager();
	XidFactory xidFactory = this.beanFactory.getXidFactory();
	Transaction current = transactionManager.getTransactionQuietly();
	if (current != null) {
		throw new XAException(XAException.XAER_PROTO);
	}

	TransactionRepository transactionRepository = this.beanFactory.getTransactionRepository();

	TransactionXid branchXid = (TransactionXid) xid;
	TransactionXid globalXid = xidFactory.createGlobalXid(branchXid.getGlobalTransactionId());

	Transaction transaction = null;
	try {
		transaction = transactionRepository.getTransaction(globalXid);
	} catch (TransactionException tex) {
		throw new XAException(XAException.XAER_RMERR);
	}

	if (transaction == null) {
		throw new XAException(XAException.XAER_NOTA);
	}
	transactionManager.associateThread(transaction);
}
 
Example 9
Source File: EhcacheXAResource.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Override
public int prepare(Xid xid) throws XAException {
  if (currentXid != null) {
    throw new EhcacheXAException("Cannot prepare a non-ended start on : " + xid, XAException.XAER_PROTO);
  }
  TransactionId transactionId = new TransactionId(xid);
  XATransactionContext<K, V> transactionContext = transactionContextFactory.get(transactionId);
  if (transactionContext == null) {
    throw new EhcacheXAException("Cannot prepare unknown XID : " + xid, XAException.XAER_NOTA);
  }

  boolean destroyContext = false;
  try {
    destroyContext = transactionContext.prepare() == 0;
    return destroyContext ? XA_RDONLY : XA_OK;
  } catch (XATransactionContext.TransactionTimeoutException tte) {
    destroyContext = true;
    throw new EhcacheXAException("Transaction timed out", XAException.XA_RBTIMEOUT);
  } catch (IllegalStateException ise) {
    throw new EhcacheXAException("Cannot prepare XID : " + xid, XAException.XAER_PROTO, ise);
  } catch (StoreAccessException cae) {
    throw new EhcacheXAException("Cannot prepare XID : " + xid, XAException.XAER_RMERR, cae);
  } finally {
    if (destroyContext) {
      transactionContextFactory.destroy(transactionId);
    }
  }
}
 
Example 10
Source File: xaHelper.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private void handleException(Throwable t) throws SQLException
{
	if (t instanceof SQLException)
	{
		// let ij handle it
		throw (SQLException)t;
	}
	if (t instanceof XAException)
	{
		int errorCode = ((XAException)t).errorCode;
		String error = LocalizedResource.getMessage("IJ_IlleValu");

		// XA_RBBASE 100
		// XA_RBROLLBACK 100
		// XA_RBCOMMFAIL 101
		// XA_RBDEADLOCK 102
		// XA_RBINTEGRITY 103
		// XA_RBOTHER 104
		// XA_RBPROTO 105
		// XA_RBTIMEOUT 106
		// XA_RBTRANSIENT 107
		// XA_RBEND 107
		//
		// XA_RDONLY 3
		// XA_RETRY 4
		// XA_HEURMIX 5
		// XA_HEURRB 6
		// XA_HEURCOM 7
		// XA_HEURHAZ 8
		// XA_NOMIGRATE 9
		//
		// XAER_ASYNC -2
		// XAER_RMERR -3
		// XAER_NOTA -4
		// XAER_INVAL -5
		// XAER_PROTO -6
		// XAER_RMFAIL -7
		// XAER_DUPID -8
		// XAER_OUTSIDE -9

		switch(errorCode)
		{
		case XAException.XA_HEURCOM : error = "XA_HEURCOM "; break;
		case XAException.XA_HEURHAZ : error = "XA_HEURHAZ"; break;
		case XAException.XA_HEURMIX : error = "XA_HEURMIX"; break;
		case XAException.XA_HEURRB : error = "XA_HEURRB "; break;
		case XAException.XA_NOMIGRATE : error = "XA_NOMIGRATE "; break;
			// case XAException.XA_RBBASE : error = "XA_RBBASE "; break;
		case XAException.XA_RBCOMMFAIL : error = "XA_RBCOMMFAIL "; break;
		case XAException.XA_RBDEADLOCK : error = "XA_RBDEADLOCK "; break;
			// case XAException.XA_RBEND : error = "XA_RBEND "; break;
		case XAException.XA_RBINTEGRITY : error = "XA_RBINTEGRITY "; break;
		case XAException.XA_RBOTHER : error = "XA_RBOTHER "; break;
		case XAException.XA_RBPROTO : error = "XA_RBPROTO "; break;
		case XAException.XA_RBROLLBACK : error = "XA_RBROLLBACK "; break;
		case XAException.XA_RBTIMEOUT : error = "XA_RBTIMEOUT "; break;
		case XAException.XA_RBTRANSIENT : error = "XA_RBTRANSIENT "; break;
		case XAException.XA_RDONLY : error = "XA_RDONLY "; break;
		case XAException.XA_RETRY : error = "XA_RETRY "; break;
		case XAException.XAER_ASYNC : error = "XAER_ASYNC "; break;
		case XAException.XAER_DUPID : error = "XAER_DUPID "; break;
		case XAException.XAER_INVAL : error = "XAER_INVAL "; break;
		case XAException.XAER_NOTA : error = "XAER_NOTA "; break;
		case XAException.XAER_OUTSIDE : error = "XAER_OUTSIDE "; break;
		case XAException.XAER_PROTO : error = "XAER_PROTO "; break;
		case XAException.XAER_RMERR : error = "XAER_RMERR "; break;
		case XAException.XAER_RMFAIL : error = "XAER_RMFAIL "; break;
		}
		//t.printStackTrace(System.out);
		throw new ijException(error);

	}
	else // StandardException or run time exception, log it first
	{
		String info = LocalizedResource.getMessage("IJ_01SeeLog", t.toString(), t.getMessage());
		//		t.printStackTrace(System.out);
		throw new ijException(info);
	}
}
 
Example 11
Source File: ServerSessionImpl.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void xaRollback(final Xid xid) throws Exception {
   this.pendingTX = null;

   if (tx != null && tx.getXid().equals(xid)) {
      final String msg = "Cannot roll back, session is currently doing work in a transaction " + tx.getXid();

      throw new ActiveMQXAException(XAException.XAER_PROTO, msg);
   } else {
      Transaction theTx = resourceManager.removeTransaction(xid);
      if (logger.isTraceEnabled()) {
         logger.trace("xarollback into " + theTx);
      }

      if (theTx == null) {
         // checked heuristic committed transactions
         if (resourceManager.getHeuristicCommittedTransactions().contains(xid)) {
            throw new ActiveMQXAException(XAException.XA_HEURCOM, "transaction has ben heuristically committed: " + xid);
         } else if (resourceManager.getHeuristicRolledbackTransactions().contains(xid)) {
            // checked heuristic rolled back transactions
            throw new ActiveMQXAException(XAException.XA_HEURRB, "transaction has ben heuristically rolled back: " + xid);
         } else {
            if (logger.isTraceEnabled()) {
               logger.trace("xarollback into " + theTx + ", xid=" + xid + " forcing a rollback regular");
            }

            try {
               // jbpapp-8845
               // This could have happened because the TX timed out,
               // at this point we would be better on rolling back this session as a way to prevent consumers from holding their messages
               this.rollback(false);
            } catch (Exception e) {
               ActiveMQServerLogger.LOGGER.unableToRollbackOnTxTimedOut(e);
            }

            throw new ActiveMQXAException(XAException.XAER_NOTA, "Cannot find xid in resource manager: " + xid);
         }
      } else {
         if (theTx.getState() == Transaction.State.SUSPENDED) {
            if (logger.isTraceEnabled()) {
               logger.trace("xarollback into " + theTx + " sending tx back as it was suspended");
            }

            // Put it back
            resourceManager.putTransaction(xid, tx);

            throw new ActiveMQXAException(XAException.XAER_PROTO, "Cannot rollback transaction, it is suspended " + xid);
         } else {
            doRollback(false, false, theTx);
         }
      }
   }
}
 
Example 12
Source File: XATransactionState.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
void start(EmbedXAResource resource, int flags) throws XAException {

		synchronized (this) {
			if (associationState == XATransactionState.TRO_FAIL)
				throw new XAException(rollbackOnlyCode);

			boolean isSuspendedByResource = (suspendedList != null) && (suspendedList.get(resource) != null);

			if (flags == XAResource.TMRESUME) {
				if (!isSuspendedByResource)
					throw new XAException(XAException.XAER_PROTO);

			} else {
				// cannot join a transaction we have suspended.
				if (isSuspendedByResource)
					throw new XAException(XAException.XAER_PROTO);
			}

			while (associationState == XATransactionState.T1_ASSOCIATED) {
				
				try {
					wait();
				} catch (InterruptedException ie) {
					throw new XAException(XAException.XA_RETRY);
				}
			}


			switch (associationState) {
			case XATransactionState.T0_NOT_ASSOCIATED:
				break;

            case XATransactionState.TRO_DEADLOCK:
            case XATransactionState.TRO_TIMEOUT:
			case XATransactionState.TRO_FAIL:
				throw new XAException(rollbackOnlyCode);

			default:
				throw new XAException(XAException.XAER_NOTA);
			}

			if (isPrepared)
				throw new XAException(XAException.XAER_PROTO);

			if (isSuspendedByResource) {
				suspendedList.remove(resource);
			}

			associationState = XATransactionState.T1_ASSOCIATED;
			associatedResource = resource;

		}
	}
 
Example 13
Source File: TransactionRecoveryImpl.java    From ByteTCC with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void recoverCoordinator(Transaction transaction)
		throws CommitRequiredException, RollbackRequiredException, SystemException {
	CompensableManager compensableManager = this.beanFactory.getCompensableManager();
	TransactionLock compensableLock = this.beanFactory.getCompensableLock();

	org.bytesoft.transaction.TransactionContext transactionContext = transaction.getTransactionContext();
	TransactionXid xid = transactionContext.getXid();

	boolean forgetRequired = false;
	boolean locked = false;
	try {
		compensableManager.associateThread(transaction);

		switch (transaction.getTransactionStatus()) {
		case Status.STATUS_ACTIVE:
		case Status.STATUS_MARKED_ROLLBACK:
		case Status.STATUS_PREPARING:
		case Status.STATUS_UNKNOWN: /* TODO */ {
			if (transactionContext.isPropagated() == false) {
				if ((locked = compensableLock.lockTransaction(xid, this.endpoint)) == false) {
					throw new SystemException(XAException.XAER_PROTO);
				}

				transaction.recoveryRollback();
				forgetRequired = true;
			}
			break;
		}
		case Status.STATUS_ROLLING_BACK: {
			if ((locked = compensableLock.lockTransaction(xid, this.endpoint)) == false) {
				throw new SystemException(XAException.XAER_PROTO);
			}

			transaction.recoveryRollback();
			forgetRequired = true;
			break;
		}
		case Status.STATUS_PREPARED:
		case Status.STATUS_COMMITTING: {
			if ((locked = compensableLock.lockTransaction(xid, this.endpoint)) == false) {
				throw new SystemException(XAException.XAER_PROTO);
			}

			transaction.recoveryCommit();
			forgetRequired = true;
			break;
		}
		case Status.STATUS_COMMITTED:
		case Status.STATUS_ROLLEDBACK:
			forgetRequired = true;
			break;
		default: // ignore
		}
	} finally {
		compensableManager.desociateThread();
		if (locked) {
			compensableLock.unlockTransaction(xid, this.endpoint);
		} // end-if (locked)
		if (forgetRequired) {
			transaction.forgetQuietly(); // forget transaction
		} // end-if (forgetRequired)
	}

}
 
Example 14
Source File: EmbedXAResource.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Inform the resource manager to roll back work done on behalf of a
 * transaction branch
 *
 * @param xid A global transaction identifier
 * @exception XAException - An error has occurred
 */
public final synchronized void rollback(Xid xid) throws XAException {
    checkXAActive();
    
    // ensure immtable and correct equals method.
    XAXactId xid_im = new XAXactId(xid);
    
    XATransactionState tranState = getTransactionState(xid_im);
    
    if (tranState == null) {
      // Gemstone changes BEGIN
      throw new XAException(XAException.XAER_NOTA);
      /* original code
        XAResourceManager rm = ra.getXAResourceManager();
        
        ContextManager inDoubtCM = rm.find(xid);
        
        // RM also does not know about this xid.
        if (inDoubtCM == null)
            throw new XAException(XAException.XAER_NOTA);
        
        ContextService csf = ContextService.getFactory();
        
        csf.setCurrentContextManager(inDoubtCM);
        try {
            rm.rollback(inDoubtCM, xid_im);
            
            // close the connection/transaction since it can never be used again.
            inDoubtCM.cleanupOnError(StandardException.closeException());
            return;
        } catch (StandardException se) {
            // The rm threw an exception, clean it up in the approprate
            // context.  There is no transactionResource to handle the
            // exception for us.
            inDoubtCM.cleanupOnError(se);
            throw wrapInXAException(se);
        } finally {
            csf.resetCurrentContextManager(inDoubtCM);
        }
      */
   // Gemstone changes END
    }
    
    synchronized (tranState) {
        
        // Check the transaction is no associated with
        // any XAResource.
        switch (tranState.associationState) {
            case XATransactionState.T0_NOT_ASSOCIATED:
            case XATransactionState.TRO_FAIL:
                break;
                
            default:
                throw new XAException(XAException.XAER_PROTO);
        }
        
        if (tranState.suspendedList != null 
                && tranState.suspendedList.size() != 0)
            throw new XAException(XAException.XAER_PROTO);
        
        checkUserCredentials(tranState.creatingResource);
        
        try {
            
            tranState.xa_rollback();
        } catch (SQLException sqle) {
            throw wrapInXAException(sqle);
        } finally {
            returnConnectionToResource(tranState, xid_im);
        }
    }
}
 
Example 15
Source File: EmbedXAResource.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Tell the resource manager to forget about a heuristically completed
 * transaction branch.
 *
 * @param xid A global transaction identifier
 * @exception XAException An error has occurred. Possible exception values
 * are XAER_RMERR, XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO.
 */
public final synchronized void forget(Xid xid) throws XAException {
    
    checkXAActive();
    
    // ensure immtable and correct equals method.
    XAXactId xid_im = new XAXactId(xid);
    
    XATransactionState tranState = getTransactionState(xid_im);
    if (tranState == null) {
        XAResourceManager rm = ra.getXAResourceManager();
        
        ContextManager inDoubtCM = rm.find(xid);
        
        // RM also does not know about this xid.
        if (inDoubtCM == null)
            throw new XAException(XAException.XAER_NOTA);
        
        ContextService csf = ContextService.getFactory();
        
        csf.setCurrentContextManager(inDoubtCM);
        try {
            rm.forget(inDoubtCM, xid_im);
            
            // close the connection/transaction since it can never be used again.
            inDoubtCM.cleanupOnError(StandardException.closeException(),
                    false);
            return;
        } catch (StandardException se) {
            // The rm threw an exception, clean it up in the approprate
            // context.  There is no transactionResource to handle the
            // exception for us.
            inDoubtCM.cleanupOnError(se, con.isActive());
            throw wrapInXAException(se);
        } finally {
            csf.resetCurrentContextManager(inDoubtCM);
        }
        
    }
    
    // DERBY-1016; if the transaction exists throw XAER_PROTO on forget
    throw new XAException(XAException.XAER_PROTO);
}
 
Example 16
Source File: ServerSessionImpl.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void xaCommit(final Xid xid, final boolean onePhase) throws Exception {
   this.pendingTX = null;

   if (tx != null && tx.getXid().equals(xid)) {
      final String msg = "Cannot commit, session is currently doing work in transaction " + tx.getXid();

      throw new ActiveMQXAException(XAException.XAER_PROTO, msg);
   } else {
      Transaction theTx = resourceManager.removeTransaction(xid);

      if (logger.isTraceEnabled()) {
         logger.trace("XAcommit into " + theTx + ", xid=" + xid);
      }

      if (theTx == null) {
         // checked heuristic committed transactions
         if (resourceManager.getHeuristicCommittedTransactions().contains(xid)) {
            throw new ActiveMQXAException(XAException.XA_HEURCOM, "transaction has been heuristically committed: " + xid);
         } else if (resourceManager.getHeuristicRolledbackTransactions().contains(xid)) {
            // checked heuristic rolled back transactions
            throw new ActiveMQXAException(XAException.XA_HEURRB, "transaction has been heuristically rolled back: " + xid);
         } else {
            if (logger.isTraceEnabled()) {
               logger.trace("XAcommit into " + theTx + ", xid=" + xid + " cannot find it");
            }

            throw new ActiveMQXAException(XAException.XAER_NOTA, "Cannot find xid in resource manager: " + xid);
         }
      } else {
         if (theTx.getState() == Transaction.State.SUSPENDED) {
            // Put it back
            resourceManager.putTransaction(xid, theTx);

            throw new ActiveMQXAException(XAException.XAER_PROTO, "Cannot commit transaction, it is suspended " + xid);
         } else {
            theTx.commit(onePhase);
         }
      }
   }
}
 
Example 17
Source File: EmbedXAResource.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Ends the work performed on behalf of a transaction branch. The resource
 * manager disassociates the XA resource from the transaction branch
 * specified and let the transaction be completed.
 *
 * <p> If TMSUSPEND is specified in flags, the transaction branch is
 * temporarily suspended in incomplete state. The transaction context
 * is in suspened state and must be resumed via start with TMRESUME
 * specified.
 *
 * <p> If TMFAIL is specified, the portion of work has failed. The
 * resource manager may mark the transaction as rollback-only
 *
 * <p> If TMSUCCESS is specified, the portion of work has completed
 * successfully.
 *
 * @param xid A global transaction identifier that is the same as what was
 * used previously in the start 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*.
 */
public final synchronized void end(Xid xid, int flags) throws XAException {
    checkXAActive();
    
    try {
        // It is possible that the isolation level state in connection
        // handle has gotten out of sync with the real isolation level.
        // This can happen if SLQ instead of JDBC api has been used to
        // set the isolation level. The code below will check if isolation
        // was set using JDBC or SQL and if yes, then it will update the
        // isolation state in BrokeredConnection with EmbedConnection's
        // isolation level.
        if (con.currentConnectionHandle != null)
            con.currentConnectionHandle.getIsolationUptoDate();
    } catch (SQLException sqle) {
        throw wrapInXAException(sqle);
    }
    
    // ensure immtable and correct equals method.
    XAXactId xid_im = new XAXactId(xid);
    
    boolean endingCurrentXid = false;
    
    // must match the Xid from start()
    if (currentXid != null) {
        if (!currentXid.equals(xid_im))
            throw new XAException(XAException.XAER_PROTO);
        endingCurrentXid = true;
    }
    
    XATransactionState tranState = getTransactionState(xid_im);
    if (tranState == null)
        throw new XAException(XAException.XAER_NOTA);
    
    boolean rollbackOnly = tranState.end(this, flags, endingCurrentXid);
    
    // RESOLVE - what happens to the connection on a fail
    // where we are not ending the current XID.
    if (endingCurrentXid) {
        currentXid = null;            
        con.realConnection = null;
    }
    
    if (rollbackOnly)
        throw new XAException(tranState.rollbackOnlyCode);        
}
 
Example 18
Source File: FBManagedConnection.java    From jaybird with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * The {@code internalCommit} method performs the requested commit and may throw an XAException to be interpreted
 * by the caller.
 *
 * @param xid
 *         a {@code Xid} value
 * @param onePhase
 *         a {@code true} if this is not a two-phase commit (not a distributed transaction)
 * @throws XAException
 *         if an error occurs
 */
void internalCommit(Xid xid, boolean onePhase) throws XAException {
    if (log.isTraceEnabled()) log.trace("Commit called: " + xid);
    FbTransaction committingTr = xidMap.get(xid);

    // check that prepare has NOT been called when onePhase = true
    if (onePhase && isPrepared(xid)) {
        throw new FBXAException("Cannot commit one-phase when transaction has been prepared", XAException.XAER_PROTO);
    }

    // check that prepare has been called when onePhase = false
    if (!onePhase && !isPrepared(xid)) {
        throw new FBXAException("Cannot commit two-phase when transaction has not been prepared", XAException.XAER_PROTO);
    }

    if (committingTr == null) {
        throw new FBXAException("Commit called with unknown transaction", XAException.XAER_NOTA);
    }

    try {
        if (committingTr == getGDSHelper().getCurrentTransaction()) {
            throw new FBXAException("Commit called with non-ended xid", XAException.XAER_PROTO);
        }

        committingTr.commit();
    } catch (SQLException ge) {
        if (gdsHelper != null) {
            try {
                committingTr.rollback();
            } catch (SQLException ge2) {
                log.debug("Exception rolling back failed tx: ", ge2);
            }
        } else {
            log.warn("Unable to rollback failed tx, connection closed or lost");
        }
        throw new FBXAException(ge.getMessage(), XAException.XAER_RMERR, ge);
    } finally {
        xidMap.remove(xid);
        preparedXid.remove(xid);
    }
}
 
Example 19
Source File: TransactionImpl.java    From ByteJTA with GNU Lesser General Public License v3.0 4 votes vote down vote up
public synchronized void cleanup() throws SystemException {
	boolean unFinishExists = false;

	for (int i = 0; i < this.participantList.size(); i++) {
		XAResourceArchive archive = this.participantList.get(i);
		Xid currentXid = archive.getXid();
		if (archive.isHeuristic()) {
			try {
				Xid branchXid = archive.getXid();
				archive.forget(branchXid);
			} catch (XAException xae) {
				// Possible exception values are XAER_RMERR, XAER_RMFAIL
				// , XAER_NOTA, XAER_INVAL, or XAER_PROTO.
				switch (xae.errorCode) {
				case XAException.XAER_RMERR:
					unFinishExists = true;
					logger.error("{}> forget: xares= {}, branch={}, error= {}",
							ByteUtils.byteArrayToString(currentXid.getGlobalTransactionId()), archive,
							ByteUtils.byteArrayToString(currentXid.getBranchQualifier()), xae.errorCode);
					break;
				case XAException.XAER_RMFAIL:
					unFinishExists = true;
					logger.error("{}> forget: xares= {}, branch={}, error= {}",
							ByteUtils.byteArrayToString(currentXid.getGlobalTransactionId()), archive,
							ByteUtils.byteArrayToString(currentXid.getBranchQualifier()), xae.errorCode);
					break;
				case XAException.XAER_NOTA:
				case XAException.XAER_INVAL:
				case XAException.XAER_PROTO:
					break;
				default:
					unFinishExists = true;
					logger.error("{}> forget: xares= {}, branch={}, error= {}",
							ByteUtils.byteArrayToString(currentXid.getGlobalTransactionId()), archive,
							ByteUtils.byteArrayToString(currentXid.getBranchQualifier()), xae.errorCode);
				}
			}
		} // end-if
	} // end-for

	if (unFinishExists) {
		throw new SystemException("Error occurred while cleaning branch transaction!");
	}

}
 
Example 20
Source File: EmbedXAResource.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Commit the global transaction specified by xid.
 * @param xid 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 paramether 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.
 */    
public final synchronized void commit(Xid xid, boolean onePhase) 
                                        throws XAException {
    checkXAActive();
    // ensure immtable and correct equals method.
    XAXactId xid_im = new XAXactId(xid);
    XATransactionState tranState = getTransactionState(xid_im);
    
    if (tranState == null) {
      // Gemstone changes BEGIN
      throw new XAException(XAException.XAER_NOTA);
      /* orginal code
        XAResourceManager rm = ra.getXAResourceManager();
        ContextManager inDoubtCM = rm.find(xid);
        // RM also does not know about this xid.
        if (inDoubtCM == null)
            throw new XAException(XAException.XAER_NOTA);
        ContextService csf = ContextService.getFactory();
        csf.setCurrentContextManager(inDoubtCM);
        try {
            rm.commit(inDoubtCM, xid_im, onePhase);
            
            // close the connection/transaction since it can never
            // be used again.
            inDoubtCM.cleanupOnError(StandardException.closeException());
            return;
        } catch (StandardException se) {
            // The rm threw an exception, clean it up in the approprate
            // context.  There is no transactionResource to handle the
            // exception for us.
            inDoubtCM.cleanupOnError(se);
            throw wrapInXAException(se);
        } finally {
            csf.resetCurrentContextManager(inDoubtCM);
        }
        */
       // Gemstone changes END
    }
    
    synchronized (tranState) {
        checkUserCredentials(tranState.creatingResource);
        
        // Check the transaction is no associated with
        // any XAResource.
        switch (tranState.associationState) {
            case XATransactionState.T0_NOT_ASSOCIATED:
                break;
                
            case XATransactionState.TRO_FAIL:
                throw new XAException(tranState.rollbackOnlyCode);
                
            default:
                throw new XAException(XAException.XAER_PROTO);
        }
        
        if (tranState.suspendedList != null && tranState.suspendedList.size() != 0)
            throw new XAException(XAException.XAER_PROTO);
        
        if (tranState.isPrepared == onePhase)
            throw new XAException(XAException.XAER_PROTO);
        
        try {
            tranState.xa_commit(onePhase);
        } catch (SQLException sqle) {
            throw wrapInXAException(sqle);
        } finally {
            returnConnectionToResource(tranState, xid_im);
        }
    }
}