Search in sources :

Example 36 with TransactionUID

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

the class UpgradeStore method upgradeTxns.

/**
 * Upgrade transactions from version 350/370/400 table to current format (410)
 */
private void upgradeTxns(Connection conn) throws BrokerException {
    TransactionDAO txnDAO = dbMgr.getDAOFactory().getTransactionDAO();
    // SQL to select all transactions from version 350/370 table
    StringBuilder strBuf = new StringBuilder(128);
    if (oldStoreVersion == JDBCStore.OLD_STORE_VERSION_400) {
        strBuf.append("SELECT ").append(TransactionDAO.ID_COLUMN).append(", ").append(TransactionDAO.STATE_COLUMN).append(", ").append(TransactionDAO.TXN_STATE_COLUMN);
    } else {
        strBuf.append("SELECT ").append(TTXN_CTUID).append(", ").append(TTXN_CSTATE).append(", ").append(TTXN_CSTATEOBJ);
    }
    strBuf.append(" FROM ").append(oldTxnTable).append(" WHERE ").append(TTXN_CSTATE).append(" <> ").append(DBConstants.TXN_DELETED);
    String getAllTxnsFromOldSQL = strBuf.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 state = rs.getInt(2);
            TransactionState txnState = (TransactionState) Util.readObject(rs, 3);
            if (DEBUG) {
                String msg = "reading transaction from old format: state=" + state + " txnState = " + txnState;
                logger.log(Logger.DEBUG, msg);
            }
            txnState.setState(state);
            // insert in new table
            try {
                pstmt.setLong(1, id);
                pstmt.setInt(2, TransactionInfo.TXN_LOCAL);
                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, null);
                Util.setObject(pstmt, 8, null);
                pstmt.setLong(9, storeSessionID);
                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();
    // Only delete txn table after we upgrade txn ack!!!
    } 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)

Example 37 with TransactionUID

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

the class ConsumerStateDAOImpl method updateTransaction.

/**
 * Update existing entry.
 *
 * @param conn database connection
 * @param sysMsgID the system message ID
 * @param conUID the consumer id
 * @param txnUID the transaction id associated with an acknowledgment
 */
@Override
public void updateTransaction(Connection conn, SysMessageID sysMsgID, ConsumerUID conUID, TransactionUID txnUID) throws BrokerException {
    String msgID = sysMsgID.getUniqueName();
    boolean myConn = false;
    PreparedStatement pstmt = null;
    Exception myex = null;
    String sql = updateTransactionSQL;
    try {
        // Get a connection
        DBManager dbMgr = DBManager.getDBManager();
        if (conn == null) {
            conn = dbMgr.getConnection(true);
            myConn = true;
        }
        // Make sure the transaction exists
        dbMgr.getDAOFactory().getTransactionDAO().checkTransaction(conn, txnUID.longValue());
        pstmt = dbMgr.createPreparedStatement(conn, sql);
        pstmt.setLong(1, txnUID.longValue());
        pstmt.setString(2, msgID);
        pstmt.setLong(3, conUID.longValue());
        if (Globals.getHAEnabled()) {
            pstmt.setString(4, dbMgr.getBrokerID());
        }
        if (pstmt.executeUpdate() == 0) {
            // For HA mode, check if this broker still owns the store
            if (Globals.getHAEnabled()) {
                String brokerID = dbMgr.getBrokerID();
                BrokerDAO dao = dbMgr.getDAOFactory().getBrokerDAO();
                if (dao.isBeingTakenOver(conn, brokerID)) {
                    BrokerException be = new StoreBeingTakenOverException(br.getKString(BrokerResources.E_STORE_BEING_TAKEN_OVER));
                    try {
                        HABrokerInfo bkrInfo = dao.getBrokerInfo(conn, brokerID);
                        logger.logStack(Logger.ERROR, be.getMessage() + "[" + (bkrInfo == null ? "" + brokerID : bkrInfo.toString()) + "]", be);
                    } catch (Throwable t) {
                    /* Ignore error */
                    }
                    throw be;
                }
            }
            // Check if ack exists
            long existingTxnId = getTransaction(conn, sysMsgID, conUID);
            if (existingTxnId > 0) {
                TransactionUID oldTxnUID = new TransactionUID(existingTxnId);
                TransactionState ts = dbMgr.getDAOFactory().getTransactionDAO().getTransactionState(conn, oldTxnUID);
                String[] args = { "[" + sysMsgID + ", " + conUID + "]", oldTxnUID + "[" + ts + "]", txnUID.toString() };
                logger.log(logger.WARNING, br.getKString(BrokerResources.W_STORE_TXN_ACK_EXIST, args));
                // message can be redelivered but rollback transaction failed
                updateTransactionNoCheck(conn, sysMsgID, conUID, txnUID);
                return;
            }
            // We're assuming the entry does not exist
            throw new BrokerException(br.getKString(BrokerResources.E_INTEREST_STATE_NOT_FOUND_IN_STORE, conUID.toString(), msgID), Status.NOT_FOUND);
        }
    } catch (Exception e) {
        myex = e;
        try {
            if ((conn != null) && !conn.getAutoCommit()) {
                conn.rollback();
            }
        } catch (SQLException rbe) {
            logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED + "[" + sql + "]", rbe);
        }
        Exception ex;
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        } else if (e instanceof SQLException) {
            ex = DBManager.wrapSQLException("[" + sql + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(BrokerResources.X_PERSIST_INTEREST_STATE_FAILED, conUID.toString(), sysMsgID.toString()), ex);
    } finally {
        if (myConn) {
            Util.close(null, pstmt, conn, myex);
        } else {
            Util.close(null, pstmt, null, myex);
        }
    }
}
Also used : TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID)

Example 38 with TransactionUID

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

the class ConsumerStateDAOImpl method updateTransactionNoCheck.

private void updateTransactionNoCheck(Connection conn, SysMessageID sysMsgID, ConsumerUID conUID, TransactionUID txnUID) throws BrokerException {
    String msgID = sysMsgID.getUniqueName();
    boolean myConn = false;
    PreparedStatement pstmt = null;
    Exception myex = null;
    String sql = updateTransactionNoCheckSQL;
    try {
        // Get a connection
        DBManager dbMgr = DBManager.getDBManager();
        if (conn == null) {
            conn = dbMgr.getConnection(true);
            myConn = true;
        }
        // Make sure the transaction exists
        dbMgr.getDAOFactory().getTransactionDAO().checkTransaction(conn, txnUID.longValue());
        pstmt = dbMgr.createPreparedStatement(conn, sql);
        pstmt.setLong(1, txnUID.longValue());
        pstmt.setString(2, msgID);
        pstmt.setLong(3, conUID.longValue());
        pstmt.setLong(4, txnUID.longValue());
        if (Globals.getHAEnabled()) {
            pstmt.setString(5, dbMgr.getBrokerID());
        }
        if (pstmt.executeUpdate() == 0) {
            if (Globals.getHAEnabled()) {
                String brokerID = dbMgr.getBrokerID();
                BrokerDAO dao = dbMgr.getDAOFactory().getBrokerDAO();
                if (dao.isBeingTakenOver(conn, brokerID)) {
                    BrokerException be = new StoreBeingTakenOverException(br.getKString(BrokerResources.E_STORE_BEING_TAKEN_OVER));
                    try {
                        HABrokerInfo bkrInfo = dao.getBrokerInfo(conn, brokerID);
                        logger.logStack(Logger.ERROR, be.getMessage() + "[" + (bkrInfo == null ? "" + brokerID : bkrInfo.toString()) + "]", be);
                    } catch (Throwable t) {
                    /* Ignore error */
                    }
                    throw be;
                }
            }
            long existingTxnId = getTransaction(conn, sysMsgID, conUID);
            if (existingTxnId > 0) {
                TransactionUID oldTxnUID = new TransactionUID(existingTxnId);
                TransactionState ts = dbMgr.getDAOFactory().getTransactionDAO().getTransactionState(conn, oldTxnUID);
                String[] args = { "[" + sysMsgID + ", " + conUID + "]TID=" + oldTxnUID + "(" + ts + ")", txnUID.toString() };
                throw new BrokerException(br.getKString(br.E_ACK_EXISTS_IN_STORE, args));
            }
            throw new BrokerException(br.getKString(BrokerResources.E_INTEREST_STATE_NOT_FOUND_IN_STORE, conUID.toString(), msgID), Status.NOT_FOUND);
        }
    } catch (Exception e) {
        myex = e;
        try {
            if ((conn != null) && !conn.getAutoCommit()) {
                conn.rollback();
            }
        } catch (SQLException rbe) {
            logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED + "[" + sql + "]", rbe);
        }
        Exception ex;
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        } else if (e instanceof SQLException) {
            ex = DBManager.wrapSQLException("[" + sql + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(BrokerResources.X_PERSIST_INTEREST_STATE_FAILED, conUID.toString(), sysMsgID.toString()), ex);
    } finally {
        if (myConn) {
            Util.close(null, pstmt, conn, myex);
        } else {
            Util.close(null, pstmt, null, myex);
        }
    }
}
Also used : TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID)

Example 39 with TransactionUID

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

the class TransactionDAOImpl method getRemoteTransactionsByBroker.

/**
 * Retrieve all remote transaction IDs that this broker participates in.
 *
 * @param conn database connection
 * @param brokerID the broker ID
 * @return a List of TransactionUID objects; an empty List is returned if the broker does not have any transactions
 */
@Override
public List getRemoteTransactionsByBroker(Connection conn, String brokerID) throws BrokerException {
    List list = new ArrayList();
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Exception myex = null;
    String sql = null;
    try {
        // Get a connection
        DBManager dbMgr = DBManager.getDBManager();
        if (conn == null) {
            conn = dbMgr.getConnection(true);
            myConn = true;
        }
        if (Globals.getHAEnabled()) {
            sql = selectRemoteTxnStatesByBrokerAndTypeSQL;
        } else {
            sql = selectTxnStatesByBrokerAndTypeSQL;
        }
        if (Globals.getHAEnabled()) {
            pstmt = dbMgr.createPreparedStatement(conn, sql);
            pstmt.setInt(1, TransactionInfo.TXN_CLUSTER);
            pstmt.setString(2, brokerID);
            pstmt.setString(3, brokerID);
            if (DEBUG) {
                logger.log(logger.INFO, "TransactionDAOImpl.getRemoteTransactionStatesByBroker(): [" + sql + "](" + TransactionInfo.TXN_CLUSTER + "," + brokerID + "," + brokerID + ")");
            }
        } else {
            pstmt = dbMgr.createPreparedStatement(conn, sql);
            pstmt.setString(1, brokerID);
            pstmt.setInt(2, TransactionInfo.TXN_REMOTE);
            if (DEBUG) {
                logger.log(logger.INFO, "TransactionDAOImpl.getRemoteTransactionStatesByBroker(): [" + sql + "](" + brokerID + "," + TransactionInfo.TXN_REMOTE + ")");
            }
        }
        rs = pstmt.executeQuery();
        while (rs.next()) {
            long id = rs.getLong(1);
            list.add(new TransactionUID(id));
            if (DEBUG) {
                logger.log(logger.INFO, "TransactionDAOImpl.getRemoteTransactionStatesByBroker(): Result[" + id + "]");
            }
        }
    } catch (Exception e) {
        myex = e;
        try {
            if ((conn != null) && !conn.getAutoCommit()) {
                conn.rollback();
            }
        } catch (SQLException rbe) {
            logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED, rbe);
        }
        Exception ex;
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        } else if (e instanceof SQLException) {
            ex = DBManager.wrapSQLException("[" + sql + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(BrokerResources.X_LOAD_TXNS_FOR_BROKER_FAILED, brokerID), ex);
    } finally {
        if (myConn) {
            Util.close(rs, pstmt, conn, myex);
        } else {
            Util.close(rs, pstmt, null, myex);
        }
    }
    if (DEBUG) {
        logger.log(logger.INFO, "TransactionDAOImpl.getRemoteTransactionStatesByBroker(): ResultSet[size=" + list.size() + "]");
    }
    return list;
}
Also used : TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) IOException(java.io.IOException)

Example 40 with TransactionUID

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

the class TransactionDAOImpl method getTransactionStatesByBroker.

/**
 * Retrieve all local and cluster transaction states.
 *
 * @param conn database connection
 * @param brokerID the broker ID
 * @return a Map of transaction IDs and TransactionState objects; an empty Map is returned if no transactions exist in
 * the store
 */
@Override
public HashMap getTransactionStatesByBroker(Connection conn, String brokerID, Long storeSession) throws BrokerException {
    HashMap map = new HashMap();
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Exception myex = null;
    String sql = selectTxnStatesByBrokerSQL;
    try {
        // Get a connection
        DBManager dbMgr = DBManager.getDBManager();
        if (conn == null) {
            conn = dbMgr.getConnection(true);
            myConn = true;
        }
        if (brokerID == null) {
            brokerID = dbMgr.getBrokerID();
        }
        if (storeSession != null) {
            StoreSessionDAOImpl.checkStoreSessionOwner(conn, storeSession, brokerID);
            sql = selectTxnStatesBySessionSQL;
        }
        pstmt = dbMgr.createPreparedStatement(conn, sql);
        if (storeSession != null) {
            pstmt.setLong(1, storeSession.longValue());
        } else {
            pstmt.setString(1, brokerID);
        }
        rs = pstmt.executeQuery();
        while (rs.next()) {
            try {
                long id = rs.getLong(1);
                int type = rs.getInt(2);
                int state = rs.getInt(3);
                TransactionState txnState = (TransactionState) Util.readObject(rs, 4);
                txnState.setState(state);
                TransactionBroker[] txnBrokers = (TransactionBroker[]) Util.readObject(rs, 5);
                map.put(new TransactionUID(id), new TransactionInfo(txnState, null, txnBrokers, type));
            } catch (IOException e) {
                // fail to parse TransactionState object; just log it
                logger.logStack(Logger.ERROR, BrokerResources.X_PARSE_TRANSACTION_FAILED, e);
            }
        }
    } catch (Exception e) {
        myex = e;
        try {
            if ((conn != null) && !conn.getAutoCommit()) {
                conn.rollback();
            }
        } catch (SQLException rbe) {
            logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED, rbe);
        }
        Exception ex;
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        } else if (e instanceof SQLException) {
            ex = DBManager.wrapSQLException("[" + sql + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(BrokerResources.X_LOAD_TRANSACTIONS_FAILED), ex);
    } finally {
        if (myConn) {
            Util.close(rs, pstmt, conn, myex);
        } else {
            Util.close(rs, pstmt, null, myex);
        }
    }
    return map;
}
Also used : TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) IOException(java.io.IOException) IOException(java.io.IOException) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) TransactionBroker(com.sun.messaging.jmq.jmsserver.data.TransactionBroker) TransactionInfo(com.sun.messaging.jmq.jmsserver.persist.api.TransactionInfo)

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