Search in sources :

Example 46 with XidImple

use of com.arjuna.ats.jta.xa.XidImple in project narayana by jbosstm.

the class ServerTransactionUnitTest method test.

@Test
public void test() throws Exception {
    ServerTransaction st = new ServerTransaction(new Uid(), new XidImple(new Uid()));
    OutputObjectState os = new OutputObjectState();
    assertTrue(st.save_state(os, ObjectType.ANDPERSISTENT));
    InputObjectState is = new InputObjectState(os);
    assertTrue(st.restore_state(is, ObjectType.ANDPERSISTENT));
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) Uid(com.arjuna.ats.arjuna.common.Uid) InputObjectState(com.arjuna.ats.arjuna.state.InputObjectState) OutputObjectState(com.arjuna.ats.arjuna.state.OutputObjectState) ServerTransaction(com.arjuna.ats.internal.jta.transaction.jts.subordinate.jca.coordinator.ServerTransaction) Test(org.junit.Test)

Example 47 with XidImple

use of com.arjuna.ats.jta.xa.XidImple in project narayana by jbosstm.

the class Test02 method main.

public static void main(String[] args) {
    Vector xaRecoveryNodes = new Vector();
    boolean passed = false;
    Uid bogusNodeName = new Uid();
    xaRecoveryNodes.add("2");
    System.err.println("Bogus XA node name: " + bogusNodeName);
    XidImple xid = new XidImple(new Uid());
    String nodeName = XAUtils.getXANodeName(xid);
    // should fail.
    System.err.println("XA node name: " + nodeName);
    System.err.println("Xid to recover: " + xid);
    if (!xaRecoveryNodes.contains(nodeName)) {
        passed = true;
    }
    if (passed) {
        System.err.println("Passed.");
    } else {
        System.err.println("Failed.");
    }
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) Uid(com.arjuna.ats.arjuna.common.Uid) Vector(java.util.Vector)

Example 48 with XidImple

use of com.arjuna.ats.jta.xa.XidImple in project narayana by jbosstm.

the class JTATransactionLogXAResourceOrphanFilter method transactionLog.

/**
 * Is there a log file for this transaction?
 *
 * @param xid the transaction to check.
 *
 * @return <code>boolean</code>true if there is a log file,
 *         <code>false</code> if there isn't.
 * @throws ObjectStoreException If there is a problem accessing the object store
 * @throws IOException In case the data from the object store is corrupted
 */
private boolean transactionLog(Xid xid) throws ObjectStoreException, IOException {
    RecoveryStore recoveryStore = StoreManager.getRecoveryStore();
    String transactionType = new AtomicAction().type();
    XidImple theXid = new XidImple(xid);
    Uid u = theXid.getTransactionUid();
    if (jtaLogger.logger.isDebugEnabled()) {
        jtaLogger.logger.debug("Checking whether Xid " + theXid + " exists in ObjectStore.");
    }
    if (!u.equals(Uid.nullUid())) {
        if (jtaLogger.logger.isDebugEnabled()) {
            jtaLogger.logger.debug("Looking for " + u + " and " + transactionType);
        }
        if (containsCommitMarkableResourceRecord(u) || recoveryStore.currentState(u, transactionType) != StateStatus.OS_UNKNOWN) {
            if (jtaLogger.logger.isDebugEnabled()) {
                jtaLogger.logger.debug("Found record for " + theXid);
            }
            return true;
        } else {
            if (jtaLogger.logger.isDebugEnabled()) {
                jtaLogger.logger.debug("No record found for " + theXid);
            }
        }
    } else {
        jtaLogger.i18NLogger.info_recovery_notaxid(XAHelper.xidToString(xid));
    }
    return false;
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) AtomicAction(com.arjuna.ats.internal.jta.transaction.arjunacore.AtomicAction) Uid(com.arjuna.ats.arjuna.common.Uid) RecoveryStore(com.arjuna.ats.arjuna.objectstore.RecoveryStore)

Example 49 with XidImple

use of com.arjuna.ats.jta.xa.XidImple in project narayana by jbosstm.

the class CommitMarkableResourceRecord method topLevelPrepare.

/**
 * This will add the required recovery data about this resource into the
 * resources preparedConnection. If the preparedConnection is in read only
 * mode, we do not need to persist this information.
 */
public int topLevelPrepare() {
    if (tsLogger.logger.isTraceEnabled()) {
        tsLogger.logger.trace("CommitMarkableResourceRecord.topLevelPrepare for " + this + ", record id=" + order());
    }
    try {
        PreparedStatement prepareStatement = null;
        preparedConnection = (Connection) connectableResource.getConnection();
        try {
            prepareStatement = preparedConnection.prepareStatement("insert into " + tableName + " (xid, transactionManagerID, actionuid) values (?,?,?)");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            XID toSave = ((XidImple) xid).getXID();
            dos.writeInt(toSave.formatID);
            dos.writeInt(toSave.gtrid_length);
            dos.writeInt(toSave.bqual_length);
            dos.writeInt(toSave.data.length);
            dos.write(toSave.data);
            dos.flush();
            prepareStatement.setBytes(1, baos.toByteArray());
            prepareStatement.setString(2, TxControl.getXANodeName());
            prepareStatement.setBytes(3, basicAction.get_uid().getBytes());
            if (prepareStatement.executeUpdate() != 1) {
                tsLogger.logger.warn("Update was not successful");
                removeConnection();
                return TwoPhaseOutcome.PREPARE_NOTOK;
            }
        } finally {
            if (prepareStatement != null)
                prepareStatement.close();
        }
        return TwoPhaseOutcome.PREPARE_OK;
    } catch (Throwable t) {
        tsLogger.logger.error("Could not add recovery data to the 1PC resource", t);
        return TwoPhaseOutcome.PREPARE_NOTOK;
    }
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) XID(com.arjuna.ats.internal.jta.xa.XID) DataOutputStream(java.io.DataOutputStream) PreparedStatement(java.sql.PreparedStatement) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 50 with XidImple

use of com.arjuna.ats.jta.xa.XidImple in project narayana by jbosstm.

the class CommitMarkableResourceRecordRecoveryModule method periodicWorkFirstPass.

@Override
public synchronized void periodicWorkFirstPass() {
    if (inFirstPass) {
        return;
    }
    inFirstPass = true;
    // TODO - this is one shot only due to a
    // remove in the function, if this delete fails only normal
    // recovery is possible
    Map<String, List<Xid>> completedBranches2 = new HashMap<String, List<Xid>>();
    synchronized (completedBranches) {
        completedBranches2.putAll(completedBranches);
        completedBranches.clear();
    }
    for (Map.Entry<String, List<Xid>> e : completedBranches2.entrySet()) delete(e.getKey(), e.getValue());
    if (tsLogger.logger.isTraceEnabled()) {
        tsLogger.logger.trace("CommitMarkableResourceRecordRecoveryModule::periodicWorkFirstPass");
    }
    this.committedXidsToJndiNames.clear();
    this.queriedResourceManagers.clear();
    this.jndiNamesToPossibleXidsForGC.clear();
    // committed
    try {
        Iterator<String> iterator = jndiNamesToContact.iterator();
        while (iterator.hasNext()) {
            String jndiName = iterator.next();
            try {
                if (tsLogger.logger.isTraceEnabled()) {
                    tsLogger.logger.trace("CommitMarkableResourceRecordRecoveryModule::connecting to: " + jndiName);
                }
                DataSource dataSource = (DataSource) context.lookup(jndiName);
                Connection connection = dataSource.getConnection();
                try {
                    Statement createStatement = connection.createStatement();
                    try {
                        String tableName = commitMarkableResourceTableNameMap.get(jndiName);
                        if (tableName == null) {
                            tableName = defaultTableName;
                        }
                        ResultSet rs = createStatement.executeQuery("SELECT xid,actionuid from " + tableName + whereFilter);
                        try {
                            int i = 0;
                            while (rs.next()) {
                                i++;
                                byte[] xidAsBytes = rs.getBytes(1);
                                ByteArrayInputStream bais = new ByteArrayInputStream(xidAsBytes);
                                DataInputStream dis = new DataInputStream(bais);
                                XID _theXid = new XID();
                                _theXid.formatID = dis.readInt();
                                _theXid.gtrid_length = dis.readInt();
                                _theXid.bqual_length = dis.readInt();
                                int dataLength = dis.readInt();
                                _theXid.data = new byte[dataLength];
                                dis.read(_theXid.data, 0, dataLength);
                                XidImple xid = new XidImple(_theXid);
                                byte[] actionuidAsBytes = new byte[Uid.UID_SIZE];
                                byte[] bytes = rs.getBytes(2);
                                System.arraycopy(bytes, 0, actionuidAsBytes, 0, bytes.length);
                                committedXidsToJndiNames.put(xid, jndiName);
                                if (tsLogger.logger.isTraceEnabled()) {
                                    tsLogger.logger.trace("committedXidsToJndiNames.put" + xid + " " + jndiName);
                                }
                                // Populate the map of possible GCable Xids
                                Uid actionuid = new Uid(actionuidAsBytes);
                                Map<Xid, Uid> map = jndiNamesToPossibleXidsForGC.get(jndiName);
                                if (map == null) {
                                    map = new HashMap<Xid, Uid>();
                                    jndiNamesToPossibleXidsForGC.put(jndiName, map);
                                }
                                map.put(xid, actionuid);
                            }
                        } finally {
                            try {
                                rs.close();
                            } catch (SQLException e) {
                                tsLogger.logger.warn("Could not close resultset", e);
                            }
                        }
                    } finally {
                        try {
                            createStatement.close();
                        } catch (SQLException e) {
                            tsLogger.logger.warn("Could not close statement", e);
                        }
                    }
                    queriedResourceManagers.add(jndiName);
                } finally {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        tsLogger.logger.warn("Could not close connection", e);
                    }
                }
            } catch (NamingException e) {
                tsLogger.logger.debug("Could not lookup CommitMarkableResource: " + jndiName, e);
            } catch (SQLException e) {
                tsLogger.logger.warn("Could not handle connection", e);
            } catch (IOException e) {
                tsLogger.logger.warn("Could not lookup write data to select", e);
            }
        }
        // reference
        try {
            InputObjectState uidList = new InputObjectState();
            recoveryStore.allObjUids(CONNECTABLE_ATOMIC_ACTION_TYPE, uidList);
            Uid currentUid = UidHelper.unpackFrom(uidList);
            while (Uid.nullUid().notEquals(currentUid)) {
                // Make sure it isn't garbage from a failure to move before:
                InputObjectState state = recoveryStore.read_committed(currentUid, ATOMIC_ACTION_TYPE);
                if (state != null) {
                    if (!recoveryStore.remove_committed(currentUid, CONNECTABLE_ATOMIC_ACTION_TYPE)) {
                        tsLogger.logger.debug("Could not remove a: " + CONNECTABLE_ATOMIC_ACTION_TYPE + " uid: " + currentUid);
                    }
                } else {
                    state = recoveryStore.read_committed(currentUid, CONNECTABLE_ATOMIC_ACTION_TYPE);
                    // TX may have been in progress and cleaned up by now
                    if (state != null) {
                        RecoverConnectableAtomicAction rcaa = new RecoverConnectableAtomicAction(CONNECTABLE_ATOMIC_ACTION_TYPE, currentUid, state);
                        if (rcaa.containsIncompleteCommitMarkableResourceRecord()) {
                            String commitMarkableResourceJndiName = rcaa.getCommitMarkableResourceJndiName();
                            // Check if the resource manager is online yet
                            if (queriedResourceManagers.contains(commitMarkableResourceJndiName)) {
                                // If it is remove the CRR and move it back and
                                // let
                                // the
                                // next stage update it
                                moveRecord(currentUid, CONNECTABLE_ATOMIC_ACTION_TYPE, ATOMIC_ACTION_TYPE);
                            }
                        } else {
                            if (tsLogger.logger.isTraceEnabled()) {
                                tsLogger.logger.trace("Moving " + currentUid + " back to being an AA");
                            }
                            // It is now safe to move it back to being an AA so that it can call getNewXAResourceRecord
                            moveRecord(currentUid, CONNECTABLE_ATOMIC_ACTION_TYPE, ATOMIC_ACTION_TYPE);
                        }
                    }
                }
                currentUid = UidHelper.unpackFrom(uidList);
            }
        } catch (ObjectStoreException | IOException ex) {
            tsLogger.logger.warn("Could not query objectstore: ", ex);
        }
        // moved in stage 2
        if (tsLogger.logger.isDebugEnabled()) {
            tsLogger.logger.debug("processing " + ATOMIC_ACTION_TYPE + " transactions");
        }
        try {
            InputObjectState uidList = new InputObjectState();
            recoveryStore.allObjUids(ATOMIC_ACTION_TYPE, uidList);
            Uid currentUid = UidHelper.unpackFrom(uidList);
            while (Uid.nullUid().notEquals(currentUid)) {
                // process.
                if (!isTransactionInMidFlight(transactionStatusConnectionMgr.getTransactionStatus(ATOMIC_ACTION_TYPE, currentUid))) {
                    InputObjectState state = recoveryStore.read_committed(currentUid, ATOMIC_ACTION_TYPE);
                    if (state != null) {
                        // Try to load it is a BasicAction that has a
                        // ConnectedResourceRecord
                        RecoverConnectableAtomicAction rcaa = new RecoverConnectableAtomicAction(ATOMIC_ACTION_TYPE, currentUid, state);
                        // Check if it did have a ConnectedResourceRecord
                        if (rcaa.containsIncompleteCommitMarkableResourceRecord()) {
                            String commitMarkableResourceJndiName = rcaa.getCommitMarkableResourceJndiName();
                            // online
                            if (!queriedResourceManagers.contains(commitMarkableResourceJndiName)) {
                                // If the resource manager wasn't online, move
                                // it
                                moveRecord(currentUid, ATOMIC_ACTION_TYPE, CONNECTABLE_ATOMIC_ACTION_TYPE);
                            } else {
                                // Update the completed outcome for the 1PC
                                // resource
                                rcaa.updateCommitMarkableResourceRecord(committedXidsToJndiNames.get(rcaa.getXid()) != null);
                                // Swap the type to avoid the rest of recovery round processing this TX as it already called getNewXAResourceRecord
                                moveRecord(currentUid, ATOMIC_ACTION_TYPE, CONNECTABLE_ATOMIC_ACTION_TYPE);
                            }
                        }
                    }
                }
                currentUid = UidHelper.unpackFrom(uidList);
            }
        } catch (ObjectStoreException | IOException ex) {
            tsLogger.logger.warn("Could not query objectstore: ", ex);
        }
    } catch (IllegalStateException e) {
        // Thrown when AS is shutting down and we attempt a lookup
        tsLogger.logger.debug("Could not lookup datasource, AS is shutting down: " + e.getMessage(), e);
    }
    inFirstPass = false;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) NamingException(javax.naming.NamingException) XidImple(com.arjuna.ats.jta.xa.XidImple) InputObjectState(com.arjuna.ats.arjuna.state.InputObjectState) ObjectStoreException(com.arjuna.ats.arjuna.exceptions.ObjectStoreException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) DataSource(javax.sql.DataSource) Uid(com.arjuna.ats.arjuna.common.Uid) Xid(javax.transaction.xa.Xid) XID(com.arjuna.ats.internal.jta.xa.XID) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

XidImple (com.arjuna.ats.jta.xa.XidImple)90 Uid (com.arjuna.ats.arjuna.common.Uid)71 Test (org.junit.Test)71 Xid (javax.transaction.xa.Xid)35 XAException (javax.transaction.xa.XAException)28 SubordinateTransaction (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction)27 Transaction (javax.transaction.Transaction)16 XATerminator (javax.resource.spi.XATerminator)15 TransactionImporter (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter)12 XATerminatorImple (com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple)10 IOException (java.io.IOException)10 InputObjectState (com.arjuna.ats.arjuna.state.InputObjectState)9 XAOnePhaseResource (com.arjuna.ats.internal.jta.resources.arjunacore.XAOnePhaseResource)9 XAResourceRecord (com.arjuna.ats.internal.jta.resources.arjunacore.XAResourceRecord)9 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)8 SampleOnePhaseResource (com.hp.mwtests.ts.jta.common.SampleOnePhaseResource)8 RollbackException (javax.transaction.RollbackException)8 SystemException (javax.transaction.SystemException)8 XAResource (javax.transaction.xa.XAResource)8 OutputObjectState (com.arjuna.ats.arjuna.state.OutputObjectState)7