transactionimpl.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,204 行 · 第 1/3 页

JAVA
1,204
字号
    return true;  }  /**   * Adds an attribute.   */  public void setAttribute(String var, Object value)  {    if (_props == null)      _props = new HashMap<String,Object>();    _props.put(var, value);  }  /**   * Gets an attribute.   */  public Object getAttribute(String var)  {    if (_props != null)      return _props.get(var);    else      return null;  }  public Xid getXid()  {    return _xid;  }  /**   * Returns the status of this transaction   */  public int getStatus()  {    return _status;  }  /**   * Suspend the transaction.  The timeouts are stopped.   */  void suspend()    throws SystemException  {    if (_isSuspended)      throw new IllegalStateException(L.l("can't suspend already-suspended transaction"));    // _alarm.dequeue();    _isSuspended = true;    for (int i = _resourceCount - 1; i >= 0; i--) {      if ((_resourceState[i] & (RES_ACTIVE|RES_SUSPENDED)) == RES_ACTIVE) {        try {          XAResource resource = _resources[i];          resource.end(_resourceXid[i], XAResource.TMSUSPEND);        } catch (Exception e) {          setRollbackOnly(e);        }      }    }    if (_userTransaction != null)      _suspendState = _userTransaction.userSuspend();    if (log.isLoggable(Level.FINER))      log.fine(this + " suspended");  }  /**   * Resume the transaction and requeue the timeout.   */  void resume()    throws SystemException  {    if (! _isSuspended)      throw new IllegalStateException(L.l("can't resume non-suspended transaction"));    _alarm.queue(_timeout + EXTRA_TIMEOUT);    for (int i = _resourceCount - 1; i >= 0; i--) {      if ((_resourceState[i] & (RES_ACTIVE|RES_SUSPENDED)) == RES_ACTIVE) {        try {          XAResource resource = _resources[i];          resource.start(_resourceXid[i], XAResource.TMRESUME);        } catch (Exception e) {          setRollbackOnly(e);        }      }    }    if (_userTransaction != null)      _userTransaction.userResume(_suspendState);    _isSuspended = false;    if (log.isLoggable(Level.FINE))      log.fine(this + " resume");  }  /**   * Register a synchronization callback   */  public void registerSynchronization(Synchronization sync)  {    if (_syncList == null)      _syncList = new ArrayList<Synchronization>();    _syncList.add(sync);  }  /**   * Force any completion to be a rollback.   */  public void setRollbackOnly()    throws SystemException  {    if (_status != Status.STATUS_ACTIVE	&& _status != Status.STATUS_MARKED_ROLLBACK)      throw new IllegalStateException(L.l("can't set rollback-only"));    _status = Status.STATUS_MARKED_ROLLBACK;    _timeout = 0;    if (log.isLoggable(Level.FINE))      log.fine(this + " rollback-only");  }  /**   * Force any completion to be a rollback.   */  public void setRollbackOnly(Throwable exn)  {    if (_status != Status.STATUS_ACTIVE	&& _status != Status.STATUS_MARKED_ROLLBACK)      throw new IllegalStateException(L.l("can't set rollback-only"));    _status = Status.STATUS_MARKED_ROLLBACK;    if (_rollbackException == null)      _rollbackException = exn;    if (log.isLoggable(Level.FINE))      log.fine(this + " rollback-only: " + exn.toString());  }  /**   * Commit the transaction.   */  public void commit()    throws RollbackException, HeuristicMixedException,           HeuristicRollbackException, SystemException  {    _alarm.dequeue();    Exception heuristicExn = null;    try {      if (_status != Status.STATUS_ACTIVE) {        switch (_status) {        case Status.STATUS_MARKED_ROLLBACK:	  break;        case Status.STATUS_NO_TRANSACTION:          throw new IllegalStateException(L.l("Can't commit outside of a transaction.  Either the UserTransaction.begin() is missing or the transaction has already been committed or rolled back."));        default:          rollbackInt();          throw new IllegalStateException(L.l("can't commit {0}", String.valueOf(_status)));        }      }      if (log.isLoggable(Level.FINE))        log.fine(this + " committing");      try {	callBeforeCompletion();      } catch (RollbackException e) {	rollbackInt();      	throw e;      } catch (Throwable e) {	setRollbackOnly(e);	rollbackInt();      	throw new RollbackException(e);      }      if (_status == Status.STATUS_MARKED_ROLLBACK) {	rollbackInt();		if (_rollbackException != null)	  throw new RollbackExceptionWrapper(_rollbackException);	else	  throw new RollbackException(L.l("Transaction has been marked rolled back."));      }      if (_resourceCount > 0) {        _status = Status.STATUS_PREPARING;        AbstractXALogStream xaLog = _manager.getXALogStream();        boolean hasPrepare = false;        boolean allowSinglePhase = false;        for (int i = _resourceCount - 1; i >= 0; i--) {          XAResource resource = (XAResource) _resources[i];          if (i == 0 && (xaLog == null || ! hasPrepare)) {            // server/1601            _resourceState[0] |= RES_COMMIT;            allowSinglePhase = true;            break;          }          if ((_resourceState[i] & RES_SHARED_RM) == 0) {            try {              int prepare = resource.prepare(_resourceXid[i]);              if (prepare == XAResource.XA_RDONLY) {              }              else if (prepare == XAResource.XA_OK) {                hasPrepare = true;                _resourceState[i] |= RES_COMMIT;              }              else {                log.finer(this + " unexpected prepare result " + prepare);                rollbackInt();              }            } catch (XAException e) {              heuristicExn = heuristicException(heuristicExn, e);              rollbackInt();              throw new RollbackExceptionWrapper(L.l("all commits rolled back"),                                                 heuristicExn);            }          }        }        if (hasPrepare && xaLog != null) {          _xaLog = xaLog;          xaLog.writeTMCommit(_xid);        }        _status = Status.STATUS_COMMITTING;        if (allowSinglePhase) {          try {            XAResource resource = (XAResource) _resources[0];            if ((_resourceState[0] & RES_COMMIT) != 0)              resource.commit(_xid, true);            if (_timeout > 0)              resource.setTransactionTimeout(0);          } catch (XAException e) {            log.log(Level.FINE, e.toString(), e);            heuristicExn = heuristicException(heuristicExn, e);          }        }        for (int i = 0; i < _resourceCount; i++) {          XAResource resource = (XAResource) _resources[i];          if (i == 0 && allowSinglePhase)            continue;          if ((_resourceState[i] & RES_SHARED_RM) != 0)            continue;          if ((_resourceState[i] & RES_COMMIT) == 0)            continue;          if (heuristicExn == null) {            try {              resource.commit(_resourceXid[i], false);              if (_timeout > 0)                resource.setTransactionTimeout(0);            } catch (XAException e) {              heuristicExn = e;              log.log(Level.FINE, e.toString(), e);            }          }          else {            try {              resource.rollback(_resourceXid[i]);              if (_timeout > 0)                resource.setTransactionTimeout(0);            } catch (XAException e) {              log.log(Level.FINE, e.toString(), e);            }          }        }      }      if (heuristicExn != null && log.isLoggable(Level.FINE))        log.fine(this + " " + heuristicExn);      if (heuristicExn == null)        _status = Status.STATUS_COMMITTED;      else if (heuristicExn instanceof RollbackException) {        _status = Status.STATUS_ROLLEDBACK;        throw (RollbackException) heuristicExn;      }      else if (heuristicExn instanceof HeuristicMixedException) {        _status = Status.STATUS_ROLLEDBACK;        throw (HeuristicMixedException) heuristicExn;      }      else if (heuristicExn instanceof HeuristicRollbackException) {        _status = Status.STATUS_ROLLEDBACK;        throw (HeuristicRollbackException) heuristicExn;      }      else if (heuristicExn instanceof SystemException) {        _status = Status.STATUS_ROLLEDBACK;        throw (SystemException) heuristicExn;      }      else {        _status = Status.STATUS_ROLLEDBACK;        throw RollbackExceptionWrapper.create(heuristicExn);      }    } finally {      callAfterCompletion();    }  }  /**   * Rollback the transaction.   */  public void rollback()  {    _alarm.dequeue();    try {      callBeforeCompletion();    } catch (Throwable e) {      setRollbackOnly(e);    }    try {      switch (_status) {      case Status.STATUS_ACTIVE:      case Status.STATUS_MARKED_ROLLBACK:        // fall through to normal completion        break;      case Status.STATUS_NO_TRANSACTION:        throw new IllegalStateException(L.l("Can't rollback outside of a transaction.  Either the UserTransaction.begin() is missing or the transaction has already been committed or rolled back."));      default:        rollbackInt();        throw new IllegalStateException(L.l("Can't rollback in state: {0}", String.valueOf(_status)));      }      _status = Status.STATUS_MARKED_ROLLBACK;      rollbackInt();    } finally {      callAfterCompletion();    }  }  /**   * Calculates the heuristic exception based on the resource manager's   * exception.   */  private Exception heuristicException(Exception oldException,                                       XAException xaException)  {    switch (xaException.errorCode) {    case XAException.XA_HEURHAZ:    case XAException.XA_HEURCOM:      return oldException;    case XAException.XA_HEURRB:      if (oldException instanceof HeuristicMixedException)        return oldException;      else if (oldException instanceof HeuristicRollbackException)        return oldException;      else if (oldException instanceof RollbackException)        return oldException;      else        return new HeuristicRollbackException(getXAMessage(xaException));    default:      if (oldException instanceof SystemException)        return oldException;      else        return new SystemExceptionWrapper(getXAMessage(xaException),                                          xaException);    }  }  /**   * Rollback the transaction.   */  private void rollbackInt()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?