Search in sources :

Example 11 with CommDBManager

use of com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager in project openmq by eclipse-ee4j.

the class ShareConfigRecordDAOImpl method getSequenceByUUID.

/**
 * Get the sequence number of the specified record
 *
 * @param conn database connection
 * @param uuid uid of the record
 * @return null if no such record
 */
private Long getSequenceByUUID(Connection conn, String uuid) throws BrokerException {
    Long seq = null;
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Exception myex = null;
    try {
        // Get a connection
        CommDBManager mgr = getDBManager();
        if (conn == null) {
            conn = mgr.getConnection(true);
            myConn = true;
        }
        pstmt = mgr.createPreparedStatement(conn, selectSeqByUUIDSQL);
        pstmt.setString(1, uuid);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            long seqv = rs.getLong(1);
            seq = Long.valueOf(seqv);
        }
    } 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 = getDBManager().wrapSQLException("[" + selectSeqByUUIDSQL + "]", (SQLException) e);
        } else {
            ex = e;
        }
        String[] args = new String[] { SEQ_COLUMN, String.valueOf(seq), ex.getMessage() };
        throw new BrokerException(br.getKString(br.X_SHARECC_QUERY_SEQ_BY_UUID_FAIL, args), ex);
    } finally {
        if (myConn) {
            closeSQLObjects(rs, pstmt, conn, myex);
        } else {
            closeSQLObjects(rs, pstmt, null, myex);
        }
    }
    return seq;
}
Also used : CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) IOException(java.io.IOException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException)

Example 12 with CommDBManager

use of com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager in project openmq by eclipse-ee4j.

the class ShareConfigRecordDAOImpl method setResetRecordFLAGNULL.

/**
 * @param conn database connection
 */
public void setResetRecordFLAGNULL(Connection conn) throws BrokerException {
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Exception myex = null;
    try {
        // Get a connection
        CommDBManager mgr = getDBManager();
        if (conn == null) {
            conn = mgr.getConnection(true);
            myConn = true;
        }
        pstmt = mgr.createPreparedStatement(conn, setResetRecordFLAGNULLSQL);
        if (pstmt.executeUpdate() < 1) {
            throw new BrokerException("Unexpected affected row count for " + setResetRecordFLAGNULLSQL);
        }
    } 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 = getDBManager().wrapSQLException("[" + setResetRecordFLAGNULLSQL + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(br.X_SHARECC_CLEAR_RESET_RECORD_FLAG_FAIL, FLAG_COLUMN, ex.getMessage()), ex);
    } finally {
        if (myConn) {
            closeSQLObjects(rs, pstmt, conn, myex);
        } else {
            closeSQLObjects(rs, pstmt, null, myex);
        }
    }
}
Also used : CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) IOException(java.io.IOException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException)

Example 13 with CommDBManager

use of com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager in project openmq by eclipse-ee4j.

the class ShareConfigRecordDAOImpl method getAllRecords.

@Override
public ArrayList<ChangeRecordInfo> getAllRecords(Connection conn, String query) throws BrokerException {
    String sql = (query == null ? selectAllSQL : query);
    ArrayList<ChangeRecordInfo> records = new ArrayList<>();
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Exception myex = null;
    try {
        // Get a connection
        CommDBManager mgr = getDBManager();
        if (conn == null) {
            conn = mgr.getConnection(true);
            myConn = true;
        }
        pstmt = mgr.createPreparedStatement(conn, sql);
        rs = pstmt.executeQuery();
        long seqv = -1;
        String uuidv = null;
        byte[] buf = null;
        int typv = 0;
        long tsv = -1;
        ChangeRecordInfo cri = null;
        boolean foundreset = false;
        while (rs.next()) {
            try {
                seqv = rs.getLong(1);
                uuidv = rs.getString(2);
                buf = Util.readBytes(rs, 3);
                typv = rs.getInt(4);
                if (typv == ChangeRecordInfo.TYPE_RESET_PERSISTENCE) {
                    foundreset = true;
                }
                tsv = rs.getLong(5);
                cri = new ChangeRecordInfo(Long.valueOf(seqv), uuidv, buf, typv, null, tsv);
                cri.setIsSelectAll(true);
                records.add(cri);
            } catch (IOException e) {
                IOException ex = getDBManager().wrapIOException("[" + selectAllSQL + "]", e);
                logger.logStack(Logger.ERROR, BrokerResources.X_PARSE_CONFIGRECORD_FAILED, String.valueOf(seqv), ex);
                throw new BrokerException(ex.getMessage(), Status.PRECONDITION_FAILED);
            }
        }
        if (!foundreset) {
            throw new BrokerException("Unexpected: shared cluster change record table [" + (query == null ? getTableName() : sql) + "] has no reset record", Status.PRECONDITION_FAILED);
        }
    } 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 = getDBManager().wrapSQLException("[" + sql + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(br.X_SHARECC_QUERY_ALL_RECORDS_FAIL + "[" + (query == null ? getTableName() : sql) + "]", ex.getMessage()), ex);
    } finally {
        if (myConn) {
            closeSQLObjects(rs, pstmt, conn, myex);
        } else {
            closeSQLObjects(rs, pstmt, null, myex);
        }
    }
    return records;
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) ChangeRecordInfo(com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)

Example 14 with CommDBManager

use of com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager in project openmq by eclipse-ee4j.

the class ShareConfigRecordDAOImpl method updateLockID.

/**
 */
@Override
public void updateLockID(Connection conn, String newLockID, String oldLockID) throws BrokerException {
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Exception myex = null;
    String sql = updateLockIDSQL;
    try {
        CommDBManager mgr = getDBManager();
        if (conn == null) {
            conn = mgr.getConnection(false);
            myConn = true;
        }
        pstmt = mgr.createPreparedStatement(conn, sql);
        Util.setString(pstmt, 1, newLockID);
        pstmt.setString(2, oldLockID);
        int cnt = pstmt.executeUpdate();
        if (cnt != 0) {
            return;
        }
        if (hasResetRecord(conn)) {
            String m = br.getKString(br.E_SHARECC_TABLE_NOT_EMPTY, getDBManager().getClusterID());
            throw new BrokerException(m, br.E_SHARECC_TABLE_NOT_EMPTY, null, Status.MOVED_PERMANENTLY);
        }
        return;
    } 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 = getDBManager().wrapSQLException("[" + sql + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(br.X_SHARECC_UPDATE_LOCKID, oldLockID, newLockID) + ": " + ex.getMessage(), ex);
    } finally {
        if (myConn) {
            closeSQLObjects(rs, pstmt, conn, myex);
        } else {
            closeSQLObjects(rs, pstmt, null, myex);
        }
    }
}
Also used : CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) IOException(java.io.IOException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException)

Example 15 with CommDBManager

use of com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager in project openmq by eclipse-ee4j.

the class ShareConfigRecordDAOImpl method insertResetRecord.

/**
 * Insert reset record.
 *
 * @param conn database connection
 * @param rec the reset record to be inserted
 */
@Override
public void insertResetRecord(Connection conn, ChangeRecordInfo rec, String lockID) throws BrokerException {
    String sql = null;
    boolean myConn = false;
    PreparedStatement pstmt = null;
    Exception myex = null;
    try {
        // Get a connection
        CommDBManager mgr = getDBManager();
        if (conn == null) {
            conn = mgr.getConnection(false);
            myConn = true;
        }
        if (!hasResetRecord(conn)) {
            sql = insertResetRecordSQL;
            if (lockID != null) {
                sql = insertResetRecordWithLockSQL;
            }
            pstmt = mgr.createPreparedStatement(conn, sql);
            pstmt.setString(1, rec.getUUID());
            Util.setBytes(pstmt, 2, rec.getRecord());
            pstmt.setInt(3, ChangeRecordInfo.TYPE_RESET_PERSISTENCE);
            pstmt.setString(4, rec.getUKey());
            pstmt.setLong(5, rec.getTimestamp());
            if (lockID != null) {
                pstmt.setString(6, lockID);
            }
            int count = pstmt.executeUpdate();
            if (count == 1) {
                setResetRecordUUID(conn, rec.getUUID());
            } else if (!hasResetRecord(conn) || lockID != null) {
                throw new BrokerException("Unexpected affected row count " + count + " on  " + sql);
            }
        } else if (lockID != null) {
            String m = br.getKString(br.E_SHARECC_TABLE_NOT_EMPTY, getDBManager().getClusterID());
            throw new BrokerException(m, br.E_SHARECC_TABLE_NOT_EMPTY, null, Status.MOVED_PERMANENTLY);
        }
        if (myConn) {
            conn.commit();
        }
    } 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);
        }
        try {
            if (hasResetRecord(conn)) {
                if (lockID != null) {
                    String m = br.getKString(br.E_SHARECC_TABLE_NOT_EMPTY, getDBManager().getClusterID());
                    throw new BrokerException(m, br.E_SHARECC_TABLE_NOT_EMPTY, null, Status.MOVED_PERMANENTLY);
                }
                myex = null;
                return;
            }
        } catch (Exception e2) {
            logger.log(Logger.ERROR, br.getKString(br.E_SHARECC_CHECK_EXIST_EXCEPTION, e2.getMessage()));
        }
        Exception ex;
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        } else if (e instanceof SQLException) {
            ex = getDBManager().wrapSQLException("[" + insertResetRecordSQL + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(br.X_SHARECC_INSERT_RESET_RECORD_FAIL, ex.getMessage()), ex);
    } finally {
        if (myConn) {
            closeSQLObjects(null, pstmt, conn, myex);
        } else {
            closeSQLObjects(null, pstmt, null, myex);
        }
    }
}
Also used : CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) IOException(java.io.IOException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException)

Aggregations

CommDBManager (com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager)24 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)13 IOException (java.io.IOException)13 ShareConfigChangeDBManager (com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigChangeDBManager)7 BaseDAO (com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.BaseDAO)5 StringUtil (com.sun.messaging.jmq.util.StringUtil)4 ChangeRecordInfo (com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)2 ArrayList (java.util.ArrayList)2 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)1 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)1 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)1 UID (com.sun.messaging.jmq.util.UID)1