Search in sources :

Example 31 with TransactionUID

use of com.sun.messaging.jmq.jmsserver.data.TransactionUID in project openmq by eclipse-ee4j.

the class DBTool method doBackup.

/**
 * Backup the JDBC store to filebased backup files.
 */
private void doBackup() throws BrokerException {
    if (!Globals.getHAEnabled()) {
        throw new BrokerException(br.getKString(BrokerResources.I_HA_NOT_ENABLE, dbmgr.getBrokerID()));
    }
    // instantiate the file store.
    Properties props = System.getProperties();
    String clusterID = Globals.getClusterID();
    String backupDir = (String) props.get(Globals.IMQ + ".backupdir") + File.separator + clusterID;
    logger.logToAll(Logger.INFO, "Backup persistent store for HA cluster " + clusterID);
    final FileStore fileStore = new FileStore(backupDir, false);
    // for backup, need to clear the store before storing anything.
    fileStore.clearAll(false);
    JDBCStore jdbcStore = null;
    try {
        jdbcStore = (JDBCStore) StoreManager.getStore();
        /*
             * For data that are not broker specific, i.e. properties, change records, consumers, brokers, and sessions, we will
             * store those data on the top level directory (a.k.a cluster filestore).
             */
        // Properties table
        Properties properties = jdbcStore.getAllProperties();
        Iterator propItr = properties.entrySet().iterator();
        while (propItr.hasNext()) {
            Map.Entry entry = (Map.Entry) propItr.next();
            fileStore.updateProperty((String) entry.getKey(), entry.getValue(), false);
        }
        propItr = null;
        properties = null;
        // Configuration Change Record table
        List<ChangeRecordInfo> records = jdbcStore.getAllConfigRecords();
        for (int i = 0, len = records.size(); i < len; i++) {
            fileStore.storeConfigChangeRecord(records.get(i).getTimestamp(), records.get(i).getRecord(), false);
        }
        records = null;
        // Consumer table
        Consumer[] consumerArray = jdbcStore.getAllInterests();
        for (int i = 0; i < consumerArray.length; i++) {
            fileStore.storeInterest(consumerArray[i], false);
        }
        consumerArray = null;
        // Broker & Session table - store all HABrokerInfo as a property
        HashMap bkrMap = jdbcStore.getAllBrokerInfos(true);
        List haBrokers = new ArrayList(bkrMap.values());
        fileStore.updateProperty(STORE_PROPERTY_HABROKERS, haBrokers, true);
        /*
             * For each broker in the cluster, we will store broker specific data, destinations, messages, transactions,
             * acknowledgements in their own filestore (a.k.a broker filestore).
             */
        Iterator bkrItr = haBrokers.iterator();
        while (bkrItr.hasNext()) {
            // Backup data for each broker
            HABrokerInfo bkrInfo = (HABrokerInfo) bkrItr.next();
            String brokerID = bkrInfo.getId();
            logger.logToAll(Logger.INFO, "Backup persistent data for broker " + brokerID);
            FileStore bkrFS = null;
            JMSBridgeStore jmsbridgeStore = null;
            try {
                String instanceRootDir = backupDir + File.separator + brokerID;
                bkrFS = new FileStore(instanceRootDir, false);
                bkrFS.clearAll(false);
                // Destination table.
                Destination[] dstArray = jdbcStore.getAllDestinations(brokerID);
                for (int i = 0, len = dstArray.length; i < len; i++) {
                    DestinationUID did = dstArray[i].getDestinationUID();
                    Destination dst = bkrFS.getDestination(did);
                    if (dst == null) {
                        // Store the destination if not found
                        bkrFS.storeDestination(dstArray[i], false);
                    }
                }
                // Storing messages for each destination.
                for (int i = 0, len = dstArray.length; i < len; i++) {
                    Enumeration e = jdbcStore.messageEnumeration(dstArray[i]);
                    try {
                        for (; e.hasMoreElements(); ) {
                            DestinationUID did = dstArray[i].getDestinationUID();
                            Packet message = (Packet) e.nextElement();
                            SysMessageID mid = message.getSysMessageID();
                            // Get interest states for the message; Consumer State table
                            HashMap stateMap = jdbcStore.getInterestStates(did, mid);
                            if (stateMap == null || stateMap.isEmpty()) {
                                bkrFS.storeMessage(did, message, false);
                            } else {
                                int size = stateMap.size();
                                ConsumerUID[] iids = new ConsumerUID[size];
                                int[] states = new int[size];
                                Iterator stateItr = stateMap.entrySet().iterator();
                                int j = 0;
                                while (stateItr.hasNext()) {
                                    Map.Entry entry = (Map.Entry) stateItr.next();
                                    iids[j] = (ConsumerUID) entry.getKey();
                                    states[j] = ((Integer) entry.getValue()).intValue();
                                    j++;
                                }
                                bkrFS.storeMessage(did, message, iids, states, false);
                            }
                        }
                    } finally {
                        jdbcStore.closeEnumeration(e);
                    }
                }
                // Transaction table
                Collection txnList = jdbcStore.getTransactions(brokerID);
                Iterator txnItr = txnList.iterator();
                while (txnItr.hasNext()) {
                    TransactionUID tid = (TransactionUID) txnItr.next();
                    TransactionInfo txnInfo = jdbcStore.getTransactionInfo(tid);
                    TransactionAcknowledgement[] txnAck = jdbcStore.getTransactionAcks(tid);
                    bkrFS.storeTransaction(tid, txnInfo, false);
                    for (int i = 0, len = txnAck.length; i < len; i++) {
                        bkrFS.storeTransactionAck(tid, txnAck[i], false);
                    }
                }
                /**
                 ************************************************
                 * JMSBridge
                 *************************************************
                 */
                Properties bp = new Properties();
                bp.setProperty("instanceRootDir", instanceRootDir);
                bp.setProperty("reset", "true");
                bp.setProperty("logdomain", "imqdbmgr");
                bp.setProperty("identityName", Globals.getIdentityName());
                BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
                List bnames = jdbcStore.getJMSBridgesByBroker(brokerID, null);
                Collections.sort(bnames);
                String bname = null;
                String currbname = null;
                Iterator itr = bnames.iterator();
                while (itr.hasNext()) {
                    bname = (String) itr.next();
                    if (currbname == null || !currbname.equals(bname)) {
                        currbname = bname;
                        bp.setProperty("jmsbridge", bname);
                        if (jmsbridgeStore != null) {
                            jmsbridgeStore.closeJMSBridgeStore();
                            jmsbridgeStore = null;
                        }
                        logger.logToAll(logger.INFO, "Backup JMS bridge " + bname);
                        jmsbridgeStore = (JMSBridgeStore) BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
                        List lrs = jdbcStore.getLogRecordsByNameByBroker(bname, brokerID, null);
                        logger.logToAll(logger.INFO, "\tBackup JMS bridge " + bname + " with " + lrs.size() + " TM log records");
                        byte[] lr = null;
                        Iterator itr1 = lrs.iterator();
                        while (itr1.hasNext()) {
                            lr = (byte[]) itr1.next();
                            jmsbridgeStore.storeTMLogRecord(null, lr, currbname, true, null);
                        }
                    }
                }
            } finally {
                bkrFS.close();
                if (jmsbridgeStore != null) {
                    jmsbridgeStore.closeJMSBridgeStore();
                }
            }
        }
        logger.logToAll(Logger.INFO, "Backup persistent store complete.");
    } catch (Throwable ex) {
        ex.printStackTrace();
        throw new BrokerException(ex.getMessage());
    } finally {
        assert fileStore != null;
        try {
            fileStore.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        StoreManager.releaseStore(true);
    }
}
Also used : Destination(com.sun.messaging.jmq.jmsserver.core.Destination) HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo) Consumer(com.sun.messaging.jmq.jmsserver.core.Consumer) TransactionInfo(com.sun.messaging.jmq.jmsserver.persist.api.TransactionInfo) JMSBridgeStore(com.sun.messaging.bridge.api.JMSBridgeStore) Packet(com.sun.messaging.jmq.io.Packet) TransactionAcknowledgement(com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) FileStore(com.sun.messaging.jmq.jmsserver.persist.file.FileStore) DestinationUID(com.sun.messaging.jmq.jmsserver.core.DestinationUID) SysMessageID(com.sun.messaging.jmq.io.SysMessageID) ChangeRecordInfo(com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)

Example 32 with TransactionUID

use of com.sun.messaging.jmq.jmsserver.data.TransactionUID in project openmq by eclipse-ee4j.

the class UpgradeStore method upgradeTxnAcks.

/**
 * Upgrade transaction acknowledgements from version 350/370 table to current format (410)
 */
private void upgradeTxnAcks(Connection conn) throws BrokerException {
    if (oldStoreVersion == JDBCStore.OLD_STORE_VERSION_400) {
        // In 400, ack table has been folded into consumer state table
        return;
    }
    ConsumerStateDAO stateDAO = dbMgr.getDAOFactory().getConsumerStateDAO();
    // SQL to select all acknowledgements from version 350/370 table
    String getAllTxnAcksFromOldSQL = "SELECT atbl." + TTXNACK_CTUID + ", " + TTXNACK_CACK + " FROM " + oldTxnTable + " ttbl, " + oldAckTable + " atbl" + " WHERE ttbl." + TTXN_CTUID + " = atbl." + TTXNACK_CTUID;
    // SQL to insert acknowledgements to consumer state table;
    // for 400, txn ack is represent as a column in consumer state table.
    String insertTxnAckSQL = new StringBuilder(128).append("UPDATE ").append(stateDAO.getTableName()).append(" SET ").append(ConsumerStateDAO.TRANSACTION_ID_COLUMN).append(" = ? ").append(" WHERE ").append(ConsumerStateDAO.MESSAGE_ID_COLUMN).append(" = ?").append(" AND ").append(ConsumerStateDAO.CONSUMER_ID_COLUMN).append(" = ?").toString();
    boolean dobatch = dbMgr.supportsBatchUpdates();
    PreparedStatement pstmt = null;
    Statement stmt = null;
    ResultSet rs = null;
    TransactionUID tid = null;
    Exception myex = null;
    try {
        pstmt = dbMgr.createPreparedStatement(conn, insertTxnAckSQL);
        stmt = conn.createStatement();
        rs = dbMgr.executeQueryStatement(stmt, getAllTxnAcksFromOldSQL);
        while (rs.next()) {
            long id = rs.getLong(1);
            tid = new TransactionUID(id);
            TransactionAcknowledgement ack = (TransactionAcknowledgement) Util.readObject(rs, 2);
            // insert in new table
            try {
                pstmt.setLong(1, id);
                pstmt.setString(2, ack.getSysMessageID().toString());
                pstmt.setLong(3, ack.getStoredConsumerUID().longValue());
                if (dobatch) {
                    pstmt.addBatch();
                } else {
                    pstmt.executeUpdate();
                }
            } catch (SQLException e) {
                SQLException ex = DBManager.wrapSQLException("[" + insertTxnAckSQL + "]", e);
                throw ex;
            }
        }
        if (dobatch) {
            pstmt.executeBatch();
        }
        conn.commit();
        if (store.upgradeNoBackup()) {
            dropTable(conn, oldTxnTable);
            dropTable(conn, oldAckTable);
        }
    } catch (Exception e) {
        myex = e;
        String errorMsg = br.getKString(BrokerResources.X_JDBC_UPGRADE_TXNACK_FAILED, (tid == null ? "loading" : tid.toString()));
        logger.logStack(Logger.ERROR, errorMsg, e);
        throw new BrokerException(errorMsg, e);
    } finally {
        Util.close(rs, stmt, null, myex);
        Util.close(null, pstmt, null, myex);
    }
}
Also used : TransactionAcknowledgement(com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID)

Example 33 with TransactionUID

use of com.sun.messaging.jmq.jmsserver.data.TransactionUID in project openmq by eclipse-ee4j.

the class ProtocolImpl method recoverTransaction.

/**
 * Recover a transaction.
 *
 * @param id id to recover or null if all
 */
@Override
public JMQXid[] recoverTransaction(TransactionUID id) {
    if (DEBUG) {
        logger.log(Logger.INFO, "ProtocolImpl.RECOVER TRANSACTION:TID=" + id);
    }
    // TransactionHandler handler = (TransactionHandler)
    // pr.getHandler(PacketType.START_TRANSACTION);
    TransactionList[] tls = DL.getTransactionList(null);
    TransactionList tl = null;
    TransactionState ts = null;
    Map<TransactionList, Vector> map = new LinkedHashMap<>();
    Vector v = null;
    for (int i = 0; i < tls.length; i++) {
        tl = tls[i];
        if (id == null) {
            v = tl.getTransactions(TransactionState.PREPARED);
            map.put(tl, v);
        } else {
            ts = tl.retrieveState(id);
            if (ts == null) {
                continue;
            }
            if (ts.getState() == TransactionState.PREPARED) {
                v = new Vector();
                v.add(id);
                map.put(tl, v);
                break;
            }
        }
    }
    ArrayList xids = new ArrayList();
    for (Map.Entry<TransactionList, Vector> pair : map.entrySet()) {
        tl = pair.getKey();
        v = pair.getValue();
        Iterator itr = v.iterator();
        while (itr.hasNext()) {
            TransactionUID tuid = (TransactionUID) itr.next();
            TransactionState _ts = tl.retrieveState(tuid);
            if (_ts == null) {
                // Should never happen
                continue;
            }
            JMQXid _xid = _ts.getXid();
            xids.add(_xid);
        }
    }
    return (JMQXid[]) xids.toArray(new JMQXid[xids.size()]);
}
Also used : TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) ArrayList(java.util.ArrayList) TransactionList(com.sun.messaging.jmq.jmsserver.data.TransactionList) JMQXid(com.sun.messaging.jmq.util.JMQXid) LinkedHashMap(java.util.LinkedHashMap) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) Iterator(java.util.Iterator) Vector(java.util.Vector) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 34 with TransactionUID

use of com.sun.messaging.jmq.jmsserver.data.TransactionUID in project openmq by eclipse-ee4j.

the class DBTool method doRestore.

/*
     * Restore the JDBC store from filebased backup files.
     */
private void doRestore() throws BrokerException {
    if (!Globals.getHAEnabled()) {
        throw new BrokerException(br.getKString(BrokerResources.I_HA_NOT_ENABLE, dbmgr.getBrokerID()));
    }
    // instantiate the file store.
    Properties props = System.getProperties();
    String clusterID = Globals.getClusterID();
    String backupDir = (String) props.get(Globals.IMQ + ".backupdir") + File.separator + clusterID;
    logger.logToAll(Logger.INFO, "Restore persistent store for HA cluster " + clusterID + " from backup dir: " + backupDir);
    final FileStore fileStore = new FileStore(backupDir, false);
    // Brokers table.
    JDBCStore jdbcStore = null;
    try {
        // Re-create the jdbc store
        doRecreate();
        jdbcStore = (JDBCStore) StoreManager.getStore();
        /*
             * For data that are not broker specific, i.e. properties, change records, consumers, brokers, and sessions, we will
             * retrieve those data from the top level directory (a.k.a cluster filestore).
             */
        // Properties table
        List haBrokers = null;
        Properties properties = fileStore.getAllProperties();
        Iterator propItr = properties.entrySet().iterator();
        while (propItr.hasNext()) {
            Map.Entry entry = (Map.Entry) propItr.next();
            String name = (String) entry.getKey();
            if (name.equals(STORE_PROPERTY_HABROKERS)) {
                // Retrieve all HABrokerInfo from a property
                haBrokers = (List) entry.getValue();
            } else {
                jdbcStore.updateProperty(name, entry.getValue(), false);
            }
        }
        propItr = null;
        properties = null;
        if (haBrokers == null || haBrokers.isEmpty()) {
            throw new BrokerException(br.getKString(BrokerResources.X_LOAD_ALL_BROKERINFO_FAILED));
        }
        // Configuration Change Record table
        List<ChangeRecordInfo> records = fileStore.getAllConfigRecords();
        for (int i = 0, len = records.size(); i < len; i++) {
            jdbcStore.storeConfigChangeRecord(records.get(i).getTimestamp(), records.get(i).getRecord(), false);
        }
        records = null;
        // Consumer table
        Consumer[] consumerArray = fileStore.getAllInterests();
        for (int i = 0; i < consumerArray.length; i++) {
            jdbcStore.storeInterest(consumerArray[i], false);
        }
        consumerArray = null;
        // Broker & Session table.
        Iterator bkrItr = haBrokers.iterator();
        while (bkrItr.hasNext()) {
            HABrokerInfo bkrInfo = (HABrokerInfo) bkrItr.next();
            jdbcStore.addBrokerInfo(bkrInfo, false);
        }
        /*
             * For each broker in the cluster, we will retrieve broker specific data, destinations, messages, transactions,
             * acknowledgements from their own filestore (a.k.a broker filestore).
             */
        bkrItr = haBrokers.iterator();
        while (bkrItr.hasNext()) {
            // Backup data for each broker
            HABrokerInfo bkrInfo = (HABrokerInfo) bkrItr.next();
            String brokerID = bkrInfo.getId();
            long sessionID = bkrInfo.getSessionID();
            logger.logToAll(Logger.INFO, "Restore persistent data for broker " + brokerID);
            FileStore bkrFS = null;
            JMSBridgeStore jmsbridgeStore = null;
            try {
                String instanceRootDir = backupDir + File.separator + brokerID;
                bkrFS = new FileStore(instanceRootDir, false);
                // Destination table.
                Destination[] dstArray = bkrFS.getAllDestinations();
                for (int i = 0, len = dstArray.length; i < len; i++) {
                    DestinationUID did = dstArray[i].getDestinationUID();
                    Destination dst = jdbcStore.getDestination(did);
                    if (dst == null) {
                        // Store the destination if not found
                        jdbcStore.storeDestination(dstArray[i], sessionID);
                    }
                }
                // Retrieve messages for each destination.
                for (int i = 0, len = dstArray.length; i < len; i++) {
                    for (Enumeration e = bkrFS.messageEnumeration(dstArray[i]); e.hasMoreElements(); ) {
                        DestinationUID did = dstArray[i].getDestinationUID();
                        Packet message = (Packet) e.nextElement();
                        SysMessageID mid = message.getSysMessageID();
                        // Get interest states for the message; Consumer State table
                        HashMap stateMap = bkrFS.getInterestStates(did, mid);
                        if (stateMap == null || stateMap.isEmpty()) {
                            jdbcStore.storeMessage(did, message, null, null, sessionID, false);
                        } else {
                            int size = stateMap.size();
                            ConsumerUID[] iids = new ConsumerUID[size];
                            int[] states = new int[size];
                            Iterator stateItr = stateMap.entrySet().iterator();
                            int j = 0;
                            while (stateItr.hasNext()) {
                                Map.Entry entry = (Map.Entry) stateItr.next();
                                iids[j] = (ConsumerUID) entry.getKey();
                                states[j] = ((Integer) entry.getValue()).intValue();
                                j++;
                            }
                            jdbcStore.storeMessage(did, message, iids, states, sessionID, false);
                        }
                    }
                }
                // Transaction table
                Collection txnList = bkrFS.getTransactions(brokerID);
                Iterator txnItr = txnList.iterator();
                while (txnItr.hasNext()) {
                    TransactionUID tid = (TransactionUID) txnItr.next();
                    TransactionInfo txnInfo = bkrFS.getTransactionInfo(tid);
                    TransactionAcknowledgement[] txnAck = bkrFS.getTransactionAcks(tid);
                    jdbcStore.storeTransaction(tid, txnInfo, sessionID);
                    for (int i = 0, len = txnAck.length; i < len; i++) {
                        jdbcStore.storeTransactionAck(tid, txnAck[i], false);
                    }
                }
                /**
                 ************************************************
                 * JMSBridge
                 *************************************************
                 */
                Properties bp = new Properties();
                bp.setProperty("instanceRootDir", instanceRootDir);
                bp.setProperty("reset", "false");
                bp.setProperty("logdomain", "imqdbmgr");
                jmsbridgeStore = (JMSBridgeStore) BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
                if (jmsbridgeStore != null) {
                    List bnames = jmsbridgeStore.getJMSBridges(null);
                    String bname = null;
                    Iterator itr = bnames.iterator();
                    while (itr.hasNext()) {
                        bname = (String) itr.next();
                        jdbcStore.addJMSBridge(bname, true, null);
                    }
                    jmsbridgeStore.closeJMSBridgeStore();
                    jmsbridgeStore = null;
                    bname = null;
                    itr = bnames.iterator();
                    while (itr.hasNext()) {
                        bname = (String) itr.next();
                        bp.setProperty("jmsbridge", bname);
                        if (jmsbridgeStore != null) {
                            jmsbridgeStore.closeJMSBridgeStore();
                            jmsbridgeStore = null;
                        }
                        logger.logToAll(logger.INFO, "Restore JMS bridge " + bname);
                        jmsbridgeStore = (JMSBridgeStore) BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
                        List xids = jmsbridgeStore.getTMLogRecordKeysByName(bname, null);
                        logger.logToAll(logger.INFO, "\tRestore JMS bridge " + bname + " with " + xids.size() + " TM log records");
                        String xid = null;
                        byte[] lr = null;
                        Iterator itr1 = xids.iterator();
                        while (itr1.hasNext()) {
                            xid = (String) itr1.next();
                            lr = jmsbridgeStore.getTMLogRecord(xid, bname, null);
                            if (lr == null) {
                                logger.logToAll(Logger.INFO, "JMSBridge TM log record not found for " + xid);
                                continue;
                            }
                            jdbcStore.storeTMLogRecord(xid, lr, bname, true, null);
                        }
                    }
                }
            } finally {
                bkrFS.close();
                if (jmsbridgeStore != null) {
                    jmsbridgeStore.closeJMSBridgeStore();
                }
            }
        }
        logger.logToAll(Logger.INFO, "Restore persistent store complete.");
    } catch (Throwable ex) {
        ex.printStackTrace();
        throw new BrokerException(ex.getMessage());
    } finally {
        assert fileStore != null;
        try {
            fileStore.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        StoreManager.releaseStore(true);
    }
}
Also used : Destination(com.sun.messaging.jmq.jmsserver.core.Destination) HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo) Consumer(com.sun.messaging.jmq.jmsserver.core.Consumer) TransactionInfo(com.sun.messaging.jmq.jmsserver.persist.api.TransactionInfo) JMSBridgeStore(com.sun.messaging.bridge.api.JMSBridgeStore) Packet(com.sun.messaging.jmq.io.Packet) TransactionAcknowledgement(com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) FileStore(com.sun.messaging.jmq.jmsserver.persist.file.FileStore) DestinationUID(com.sun.messaging.jmq.jmsserver.core.DestinationUID) SysMessageID(com.sun.messaging.jmq.io.SysMessageID) ChangeRecordInfo(com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)

Example 35 with TransactionUID

use of com.sun.messaging.jmq.jmsserver.data.TransactionUID in project openmq by eclipse-ee4j.

the class UpgradeHAStore method upgradeTxns.

/**
 * Upgrade transactions
 */
private void upgradeTxns(Connection conn) throws BrokerException {
    TransactionDAO txnDAO = dbMgr.getDAOFactory().getTransactionDAO();
    // Non-HA table
    String oldtxntbl = TransactionDAO.TABLE_NAME_PREFIX + "S" + brokerID;
    // SQL to select all transactions from version Non_HA table
    String getAllTxnsFromOldSQL = new StringBuilder(128).append("SELECT ").append(TransactionDAO.ID_COLUMN).append(", ").append(TransactionDAO.TYPE_COLUMN).append(", ").append(TransactionDAO.STATE_COLUMN).append(", ").append(TransactionDAO.TXN_STATE_COLUMN).append(", ").append(TransactionDAO.TXN_HOME_BROKER_COLUMN).append(", ").append(TransactionDAO.TXN_BROKERS_COLUMN).append(", ").append(TransactionDAO.STORE_SESSION_ID_COLUMN).append(" FROM ").append(oldtxntbl).append(" WHERE ").append(TransactionDAO.ID_COLUMN).append(" NOT IN (SELECT ").append(TransactionDAO.ID_COLUMN).append(" FROM ").append(txnDAO.getTableName()).append(')').toString();
    // SQL to insert transactions to new table
    String insertTxnSQL = new StringBuilder(128).append("INSERT INTO ").append(txnDAO.getTableName()).append(" ( ").append(TransactionDAO.ID_COLUMN).append(", ").append(TransactionDAO.TYPE_COLUMN).append(", ").append(TransactionDAO.STATE_COLUMN).append(", ").append(TransactionDAO.AUTO_ROLLBACK_COLUMN).append(", ").append(TransactionDAO.XID_COLUMN).append(", ").append(TransactionDAO.TXN_STATE_COLUMN).append(", ").append(TransactionDAO.TXN_HOME_BROKER_COLUMN).append(", ").append(TransactionDAO.TXN_BROKERS_COLUMN).append(", ").append(TransactionDAO.STORE_SESSION_ID_COLUMN).append(", ").append(TransactionDAO.EXPIRED_TS_COLUMN).append(", ").append(TransactionDAO.ACCESSED_TS_COLUMN).append(") VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )").toString();
    boolean dobatch = dbMgr.supportsBatchUpdates();
    PreparedStatement pstmt = null;
    Statement stmt = null;
    ResultSet rs = null;
    TransactionUID tid = null;
    Exception myex = null;
    try {
        pstmt = dbMgr.createPreparedStatement(conn, insertTxnSQL);
        stmt = conn.createStatement();
        rs = dbMgr.executeQueryStatement(stmt, getAllTxnsFromOldSQL);
        while (rs.next()) {
            long id = rs.getLong(1);
            tid = new TransactionUID(id);
            int type = rs.getInt(2);
            int state = rs.getInt(3);
            TransactionState txnState = (TransactionState) Util.readObject(rs, 4);
            txnState.setState(state);
            BrokerAddress txnHomeBroker = (BrokerAddress) Util.readObject(rs, 5);
            TransactionBroker[] txnBrokers = (TransactionBroker[]) Util.readObject(rs, 6);
            long sessionID = rs.getLong(7);
            // insert in new table
            try {
                pstmt.setLong(1, id);
                pstmt.setInt(2, type);
                pstmt.setInt(3, state);
                pstmt.setInt(4, txnState.getType().intValue());
                JMQXid jmqXid = txnState.getXid();
                if (jmqXid != null) {
                    pstmt.setString(5, jmqXid.toString());
                } else {
                    pstmt.setNull(5, Types.VARCHAR);
                }
                Util.setObject(pstmt, 6, txnState);
                Util.setObject(pstmt, 7, txnHomeBroker);
                Util.setObject(pstmt, 8, txnBrokers);
                pstmt.setLong(9, sessionID);
                pstmt.setLong(10, txnState.getExpirationTime());
                pstmt.setLong(11, txnState.getLastAccessTime());
                if (dobatch) {
                    pstmt.addBatch();
                } else {
                    pstmt.executeUpdate();
                }
            } catch (IOException e) {
                IOException ex = DBManager.wrapIOException("[" + insertTxnSQL + "]", e);
                throw ex;
            } catch (SQLException e) {
                SQLException ex = DBManager.wrapSQLException("[" + insertTxnSQL + "]", e);
                throw ex;
            }
        }
        if (dobatch) {
            pstmt.executeBatch();
        }
        conn.commit();
    } catch (Exception e) {
        myex = e;
        String errorMsg = br.getKString(BrokerResources.X_JDBC_UPGRADE_TRANSACTIONS_FAILED, (tid == null ? "loading" : tid.toString()));
        logger.logStack(Logger.ERROR, errorMsg, e);
        throw new BrokerException(errorMsg, e);
    } finally {
        Util.close(rs, stmt, null, myex);
        Util.close(null, pstmt, null, myex);
    }
}
Also used : TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) JMQXid(com.sun.messaging.jmq.util.JMQXid) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) TransactionBroker(com.sun.messaging.jmq.jmsserver.data.TransactionBroker)

Aggregations

TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)71 TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)23 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)22 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)19 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)18 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)15 JMQXid (com.sun.messaging.jmq.util.JMQXid)13 IOException (java.io.IOException)13 HashMap (java.util.HashMap)12 TransactionBroker (com.sun.messaging.jmq.jmsserver.data.TransactionBroker)10 SelectorFormatException (com.sun.messaging.jmq.util.selector.SelectorFormatException)10 ArrayList (java.util.ArrayList)10 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)8 Consumer (com.sun.messaging.jmq.jmsserver.core.Consumer)7 TransactionAcknowledgement (com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement)7 ConnectionUID (com.sun.messaging.jmq.jmsserver.service.ConnectionUID)7 Iterator (java.util.Iterator)7 LinkedHashMap (java.util.LinkedHashMap)7 Map (java.util.Map)7 BrokerAddress (com.sun.messaging.jmq.jmsserver.core.BrokerAddress)6