Search in sources :

Example 21 with JMQXid

use of com.sun.messaging.jmq.util.JMQXid 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 22 with JMQXid

use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.

the class TransactionDAOImpl method insert.

/**
 * Insert a new entry.
 *
 * @param conn database connection
 * @param txnUID the transaction ID
 * @param txnState the TransactionState
 * @param txnHomeBroker the home broker for a REMOTE txn
 * @param txnBrokers the participant brokers for a REMOTE/CLUSTER txn
 * @param storeSessionID the store session ID
 */
@Override
public void insert(Connection conn, TransactionUID txnUID, TransactionState txnState, BrokerAddress txnHomeBroker, TransactionBroker[] txnBrokers, int type, long storeSessionID) throws BrokerException {
    long id = txnUID.longValue();
    boolean myConn = false;
    PreparedStatement pstmt = null;
    Exception myex = null;
    try {
        // Get a connection
        DBManager dbMgr = DBManager.getDBManager();
        if (conn == null) {
            conn = dbMgr.getConnection(true);
            myConn = true;
        }
        try {
            pstmt = dbMgr.createPreparedStatement(conn, insertSQL);
            pstmt.setLong(1, id);
            pstmt.setInt(2, type);
            pstmt.setInt(3, txnState.getState());
            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, storeSessionID);
            pstmt.setLong(10, txnState.getExpirationTime());
            pstmt.setLong(11, txnState.getLastAccessTime());
            pstmt.executeUpdate();
        } catch (Exception e) {
            myex = e;
            try {
                if (!conn.getAutoCommit()) {
                    conn.rollback();
                }
            } catch (SQLException rbe) {
                logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED, rbe);
            }
            try {
                // the chance of inserting duplicate record is very small
                if (hasTransaction(conn, id)) {
                    throw new BrokerException(br.getKString(BrokerResources.E_TRANSACTIONID_EXISTS_IN_STORE, txnUID.toString()));
                }
            } catch (Exception e2) {
                // Ignore this exception so orig exception can be thrown!
                logger.log(Logger.WARNING, br.getKString(br.X_STORE_CHECK_EXISTENCE_TXN_AFTER_ADD_FAILURE, txnUID + "[" + storeSessionID + ", " + txnHomeBroker + "]", e2.getMessage()));
            }
            Exception ex;
            if (e instanceof BrokerException) {
                throw (BrokerException) e;
            } else if (e instanceof IOException) {
                ex = DBManager.wrapIOException("[" + insertSQL + "]", (IOException) e);
            } else if (e instanceof SQLException) {
                ex = DBManager.wrapSQLException("[" + insertSQL + "]", (SQLException) e);
            } else {
                ex = e;
            }
            throw new BrokerException(br.getKString(BrokerResources.X_PERSIST_TRANSACTION_FAILED, txnUID), ex);
        }
    } catch (BrokerException e) {
        myex = e;
        throw e;
    } finally {
        if (myConn) {
            Util.close(null, pstmt, conn, myex);
        } else {
            Util.close(null, pstmt, null, myex);
        }
    }
}
Also used : IOException(java.io.IOException) JMQXid(com.sun.messaging.jmq.util.JMQXid) IOException(java.io.IOException)

Example 23 with JMQXid

use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.

the class JMSServiceImpl method recoverTransaction.

/**
 * Recover a transaction from the broker.
 *
 * @param connectionId The Id of the connection
 * @param transactionId The id of the transaction to recover
 *
 * @return The transactionId is returned if the transaction is in the PREPARED state. If the transaction is not found or
 * not in the PREPARED state a zero (0L) is returned.
 */
@Override
public long recoverTransaction(long connectionId, long transactionId) throws JMSServiceException {
    // IMQConnection cxn;
    TransactionUID txnUID = null;
    JMQXid[] jmqXids;
    // cxn = checkConnectionId(connectionId, "recoverTransaction");
    checkConnectionId(connectionId, "recoverTransaction");
    txnUID = new TransactionUID(transactionId);
    try {
        jmqXids = protocol.recoverTransaction(txnUID);
        if (jmqXids.length > 0) {
            return (transactionId);
        }
    } catch (Exception e) {
        String errStr = "recoverTransaction: recover transaction failed. Connection ID:" + connectionId + ", Transaction ID: " + transactionId;
        logger.logStack(Logger.ERROR, errStr, e);
        HashMap props = new HashMap();
        props.put("JMQStatus", getErrorReplyStatus(e));
        throw new JMSServiceException(errStr, e, props);
    }
    return (0);
}
Also used : TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) JMQXid(com.sun.messaging.jmq.util.JMQXid) SelectorFormatException(com.sun.messaging.jmq.util.selector.SelectorFormatException) AccessControlException(java.security.AccessControlException)

Example 24 with JMQXid

use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.

the class JMSServiceImpl method commitTransaction.

/**
 * Commit a transaction.
 *
 * @param connectionId The Id of the connection
 * @param transactionId If non-zero, the transaction being committed is identified by this broker-generated id
 * @param xid If transactionId is zero, then xid contains the Xid of the XA transaction being committed
 * @param flags If this is an XA transaction, then flags is one of:
 * <UL>
 * <LI>XAResource.TMONEPHASE:One phase commit. The transaction need not be in the PREPARED state</LI>
 * <LI>XAResource.TMNOFLAGS: Two phase commit. The transaction must be in the PREPARED state</LI>
 * </UL>
 *
 * @return The JMSServiceReply of the request to commit a transaction
 *
 * @throws JMSServiceException if the Status returned for the commitTransaction method is not
 * {@link JMSServiceReply.Status#OK}
 *
 * @see JMSServiceReply.Status#getJMQTransactionID
 *
 * @see JMSServiceReply.Status#BAD_REQUEST
 * @see JMSServiceReply.Status#NOT_FOUND
 * @see JMSServiceReply.Status#PRECONDITION_FAILED
 * @see JMSServiceReply.Status#TIMEOUT
 * @see JMSServiceReply.Status#ERROR
 */
@Override
public JMSServiceReply commitTransaction(long connectionId, long transactionId, Xid xid, int flags) throws JMSServiceException {
    JMSServiceReply reply;
    HashMap props = new HashMap();
    IMQConnection cxn;
    TransactionUID txnUID = null;
    JMQXid jmqXid = null;
    Integer xaFlags;
    cxn = checkConnectionId(connectionId, "commitTransaction");
    txnUID = new TransactionUID(transactionId);
    if (xid != null) {
        jmqXid = new JMQXid(xid);
    }
    xaFlags = Integer.valueOf(flags);
    try {
        protocol.commitTransaction(txnUID, jmqXid, xaFlags, cxn);
    } catch (BrokerException be) {
        String errStr = "CommitTransaction: commit failed. Connection ID: " + connectionId + ", Transaction ID: " + transactionId;
        logger.logStack(Logger.ERROR, errStr, be);
        props.put("JMQStatus", getErrorReplyStatus(be));
        throw new JMSServiceException(errStr, be, props);
    }
    props.put("JMQStatus", JMSServiceReply.Status.OK);
    props.put("JMQTransactionID", txnUID.longValue());
    reply = new JMSServiceReply(props);
    return (reply);
}
Also used : TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) JMQXid(com.sun.messaging.jmq.util.JMQXid)

Example 25 with JMQXid

use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.

the class JMSServiceImpl method startTransaction.

/**
 * Start a transaction.
 *
 * @param connectionId The Id of the connection
 * @param sessionId If non-zero, this is the Id of the session in which the transaction is being created. This parameter
 * is zero for XA transactions
 * @param xid If non-null, an XA transaction is being started
 * @param flags If xId is non-null, then flags is one of:
 * <p>
 * <UL>
 * <LI>XAResource.TMNOFLAGS = start a brand new transaction</LI>
 * <LI>XAResource.TMRESUNE = resume a previously suspended transaction</LI>
 * </UL>
 * @param rollback The type of transaction rollback behavior to use
 * @param timeout The transaction timeout to use. The timeout is the maximum time in seconds that the transaction will
 * be allowed to be in an un-prepared state.
 *
 * @return The JMSServiceReply of the request to start a transaction
 *
 * @throws JMSServiceException if the Status returned for the startTransaction method is not
 * {@link JMSServiceReply.Status#OK}
 */
@Override
public JMSServiceReply startTransaction(long connectionId, long sessionId, Xid xid, int flags, JMSService.TransactionAutoRollback rollback, long timeout) throws JMSServiceException {
    JMSServiceReply reply;
    HashMap props = new HashMap();
    IMQConnection cxn;
    TransactionUID txnUID = null;
    JMQXid jmqXid = null;
    Integer xaFlags = null;
    cxn = checkConnectionId(connectionId, "startTransaction");
    if (xid != null) {
        jmqXid = new JMQXid(xid);
        xaFlags = Integer.valueOf(flags);
    }
    try {
        txnUID = protocol.startTransaction(jmqXid, xaFlags, null, /* type */
        timeout, cxn);
    } catch (BrokerException be) {
        String errStr = "startTransaction: start transaction failed. Connection ID: " + connectionId + ", session ID: " + sessionId + ", XID: " + xid;
        logger.logStack(Logger.ERROR, errStr, be);
        props.put("JMQStatus", getErrorReplyStatus(be));
        throw new JMSServiceException(errStr, be, props);
    }
    props.put("JMQStatus", JMSServiceReply.Status.OK);
    props.put("JMQTransactionID", txnUID.longValue());
    reply = new JMSServiceReply(props);
    return (reply);
}
Also used : TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) JMQXid(com.sun.messaging.jmq.util.JMQXid)

Aggregations

JMQXid (com.sun.messaging.jmq.util.JMQXid)39 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)13 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)9 TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)6 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)6 XidImpl (com.sun.messaging.jmq.util.XidImpl)4 JMSException (jakarta.jms.JMSException)4 Hashtable (java.util.Hashtable)4 XAException (javax.transaction.xa.XAException)4 JMSRASessionAdapter (com.sun.messaging.jms.ra.api.JMSRASessionAdapter)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3 Map (java.util.Map)3 BrokerAddress (com.sun.messaging.jmq.jmsserver.core.BrokerAddress)2 TransactionBroker (com.sun.messaging.jmq.jmsserver.data.TransactionBroker)2 TransactionHandler (com.sun.messaging.jmq.jmsserver.data.handlers.TransactionHandler)2 PartitionedStore (com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)2 UnknownTransactionException (com.sun.messaging.jmq.jmsserver.util.UnknownTransactionException)2 SelectorFormatException (com.sun.messaging.jmq.util.selector.SelectorFormatException)2