Java Code Examples for javax.transaction.xa.XAResource#TMSTARTRSCAN

The following examples show how to use javax.transaction.xa.XAResource#TMSTARTRSCAN . 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: 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 2
Source File: EhcacheXAResource.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Override
public Xid[] recover(int flags) throws XAException {
  if (flags != XAResource.TMNOFLAGS && flags != XAResource.TMSTARTRSCAN && flags != XAResource.TMENDRSCAN && flags != (XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN)) {
    throw new EhcacheXAException("Recover flags not supported : " + xaResourceFlagsToString(flags), XAException.XAER_INVAL);
  }

  if ((flags & XAResource.TMSTARTRSCAN) == XAResource.TMSTARTRSCAN) {
    List<Xid> xids = new ArrayList<>();
    Set<TransactionId> transactionIds = journal.recover().keySet();
    for (TransactionId transactionId : transactionIds) {
      // filter-out in-flight tx
      if (!transactionContextFactory.contains(transactionId)) {
        xids.add(transactionId.getSerializableXid());
      }
    }
    return xids.toArray(new Xid[xids.size()]);
  }
  return new Xid[0];
}
 
Example 3
Source File: ResourceAdapterImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public Xid[] recover(int flags) throws StandardException {
  XAXactId[] ret_xid_list;

  if ((flags & XAResource.TMSTARTRSCAN) != 0) {
    XAXactId[] xid_list = new XAXactId[connectionTable.size()];
    int num_prepared = 0;

    // Need to hold sync while linear searching the hash table.
    synchronized (connectionTable) {
      int i = 0;

      for (Enumeration e = connectionTable.elements(); e.hasMoreElements(); i++) {
        XATransactionState xaTxnState = (XATransactionState)e.nextElement();

        if (xaTxnState.isPrepared()) {
          xid_list[i] = xaTxnState.getXId();
          num_prepared++;
        }
      }
    }

    // now need to squish the nulls out of the array to return.
    ret_xid_list = new XAXactId[num_prepared];
    int ret_index = 0;
    for (int i = xid_list.length; i-- > 0;) {
      if (xid_list[i] != null)
        ret_xid_list[ret_index++] = xid_list[i];
    }

    if (SanityManager.DEBUG) {
      SanityManager.ASSERT(ret_index == num_prepared);
    }
  }
  else {
    ret_xid_list = new XAXactId[0];
  }

  return (ret_xid_list);
}
 
Example 4
Source File: ResourceAdapterImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public Xid[] recover(int flags) throws StandardException {
  XAXactId[] ret_xid_list;

  if ((flags & XAResource.TMSTARTRSCAN) != 0) {
    XAXactId[] xid_list = new XAXactId[connectionTable.size()];
    int num_prepared = 0;

    // Need to hold sync while linear searching the hash table.
    synchronized (connectionTable) {
      int i = 0;

      for (Enumeration e = connectionTable.elements(); e.hasMoreElements(); i++) {
        XATransactionState xaTxnState = (XATransactionState)e.nextElement();

        if (xaTxnState.isPrepared()) {
          xid_list[i] = xaTxnState.getXId();
          num_prepared++;
        }
      }
    }

    // now need to squish the nulls out of the array to return.
    ret_xid_list = new XAXactId[num_prepared];
    int ret_index = 0;
    for (int i = xid_list.length; i-- > 0;) {
      if (xid_list[i] != null)
        ret_xid_list[ret_index++] = xid_list[i];
    }

    if (SanityManager.DEBUG) {
      SanityManager.ASSERT(ret_index == num_prepared);
    }
  }
  else {
    ret_xid_list = new XAXactId[0];
  }

  return (ret_xid_list);
}
 
Example 5
Source File: AbstractTransactionAssistanceXAResource.java    From genericconnector with Apache License 2.0 5 votes vote down vote up
@Override
public Xid[] recover(int flag) throws XAException {
    List<Xid> xids = new ArrayList<Xid>();
    
    switch(flag) {
    case (XAResource.TMSTARTRSCAN):
        log.log(Level.INFO, "RECOVER TMSTARTRSCAN");
        UnderlyingConnection callback = getUnderlyingConnection();
        if(callback != null){
            final String[] unfinishedTxIds;
            if(isHandleRecoveryInternally()){
                unfinishedTxIds = getTransactionsInNeedOfRecovery();
            }else{
                unfinishedTxIds = callback.getTransactionsInNeedOfRecovery();
            }
            if(unfinishedTxIds != null){
                for(String txId : unfinishedTxIds){
                    log.log(Level.INFO, "recovery required for " + txId);
                    Xid xid = XidImpl.getXid(txId);
                    xids.add(xid);
                }
            }
        }
        break;
    case (XAResource.TMENDRSCAN):
        log.log(Level.INFO, "RECOVER TMENDRSCAN");
        break;
    case (XAResource.TMNOFLAGS):
        log.log(Level.INFO, "RECOVER TMNOFLAGS");
        break;
    default:
        log.log(Level.INFO, "RECOVER " + flag);
    }

    return xids.toArray(new Xid[0]);
}
 
Example 6
Source File: XactXAResourceManager.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
   * This method is called to obtain a list of prepared transactions.
   * <p>
   * This call returns a complete list of global transactions which are 
   * either prepared or heuristically complete.
   * <p>
   * The XAResource interface expects a scan type interface, but our
   * implementation only returns a complete list of transactions.  So to
   * simulate the scan the following state is maintained.  If TMSTARTSCAN
   * is specified the complete list is returned.  If recover is called with
   * TMNOFLAGS is ever called a 0 length array is returned.  
   *
* @return Return a array with 0 or more Xid's which are currently in
   *         prepared or heuristically completed state.  If an error occurs
   *         during the operation, an appropriate error is thrown.
   *
   * @param flags    combination of the following flags 
   *                 XAResource.{TMSTARTRSCAN,TMENDRSCAN,TMNOFLAGS}.  
   *                 TMNOFLAGS must be used when no other flags are used.
   *
* @exception  StandardException  Standard exception policy.
   **/
  public Xid[] recover(int flags)
      throws StandardException
  {
      XAXactId[] ret_xid_list;

      if ((flags & XAResource.TMSTARTRSCAN) != 0)
      {
          Hashtable   trans_hashtable = transaction_table.getTableForXA();
          XAXactId[]  xid_list        = new XAXactId[trans_hashtable.size()];
          int         num_prepared    = 0;

          // Need to hold sync while linear searching the hash table.
          synchronized (trans_hashtable)
          {
              int i = 0;

              for (Enumeration e = trans_hashtable.elements(); 
                   e.hasMoreElements(); i++) 
              {
                  Xact xact = 
                      ((TransactionTableEntry) e.nextElement()).getXact();

                  if (xact.isPrepared())
                  {
                      GlobalTransactionId xa_id = xact.getGlobalId();

                      xid_list[i] = 
                          new XAXactId(
                              xa_id.getFormat_Id(), 
                              xa_id.getGlobalTransactionId(), 
                              xa_id.getBranchQualifier());
                      num_prepared++;
                  }
              }
          }

          // now need to squish the nulls out of the array to return. 
          ret_xid_list = new XAXactId[num_prepared];
          int ret_index = 0;
          for (int i = xid_list.length; i-- > 0; )
          {
              if (xid_list[i] != null)
                  ret_xid_list[ret_index++] = xid_list[i];
          }

          if (SanityManager.DEBUG)
          {
              SanityManager.ASSERT(ret_index == num_prepared);
          }
      }
      else
      {
          ret_xid_list = new XAXactId[0];
      }

      return(ret_xid_list);
  }
 
Example 7
Source File: XactXAResourceManager.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
   * This method is called to obtain a list of prepared transactions.
   * <p>
   * This call returns a complete list of global transactions which are 
   * either prepared or heuristically complete.
   * <p>
   * The XAResource interface expects a scan type interface, but our
   * implementation only returns a complete list of transactions.  So to
   * simulate the scan the following state is maintained.  If TMSTARTSCAN
   * is specified the complete list is returned.  If recover is called with
   * TMNOFLAGS is ever called a 0 length array is returned.  
   *
* @return Return a array with 0 or more Xid's which are currently in
   *         prepared or heuristically completed state.  If an error occurs
   *         during the operation, an appropriate error is thrown.
   *
   * @param flags    combination of the following flags 
   *                 XAResource.{TMSTARTRSCAN,TMENDRSCAN,TMNOFLAGS}.  
   *                 TMNOFLAGS must be used when no other flags are used.
   *
* @exception  StandardException  Standard exception policy.
   **/
  public Xid[] recover(int flags)
      throws StandardException
  {
      XAXactId[] ret_xid_list;

      if ((flags & XAResource.TMSTARTRSCAN) != 0)
      {
          Hashtable   trans_hashtable = transaction_table.getTableForXA();
          XAXactId[]  xid_list        = new XAXactId[trans_hashtable.size()];
          int         num_prepared    = 0;

          // Need to hold sync while linear searching the hash table.
          synchronized (trans_hashtable)
          {
              int i = 0;

              for (Enumeration e = trans_hashtable.elements(); 
                   e.hasMoreElements(); i++) 
              {
                  Xact xact = 
                      ((TransactionTableEntry) e.nextElement()).getXact();

                  if (xact.isPrepared())
                  {
                      GlobalTransactionId xa_id = xact.getGlobalId();

                      xid_list[i] = 
                          new XAXactId(
                              xa_id.getFormat_Id(), 
                              xa_id.getGlobalTransactionId(), 
                              xa_id.getBranchQualifier());
                      num_prepared++;
                  }
              }
          }

          // now need to squish the nulls out of the array to return. 
          ret_xid_list = new XAXactId[num_prepared];
          int ret_index = 0;
          for (int i = xid_list.length; i-- > 0; )
          {
              if (xid_list[i] != null)
                  ret_xid_list[ret_index++] = xid_list[i];
          }

          if (SanityManager.DEBUG)
          {
              SanityManager.ASSERT(ret_index == num_prepared);
          }
      }
      else
      {
          ret_xid_list = new XAXactId[0];
      }

      return(ret_xid_list);
  }
 
Example 8
Source File: FBManagedConnection.java    From jaybird with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
     * Obtain a list of prepared transaction branches from a resource manager.
     * The transaction manager calls this method during recovery to obtain the
     * list of transaction branches that are currently in prepared or
     * heuristically completed states.
     *
     * @param flags
     *         One of TMSTARTRSCAN, TMENDRSCAN, TMNOFLAGS. TMNOFLAGS must be used when no other flags are set in flags.
     * @return The resource manager returns zero or more XIDs for the transaction branches that are currently in a
     * prepared or heuristically completed state. If an error occurs during the operation, the resource manager should
     * throw the appropriate XAException.
     * @throws XAException
     *         An error has occurred. Possible values are XAER_RMERR, XAER_RMFAIL, XAER_INVAL, and XAER_PROTO.
     */
    private Xid[] recover(int flags) throws javax.transaction.xa.XAException {
        if (flags != XAResource.TMSTARTRSCAN && flags != XAResource.TMENDRSCAN && flags != XAResource.TMNOFLAGS
                && flags != (XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN)) {
            throw new FBXAException("flag not allowed in this context: " + flags + ", valid flags are TMSTARTRSCAN, TMENDRSCAN, TMNOFLAGS, TMSTARTRSCAN|TMENDRSCAN", XAException.XAER_PROTO);
        }

        try {
            // if (!((flags & XAResource.TMSTARTRSCAN) == 0))
//            if ((flags & XAResource.TMENDRSCAN) == 0 && (flags & XAResource.TMNOFLAGS) == 0)
//                return new Xid[0];

            List<FBXid> xids = new ArrayList<>();

            FbTransaction trHandle2 = database.startTransaction(tpb.getTransactionParameterBuffer());

            FbStatement stmtHandle2 = database.createStatement(trHandle2);

            GDSHelper gdsHelper2 = new GDSHelper(database);
            gdsHelper2.setCurrentTransaction(trHandle2);

            stmtHandle2.prepare(RECOVERY_QUERY);

            DataProvider dataProvider0 = new DataProvider(0);
            stmtHandle2.addStatementListener(dataProvider0);
            DataProvider dataProvider1 = new DataProvider(1);
            stmtHandle2.addStatementListener(dataProvider1);

            stmtHandle2.execute(RowValue.EMPTY_ROW_VALUE);
            stmtHandle2.fetchRows(10);

            FBField field0 = FBField.createField(stmtHandle2.getRowDescriptor().getFieldDescriptor(0), dataProvider0, gdsHelper2, false);
            FBField field1 = FBField.createField(stmtHandle2.getRowDescriptor().getFieldDescriptor(1), dataProvider1, gdsHelper2, false);

            int row = 0;
            while (row < dataProvider0.getRowCount()) {
                dataProvider0.setRow(row);
                dataProvider1.setRow(row);

                long inLimboTxId = field0.getLong();
                byte[] inLimboMessage = field1.getBytes();

                try {
                    FBXid xid = new FBXid(new ByteArrayInputStream(inLimboMessage), inLimboTxId);
                    xids.add(xid);
                } catch (FBIncorrectXidException ex) {
                    log.warn("ignoring XID stored with invalid format in RDB$TRANSACTIONS for RDB$TRANSACTION_ID=" + inLimboTxId);
                }

                row++;
            }

            stmtHandle2.close();
            trHandle2.commit();

            return xids.toArray(new FBXid[0]);
        } catch (SQLException | IOException e) {
            throw new FBXAException("can't perform query to fetch xids", XAException.XAER_RMFAIL, e);
        }
    }
 
Example 9
Source File: XASupport.java    From jTDS with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Invoke the xa_recover routine on the SQL Server.
 * <p/>
 * This version of xa_recover will return all XIDs on the first call.
 *
 * @param connection JDBC Connection enlisted in the transaction
 * @param xaConId    the connection ID allocated by the server
 * @param flags      XA Flags for start command
 * @return transactions to recover as a <code>Xid[]</code>
 * @exception javax.transaction.xa.XAException
 *             if an error condition occurs
 */
public static Xid[] xa_recover(Connection connection, int xaConId, int flags)
        throws XAException {

    JtdsConnection con = (JtdsConnection)connection;
    if (con.isXaEmulation()) {
        //
        // Emulate xa_recover method
        //
        // There is no state available all uncommited transactions
        // will have been rolled back by the server.
        if (flags != XAResource.TMSTARTRSCAN &&
            flags != XAResource.TMENDRSCAN &&
            flags != ( XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN ) &&
            flags != XAResource.TMNOFLAGS) {
            raiseXAException(XAException.XAER_INVAL);
        }
        return new JtdsXid[0];
    }
    //
    // Execute xa_recover via MSDTC
    //
    int args[] = new int[5];
    args[1] = XA_RECOVER;
    args[2] = xaConId;
    args[3] = XA_RMID;
    args[4] = XAResource.TMNOFLAGS;
    Xid[] list = null;

    if (flags != XAResource.TMSTARTRSCAN) {
        return new JtdsXid[0];
    }

    try {
        byte[][] buffer = ((JtdsConnection) connection).sendXaPacket(args, null);
        if (args[0] >= 0) {
            int n = buffer.length;
            list = new JtdsXid[n];
            for (int i = 0; i < n; i++) {
                list[i] = new JtdsXid(buffer[i], 0);
            }
        }
    } catch (SQLException e) {
        raiseXAException(e);
    }
    if (args[0] < 0) {
        raiseXAException(args[0]);
    }
    if (list == null) {
        list = new JtdsXid[0];
    }
    return list;
}