/** rollback the resource */
  public void rollback(Xid xid) throws XAException {
    try {
      int endFlags = _endFlags;
      _endFlags = -1;

      if (endFlags != -1 && _isXATransaction) {
        boolean isValid = false;

        try {
          endResource(xid, endFlags);
          isValid = true;
        } finally {
          if (!isValid) _xaResource.rollback(xid);
        }
      }

      if (log.isLoggable(Level.FINER)) log.finer("connection pool rollback XA: " + this);

      if (_isXATransaction) _xaResource.rollback(xid);
      else if (_localTransaction != null) {
        try {
          _isLocalTransaction = false;
          _localTransaction.rollback();
        } catch (ResourceException e) {
          throw new XAExceptionWrapper(e);
        }
      }
    } finally {
      if (_xaResource != null) _isXATransaction = true;

      clearXid();
    }
  }
 /** @throws Exception */
 public void testDoCommit() throws Exception {
   Xid xid = new XidImpl();
   xares_.start(xid, XAResource.TMNOFLAGS);
   xares_.end(xid, XAResource.TMSUCCESS);
   xares_.commit(xid, true);
   assertEquals("1", true, con_.getAutoCommit());
 }
 /** @throws Exception */
 public void testDoRollback() throws Exception {
   Xid xid = new XidImpl();
   xares_.start(xid, XAResource.TMNOFLAGS);
   xares_.end(xid, XAResource.TMFAIL);
   xares_.rollback(xid);
   assertEquals("1", true, con_.getAutoCommit());
 }
  public ManagedPoolItem(ConnectionPool cm, ManagedConnectionFactory mcf, ManagedConnection conn) {
    _cm = cm;

    _id = _cm.generateId();

    _mcf = mcf;
    _mConn = conn;

    _poolStartTime = Alarm.getCurrentTime();
    _poolEventTime = Alarm.getCurrentTime();

    _connectionStartTime = cm.getConnectionTimeProbe().start();

    // Gets the resource object from the driver
    try {
      if (cm.isXATransaction()) {
        XAResource xaResource = conn.getXAResource();

        try {
          _defaultTransactionTimeout = xaResource.getTransactionTimeout();
        } catch (Throwable e) {
          log.log(Level.FINE, e.toString(), e);
        }

        _xaResource = xaResource;
      }
    } catch (NotSupportedException e) {
      _cm.setXATransaction(false);
      log.log(Level.FINER, e.toString(), e);
    } catch (Exception e) {
      log.log(Level.FINE, e.toString(), e);
    }

    if (_xaResource == null) _isXATransaction = false;

    // Gets the local transaction from the driver
    try {
      if (_cm.isLocalTransaction()) _localTransaction = conn.getLocalTransaction();
    } catch (NotSupportedException e) {
      _cm.setLocalTransaction(false);
      log.log(Level.FINER, e.toString(), e);
    } catch (Exception e) {
      log.log(Level.FINER, e.toString(), e);
    }

    _mConn.addConnectionEventListener(this);

    if (log.isLoggable(Level.FINE))
      log.fine(
          "create: "
              + this
              + "(active:"
              + _cm.getConnectionActiveCount()
              + ", total:"
              + _cm.getConnectionCount()
              + ")");
  }
 private void runXaResourceRollback() {
   Collection<XAResource> resources = getEnlistedResources();
   for (XAResource res : resources) {
     try {
       res.rollback(xid);
     } catch (XAException e) {
       log.warn("Error while rolling back", e);
     }
   }
 }
  /** Sets the transaction timeout */
  public boolean setTransactionTimeout(int seconds) throws XAException {
    if (seconds == _transactionTimeout) return true;

    XAResource xaResource = _xaResource;

    _transactionTimeout = seconds;

    if (xaResource == null) return true;
    else if (seconds == 0) return xaResource.setTransactionTimeout(_defaultTransactionTimeout);
    else return xaResource.setTransactionTimeout(seconds);
  }
 private boolean runXaResourceCommitTx() throws HeuristicMixedException {
   Collection<XAResource> resources = getEnlistedResources();
   for (XAResource res : resources) {
     try {
       res.commit(xid, false); // todo we only support one phase commit for now, change this!!!
     } catch (XAException e) {
       log.warn("exception while committing", e);
       throw new HeuristicMixedException(e.getMessage());
     }
   }
   return true;
 }
  /** commit the resource */
  public void commit(Xid xid, boolean onePhase) throws XAException {
    boolean logFiner = log.isLoggable(Level.FINER);

    try {
      int endFlags = _endFlags;
      _endFlags = -1;

      if (endFlags != -1 && _isXATransaction) {
        boolean isValid = false;

        try {
          endResource(xid, endFlags);
          isValid = true;
        } finally {
          if (!isValid) _xaResource.rollback(xid);
        }
      }

      if (_isXATransaction) {
        if (logFiner) {
          log.finer("commit-XA" + (onePhase ? "-1p: " : ": ") + xid + " " + _xaResource);
        }

        try {
          _xaResource.commit(xid, onePhase);
        } catch (XAException e) {
          if (logFiner) log.finer("commit-XA failed: " + _xaResource + " " + e);

          throw e;
        }
      } else if (_localTransaction != null) {
        if (logFiner) log.finer("commit-local: " + _localTransaction);

        try {
          _localTransaction.commit();
        } catch (ResourceException e) {
          if (logFiner) log.finer("commit failed: " + _localTransaction + " " + e);

          throw new XAExceptionWrapper(e);
        } finally {
          _isLocalTransaction = false;
        }
      } else {
        if (logFiner) log.finer("commit for resource with no XA support: " + this);
      }
    } finally {
      if (_xaResource != null) _isXATransaction = true;

      clearXid();
    }
  }
 private boolean runXaResourcePrepare() throws SystemException {
   Collection<XAResource> resources = getEnlistedResources();
   for (XAResource res : resources) {
     try {
       res.prepare(xid);
     } catch (XAException e) {
       log.trace("The resource wants to rollback!", e);
       return false;
     } catch (Throwable th) {
       log.error("Unexpected error from resource manager!", th);
       throw new SystemException(th.getMessage());
     }
   }
   return true;
 }
  /** Vote using phase-1 of the 2-phase commit. */
  public int prepare(Xid xid) throws XAException {
    if (_endFlags != -1) {
      int endFlags = _endFlags;
      _endFlags = -1;

      if (_isXATransaction) endResource(xid, endFlags);
    }

    if (_isXATransaction) {
      try {
        if (log.isLoggable(Level.FINER)) log.finer("prepare-XA: " + xid + " " + _xaResource);

        int result = _xaResource.prepare(xid);

        if (result == XA_RDONLY) {
          if (_xaResource != null) _isXATransaction = true;

          clearXid();
        }

        return result;
      } catch (XAException e) {
        if (log.isLoggable(Level.FINER))
          log.finer("failed prepare-XA: " + xid + " " + _xaResource + " " + e);

        throw e;
      }
    } else return XA_OK;
  }
 /** forget about the transaction */
 public void forget(Xid xid) throws XAException {
   try {
     if (_isXATransaction) _xaResource.forget(xid);
   } finally {
     clearXid();
   }
 }
 protected int doCommit(Xid xid, int cPhase) throws XAException {
   int returnVal = -1;
   synchronized (physicalConn) {
     synchronized (this) {
       int command = cPhase != 1 ? 2 : 4;
       returnVal = doTransaction(xid, 1, command);
       if (cPhase == 1 && (returnVal == 2 || returnVal == 4)) {
         returnVal = 0;
       } else if (cPhase != 1 && returnVal == 5) {
         returnVal = 0;
       } else if (returnVal == 8) {
         throw new XAException(106);
       }
       if (returnVal == 24756) {
         returnVal = kputxrec(xid, 1, timeout + 120);
       } else if (returnVal == 24780) {
         OracleXADataSource oxds = null;
         XAConnection pc = null;
         try {
           oxds = new OracleXADataSource();
           oxds.setURL(physicalConn.url);
           oxds.setUser(physicalConn.user);
           physicalConn.getPasswordInternal(this);
           oxds.setPassword(password);
           pc = oxds.getXAConnection();
           XAResource oxar = pc.getXAResource();
           oxar.commit(xid, cPhase == 1);
           returnVal = 0;
         } catch (SQLException e) {
           throw new XAException(-6);
         } finally {
           try {
             if (pc != null) {
               pc.close();
             }
             if (oxds != null) {
               oxds.close();
             }
           } catch (Exception ee) {
           }
         }
       }
     }
   }
   return returnVal;
 }
 protected int doRollback(Xid xid) throws XAException {
   int returnVal = -1;
   synchronized (physicalConn) {
     synchronized (this) {
       returnVal = doTransaction(xid, 2, 3);
       if (returnVal == 8) {
         throw new XAException(106);
       }
       if (returnVal == 3 || returnVal == 25402) {
         returnVal = 0;
       }
       if (returnVal == 24756) {
         returnVal = kputxrec(xid, 2, timeout + 120);
       } else if (returnVal == 24780) {
         OracleXADataSource oxds = null;
         XAConnection pc = null;
         try {
           oxds = new OracleXADataSource();
           oxds.setURL(physicalConn.url);
           oxds.setUser(physicalConn.user);
           physicalConn.getPasswordInternal(this);
           oxds.setPassword(password);
           pc = oxds.getXAConnection();
           XAResource oxar = pc.getXAResource();
           oxar.rollback(xid);
           returnVal = 0;
         } catch (SQLException e) {
           throw new XAException(-6);
         } finally {
           try {
             if (pc != null) {
               pc.close();
             }
             if (oxds != null) {
               oxds.close();
             }
           } catch (Exception ee) {
           }
         }
       }
     }
   }
   return returnVal;
 }
  /** starts work on a transaction branch */
  public void start(Xid xid, int flags) throws XAException {
    if (_xid != null) {
      if (log.isLoggable(Level.FINER)) log.finer("connection pool start XA: rejoin " + this);

      return;
    }

    if (flags == TMJOIN && _xid == null) {
      // TMJOIN means the resource manager is managing more than one
      // connection.  The delegates tie the PoolItems managed by
      // the same resource manager together.

      _xid = xid;

      UserTransactionImpl trans = _cm.getTransaction();

      if (trans != null) {
        ManagedPoolItem xaHead = trans.findJoin(this);

        if (xaHead != null) {
          _xaNext = xaHead._xaNext;
          _xaHead = xaHead;
          xaHead._xaNext = this;
        }
      }
    }

    // local transaction optimization
    if (!_isXATransaction && flags != TMJOIN && _localTransaction != null) {
      try {
        if (log.isLoggable(Level.FINER))
          log.finer("begin-local-XA: " + xid + " " + _localTransaction);

        _localTransaction.begin();
      } catch (ResourceException e) {
        throw new XAExceptionWrapper(e);
      }

      _xid = xid;

      return;
    }

    if (_xaResource != null) {
      if (log.isLoggable(Level.FINER)) log.finer("start-XA: " + xid + " " + _xaResource);

      _xaResource.start(xid, flags);
      _isXATransaction = true;
    } else {
      if (log.isLoggable(Level.FINER))
        log.finer("start-XA with non XA resource: " + xid + " " + _xaResource);
    }

    _xid = xid;
  }
Beispiel #15
0
 @Override
 public boolean isSameRM(XAResource xaResource) throws XAException {
   if (this == xaResource) {
     return true;
   }
   if (xaResource instanceof XAResourceImpl) {
     XAResourceImpl otherXaResource = (XAResourceImpl) xaResource;
     return groupName.equals(otherXaResource.groupName);
   }
   return xaResource.isSameRM(this);
 }
 public boolean enlistResource(XAResource xaResource)
     throws RollbackException, IllegalStateException, SystemException {
   enlistedResources.add(new WrappedXaResource(xaResource));
   try {
     xaResource.start(xid, 0);
   } catch (XAException e) {
     log.error("Got an exception", e);
     throw new SystemException(e.getMessage());
   }
   return true;
 }
Beispiel #17
0
 synchronized byte[] getBranchId(XAResource xaResource) {
   if (xaResource instanceof XaResource) {
     byte branchId[] = ((XaResource) xaResource).getBranchId();
     if (branchId != null) {
       return branchId;
     }
   }
   for (Map.Entry<String, XaDataSource> entry : dataSources.entrySet()) {
     XaDataSource dataSource = entry.getValue();
     XAResource resource = dataSource.getXaConnection().getXaResource();
     try {
       if (resource.isSameRM(xaResource)) {
         String name = entry.getKey();
         return sourceIdMapping.get(name);
       }
     } catch (XAException e) {
       throw new TransactionFailureException("Unable to check is same resource", e);
     }
   }
   throw new TransactionFailureException(
       "Unable to find mapping for XAResource[" + xaResource + "]");
 }
  /** identity of resources */
  public boolean isSameRM(XAResource resource) throws XAException {
    if (!(resource instanceof ManagedPoolItem)) return false;

    ManagedPoolItem poolItem = (ManagedPoolItem) resource;

    // if (_cm == poolItem._cm)
    //  return true;

    if (_xaResource == null) return false;

    boolean isSameRM = _xaResource.isSameRM(poolItem._xaResource);

    if (log.isLoggable(Level.FINER)) log.finer("isSameRM->" + isSameRM + " " + _xaResource);

    return isSameRM;
  }
 public synchronized boolean delistResource(XAResource xaRes, int flag)
     throws IllegalStateException {
   if (xaRes == null) {
     throw new IllegalArgumentException("Null xa resource");
   }
   if (flag != XAResource.TMSUCCESS && flag != XAResource.TMSUSPEND && flag != XAResource.TMFAIL) {
     throw new IllegalArgumentException("Illegal flag: " + flag);
   }
   ResourceElement re = null;
   Iterator<ResourceElement> itr = resourceList.iterator();
   while (itr.hasNext()) {
     ResourceElement reMatch = itr.next();
     if (reMatch.getResource() == xaRes) {
       re = reMatch;
       break;
     }
   }
   if (re == null) {
     return false;
   }
   if (status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK) {
     try {
       xaRes.end(re.getXid(), flag);
       if (flag == XAResource.TMSUSPEND || flag == XAResource.TMFAIL) {
         re.setStatus(RS_SUSPENDED);
       } else {
         re.setStatus(RS_DELISTED);
       }
       return true;
     } catch (XAException e) {
       e.printStackTrace();
       log.severe("Unable to delist resource[" + xaRes + "]");
       status = Status.STATUS_MARKED_ROLLBACK;
       return false;
     }
   }
   throw new IllegalStateException("Tx status is: " + txManager.getTxStatusAsString(status));
 }
  static List<Xid> recoverXids(XAResource xaResource, XidSelector selector) throws XAException {
    List<Xid> ret = new ArrayList<Xid>();

    boolean done = false;
    int flags = XAResource.TMSTARTRSCAN;
    Xid[] xidsFromLastScan = null;
    List<Xid> allRecoveredXidsSoFar = new ArrayList<Xid>();
    do {
      xidsFromLastScan = xaResource.recover(flags);
      flags = XAResource.TMNOFLAGS;
      done = (xidsFromLastScan == null || xidsFromLastScan.length == 0);
      if (!done) {

        // TEMPTATIVELY SET done TO TRUE
        // TO TOLERATE ORACLE 8.1.7 INFINITE
        // LOOP (ALWAYS RETURNS SAME RECOVER
        // SET). IF A NEW SET OF XIDS IS RETURNED
        // THEN done WILL BE RESET TO FALSE

        done = true;
        for (int i = 0; i < xidsFromLastScan.length; i++) {
          Xid xid = new XID(xidsFromLastScan[i]);
          // our own XID implements equals and hashCode properly
          if (!allRecoveredXidsSoFar.contains(xid)) {
            // a new xid is returned -> we can not be in a recovery loop -> go on
            allRecoveredXidsSoFar.add(xid);
            done = false;
            if (selector.selects(xid)) {
              ret.add(xid);
            }
          }
        }
      }
    } while (!done);

    return ret;
  }
 @Override
 public int getTransactionTimeout() throws XAException {
   return xaResource.getTransactionTimeout();
 }
 @Override
 public boolean setTransactionTimeout(int i) throws XAException {
   return xaResource.setTransactionTimeout(i);
 }
 @Override
 public void start(Xid xid, int i) throws XAException {
   xaResource.start(xid, i);
 }
 @Override
 public Xid[] recover(int i) throws XAException {
   return xaResource.recover(i);
 }
 @Override
 public void rollback(Xid xid) throws XAException {
   xaResource.rollback(xid);
 }
 @Override
 public int prepare(Xid xid) throws XAException {
   prepareResult = xaResource.prepare(xid);
   return prepareResult;
 }
 @Override
 public boolean isSameRM(XAResource xaResource) throws XAException {
   return xaResource.isSameRM(xaResource);
 }
 @Override
 public void commit(Xid xid, boolean b) throws XAException {
   // Commit only if not read only.
   if (prepareResult != XAResource.XA_RDONLY) xaResource.commit(xid, b);
   else log.tracef("Not committing {0} due to readonly.", xid);
 }
 @Override
 public void forget(Xid xid) throws XAException {
   xaResource.forget(xid);
 }
 @Override
 public void end(Xid xid, int i) throws XAException {
   xaResource.end(xid, i);
 }