Java Code Examples for javax.transaction.xa.XAException#initCause()

The following examples show how to use javax.transaction.xa.XAException#initCause() . 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: XAConnectionPoolingDataSource.java    From reladomo with Apache License 2.0 6 votes vote down vote up
@Override
public void commit(Xid xid, boolean b) throws XAException
{
    if(committed)
    {
        throw new XAException("Transaction is already committed");
    }
    try
    {
        if(connectionsInUse.size() != 0)
        {
            throw new XAException("Detected open jdbc connections. Change code to close all connection borrowed from the connection manager");
        }
        commitConnections(availableConnections);
        committed = true;
    }
    catch (SQLException e)
    {
        logger.error("Commit failed", e);
        rollbackConnections(availableConnections); // if commit fails, we must ensure we've already rolledback
        XAException xaException = new XAException("Commit Failed - "+e.getMessage());
        xaException.initCause(e);
        throw xaException;
    }
}
 
Example 2
Source File: ClientSessionImpl.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public void forget(final Xid xid) throws XAException {
   checkXA();
   startCall();
   try {
      sessionContext.xaForget(xid);
   } catch (XAException xae) {
      throw xae;
   } catch (Throwable t) {
      // This could occur if the TM interrupts the thread
      XAException xaException = new XAException(XAException.XAER_RMFAIL);
      xaException.initCause(t);
      throw xaException;
   } finally {
      endCall();
   }
}
 
Example 3
Source File: ClientSessionImpl.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public Xid[] recover(final int flags) throws XAException {
   checkXA();

   if ((flags & XAResource.TMSTARTRSCAN) == XAResource.TMSTARTRSCAN) {
      try {
         return sessionContext.xaScan();
      } catch (Throwable t) {
         // This could occur if the TM interrupts the thread
         XAException xaException = new XAException(XAException.XAER_RMFAIL);
         xaException.initCause(t);
         throw xaException;
      }
   }

   return new Xid[0];
}
 
Example 4
Source File: XATransactionState.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * This function is called from the timer task when the transaction
 * times out.
 *
 * @see CancelXATransactionTask
 */
private synchronized void cancel() throws XAException {
    // Check isFinished just to be sure that
    // the cancellation task was not started
    // just before the xa_commit/rollback
    // obtained this object's monitor.
    if (!isFinished) {
        // Check whether the transaction is associated
        // with any EmbedXAResource instance.
        if (associationState == XATransactionState.T1_ASSOCIATED) {
            conn.cancelRunningStatement();
            EmbedXAResource assocRes = associatedResource;
            end(assocRes, XAResource.TMFAIL, true);
        }

        // Rollback the global transaction
        try {
            conn.xa_rollback();
        } catch (SQLException sqle) {
            XAException ex = new XAException(XAException.XAER_RMERR);
            ex.initCause(sqle);
            throw ex;
        }

        // Do the cleanup on the resource
        creatingResource.returnConnectionToResource(this, xid);
    }
}
 
Example 5
Source File: XATransactionState.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * This function is called from the timer task when the transaction
 * times out.
 *
 * @see CancelXATransactionTask
 */
private synchronized void cancel() throws XAException {
    // Check isFinished just to be sure that
    // the cancellation task was not started
    // just before the xa_commit/rollback
    // obtained this object's monitor.
    if (!isFinished) {
        // Check whether the transaction is associated
        // with any EmbedXAResource instance.
        if (associationState == XATransactionState.T1_ASSOCIATED) {
            conn.cancelRunningStatement();
            EmbedXAResource assocRes = associatedResource;
            end(assocRes, XAResource.TMFAIL, true);
        }

        // Rollback the global transaction
        try {
            conn.xa_rollback();
        } catch (SQLException sqle) {
            XAException ex = new XAException(XAException.XAER_RMERR);
            ex.initCause(sqle);
            throw ex;
        }

        // Do the cleanup on the resource
        creatingResource.returnConnectionToResource(this, xid);
    }
}
 
Example 6
Source File: CacheJtaResource.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param msg Message.
 * @param cause Cause.
 * @throws XAException XA exception.
 */
private void throwException(String msg, Throwable cause) throws XAException {
    XAException ex = new XAException(msg);

    ex.initCause(cause);

    throw ex;
}
 
Example 7
Source File: ActiveMQRAXAResource.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Start
 *
 * @param xid   A global transaction identifier
 * @param flags One of TMNOFLAGS, TMJOIN, or TMRESUME
 * @throws XAException An error has occurred
 */
@Override
public void start(final Xid xid, final int flags) throws XAException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("start(" + xid + ", " + flags + ")");
   }

   managedConnection.lock();

   ClientSessionInternal sessionInternal = (ClientSessionInternal) xaResource;
   try {
      try {
         //this resets any tx stuff, we assume here that the tm and jca layer are well behaved when it comes to this
         sessionInternal.resetIfNeeded();
      } catch (ActiveMQException e) {
         ActiveMQRALogger.LOGGER.problemResettingXASession(e);

         XAException xaException = new XAException(XAException.XAER_RMFAIL);
         xaException.initCause(e);
         throw xaException;
      }

      xaResource.start(xid, flags);
   } finally {
      managedConnection.setInManagedTx(true);
      managedConnection.unlock();
   }
}
 
Example 8
Source File: ClientSessionImpl.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public void end(final Xid xid, final int flags) throws XAException {
   if (logger.isTraceEnabled()) {
      logger.trace("Calling end:: " + convert(xid) + ", flags=" + convertTXFlag(flags));
   }

   checkXA();

   try {
      if (rollbackOnly) {
         try {
            rollback(false, false);
         } catch (Throwable ignored) {
            logger.debug("Error on rollback during end call!", ignored);
         }
         throw new XAException(XAException.XAER_RMFAIL);
      }

      try {
         flushAcks();

         startCall();
         try {
            sessionContext.xaEnd(xid, flags);
         } finally {
            endCall();
         }
      } catch (XAException xae) {
         throw xae;
      } catch (Throwable t) {
         ActiveMQClientLogger.LOGGER.errorCallingEnd(t);
         // This could occur if the TM interrupts the thread
         XAException xaException = new XAException(XAException.XAER_RMFAIL);
         xaException.initCause(t);
         throw xaException;
      }
   } finally {
      currentXID = null;
   }
}
 
Example 9
Source File: ClientSessionImpl.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public int getTransactionTimeout() throws XAException {
   checkXA();

   try {
      return sessionContext.recoverSessionTimeout();
   } catch (Throwable t) {
      // This could occur if the TM interrupts the thread
      XAException xaException = new XAException(XAException.XAER_RMFAIL);
      xaException.initCause(t);
      throw xaException;
   }
}
 
Example 10
Source File: ClientSessionImpl.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public boolean setTransactionTimeout(final int seconds) throws XAException {
   checkXA();

   try {
      return sessionContext.configureTransactionTimeout(seconds);
   } catch (Throwable t) {
      markRollbackOnly(); // The TM will ignore any errors from here, if things are this screwed up we mark rollbackonly
      // This could occur if the TM interrupts the thread
      XAException xaException = new XAException(XAException.XAER_RMFAIL);
      xaException.initCause(t);
      throw xaException;
   }
}
 
Example 11
Source File: XATransactionState.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * This function is called from the timer task when the transaction
 * times out.
 *
 * @see CancelXATransactionTask
 */
synchronized void cancel(String messageId) throws XAException {
    // Check performTimeoutRollback just to be sure that
    // the cancellation task was not started
    // just before the xa_commit/rollback
    // obtained this object's monitor.
    if (performTimeoutRollback) {

        // Log the message about the transaction cancelled
        if (messageId != null)
            Monitor.logTextMessage(messageId, xid.toString());

        // Check whether the transaction is associated
        // with any EmbedXAResource instance.
        if (associationState == XATransactionState.T1_ASSOCIATED) {
            conn.cancelRunningStatement();
            EmbedXAResource assocRes = associatedResource;
            end(assocRes, XAResource.TMFAIL, true);
        }

        // Rollback the global transaction
        try {
            conn.xa_rollback();
        } catch (SQLException sqle) {
            XAException ex = new XAException(XAException.XAER_RMERR);
            ex.initCause(sqle);
            throw ex;
        }

        // Do the cleanup on the resource
        creatingResource.returnConnectionToResource(this, xid);
    }
}
 
Example 12
Source File: JtaXAResource.java    From hbase-secondary-index with GNU General Public License v3.0 5 votes vote down vote up
public void forget(final Xid xid) throws XAException {
    LOG.trace("forget [" + xid.toString() + "] ");
    threadLocalTransactionState.remove();
    TransactionState state = xidToTransactionState.remove(xid);
    if (state != null) {
        try {
            transactionManager.abort(state);
        } catch (IOException e) {
            XAException xae = new XAException(XAException.XAER_RMERR);
            xae.initCause(e);
            throw xae;
        }
    }
}
 
Example 13
Source File: XATerminatorImpl.java    From ByteJTA with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void invokeTwoPhaseCommit(XAResourceArchive archive) throws XAException {
	try {
		archive.commit(archive.getXid(), false);
	} catch (XAException xaex) {
		// * @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.
		switch (xaex.errorCode) {
		case XAException.XA_HEURHAZ:
			// OSI-TP: The condition that arises when, as a result of communication failure with a
			// subordinate, the bound data of the subordinate's subtree are in an unknown state.

			// XA: Due to some failure, the work done on behalf of the specified
			// transaction branch may have been heuristically completed.
		case XAException.XA_HEURMIX:
			// Due to a heuristic decision, the work done on behalf of the specified
			// transaction branch was partially committed and partially rolled back.
		case XAException.XA_HEURCOM:
			// Due to a heuristic decision, the work done on behalf of
			// the specified transaction branch was committed.
		case XAException.XA_HEURRB:
			// Due to a heuristic decision, the work done on behalf of
			// the specified transaction branch was rolled back.
			throw xaex;
		case XAException.XAER_NOTA:
			// The specified XID is not known by the resource manager.
			throw new XAException(XAException.XA_RDONLY); // read-only
		case XAException.XAER_RMFAIL:
			// An error occurred that makes the resource manager unavailable.
			throw xaex;
		case XAException.XAER_INVAL:
			// Invalid arguments were specified.
		case XAException.XAER_PROTO:
			// The routine was invoked in an improper context.
			XAException error = new XAException(XAException.XAER_RMERR);
			error.initCause(xaex);
			throw error;
		case XAException.XAER_RMERR:
			// An error occurred in committing the work performed on behalf of the transaction
			// branch and the branch’s work has been rolled back. Note that returning this error
			// signals a catastrophic event to a transaction manager since other resource
			// managers may successfully commit their work on behalf of this branch. This error
			// should be returned only when a resource manager concludes that it can never
			// commit the branch and that it cannot hold the branch’s resources in a prepared
			// state. Otherwise, [XA_RETRY] should be returned.
			throw xaex; // TODO XA_RETRY is not defined by the JTA specification
		default:// XA_RB*
			XAException xarb = new XAException(XAException.XA_HEURRB);
			xarb.initCause(xaex);
			throw xarb;
		}
	}
}
 
Example 14
Source File: XATerminatorImpl.java    From ByteJTA with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void invokeRollback(XAResourceArchive archive) throws XAException {
	try {
		archive.rollback(archive.getXid());
	} catch (XAException xaex) {
		// * @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 transaction branch is already marked rollback-only 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.
		switch (xaex.errorCode) {
		case XAException.XA_HEURHAZ:
			// Due to some failure, the work done on behalf of the specified transaction branch
			// may have been heuristically completed. A resource manager may return this
			// value only if it has successfully prepared xid.
		case XAException.XA_HEURMIX:
			// Due to a heuristic decision, the work done on behalf of the specified transaction
			// branch was partially committed and partially rolled back. A resource manager
			// may return this value only if it has successfully prepared xid.
		case XAException.XA_HEURCOM:
			// Due to a heuristic decision, the work done on behalf of the specified transaction
			// branch was committed. A resource manager may return this value only if it has
			// successfully prepared xid.
		case XAException.XA_HEURRB:
			// Due to a heuristic decision, the work done on behalf of the specified transaction
			// branch was rolled back. A resource manager may return this value only if it has
			// successfully prepared xid.
			throw xaex;
		case XAException.XAER_RMFAIL:
			// An error occurred that makes the resource manager unavailable.
			XAException xrhaz = new XAException(XAException.XA_HEURHAZ);
			xrhaz.initCause(xaex);
			throw xrhaz;
		case XAException.XAER_NOTA:
			// The specified XID is not known by the resource manager.
			if (archive.isReadonly()) {
				throw new XAException(XAException.XA_RDONLY);
			} else if (archive.getVote() == XAResourceArchive.DEFAULT_VOTE) {
				break; // rolled back
			} else if (archive.getVote() == XAResource.XA_RDONLY) {
				throw new XAException(XAException.XA_RDONLY);
			} else if (archive.getVote() == XAResource.XA_OK) {
				throw new XAException(XAException.XAER_RMERR);
			} else {
				throw new XAException(XAException.XAER_RMERR);
			}
		case XAException.XAER_PROTO:
			// The routine was invoked in an improper context.
		case XAException.XAER_INVAL:
			// Invalid arguments were specified.
			throw new XAException(XAException.XAER_RMERR);
		case XAException.XAER_RMERR:
			// An error occurred in rolling back the transaction branch. The resource manager is
			// free to forget about the branch when returning this error so long as all accessing
			// threads of control have been notified of the branch’s state.
		default: // XA_RB*
			// The resource manager has rolled back the transaction branch’s work and has
			// released all held resources. These values are typically returned when the
			// branch was already marked rollback-only.
			XAException xarb = new XAException(XAException.XA_HEURRB);
			xarb.initCause(xaex);
			throw xarb;
		}
	}
}
 
Example 15
Source File: LocalXAResource.java    From ByteJTA with GNU Lesser General Public License v3.0 4 votes vote down vote up
public synchronized void start(Xid xid, int flags) throws XAException {
	if (xid == null) {
		throw new XAException(XAException.XAER_INVAL);
	} else if (flags == XAResource.TMRESUME && this.suspendXid != null) {
		if (this.suspendXid.equals(xid)) {
			this.suspendXid = null;
			this.currentXid = xid;
			this.originalAutoCommit = this.suspendAutoCommit;
			this.suspendAutoCommit = true;
			return;
		} else {
			throw new XAException(XAException.XAER_PROTO);
		}
	} else if (flags == XAResource.TMJOIN) {
		if (this.currentXid == null) {
			throw new XAException(XAException.XAER_PROTO);
		}
	} else if (flags != XAResource.TMNOFLAGS) {
		throw new XAException(XAException.XAER_PROTO);
	} else if (this.currentXid != null) {
		throw new XAException(XAException.XAER_PROTO);
	} else {
		Connection connection = this.managedConnection.getPhysicalConnection();

		try {
			originalAutoCommit = connection.getAutoCommit();
		} catch (Exception ignored) {
			originalAutoCommit = true;
		}

		try {
			connection.setAutoCommit(false);
		} catch (Exception ex) {
			XAException xae = new XAException(XAException.XAER_RMERR);
			xae.initCause(ex);
			throw xae;
		}

		this.currentXid = xid;
	}
}