Search in sources :

Example 21 with CommDBManager

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

the class ShareConfigRecordDAOImpl method getRecords.

/**
 * Get records since sequence seq (exclusive) or get all records
 *
 * @param conn database connection
 * @param seq sequence number, null if get all records
 * @param resetUUID last reset UUID this broker has processed
 * @return an array of ShareConfigRecord
 */
@Override
public List<ChangeRecordInfo> getRecords(Connection conn, Long seq, String resetUUID, boolean canReset) throws BrokerException {
    ArrayList<ChangeRecordInfo> records = new ArrayList<>();
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    String selectSQL = null;
    Exception myex = null;
    try {
        // Get a connection
        CommDBManager mgr = getDBManager();
        if (conn == null) {
            conn = mgr.getConnection(false);
            myConn = true;
        }
        if (seq == null || resetUUID == null) {
            records = getAllRecords(conn, null);
        } else {
            selectSQL = selectSinceWithResetRecordSQL;
            pstmt = mgr.createPreparedStatement(conn, selectSQL);
            pstmt.setLong(1, seq.longValue());
            rs = pstmt.executeQuery();
            long seqv = -1;
            String uuidv = null;
            byte[] buf = null;
            int typv = 0;
            long tsv = -1;
            ChangeRecordInfo cri = null;
            boolean reseted = false, foundreset = false;
            String newResetUUID = null;
            while (rs.next()) {
                try {
                    seqv = rs.getLong(1);
                    uuidv = rs.getString(2);
                    buf = Util.readBytes(rs, 3);
                    typv = rs.getInt(4);
                    tsv = rs.getLong(5);
                    if (typv == ChangeRecordInfo.TYPE_RESET_PERSISTENCE) {
                        foundreset = true;
                        if (uuidv.equals(resetUUID)) {
                            continue;
                        }
                        newResetUUID = uuidv;
                        reseted = true;
                        break;
                    }
                    cri = new ChangeRecordInfo(Long.valueOf(seqv), uuidv, buf, typv, null, tsv);
                    cri.setResetUUID(resetUUID);
                    cri.setIsSelectAll(false);
                    records.add(cri);
                } catch (IOException e) {
                    IOException ex = getDBManager().wrapIOException("[" + selectAllSQL + "]", e);
                    logger.logStack(Logger.ERROR, BrokerResources.X_PARSE_CONFIGRECORD_FAILED, String.valueOf(seq), ex);
                }
            }
            if (!foundreset) {
                throw new BrokerException("Unexpected: shared database table " + getTableName() + " has no reset record", Status.PRECONDITION_FAILED);
            }
            if (reseted) {
                if (!canReset) {
                    throw new BrokerException(br.getKString(br.X_SHARECC_TABLE_RESET, "[" + resetUUID + ", " + newResetUUID + "]"), Status.PRECONDITION_FAILED);
                }
                logger.log(logger.INFO, br.getKString(br.I_SHARECC_USE_RESETED_TABLE, "[" + resetUUID + ", " + newResetUUID + "]"));
                records = getAllRecords(conn, null);
            }
        }
        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);
        }
        Exception ex;
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        } else if (e instanceof SQLException) {
            ex = getDBManager().wrapSQLException("[" + selectSQL + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(br.X_SHARECC_QUERY_RECORDS_FAIL, 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 22 with CommDBManager

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

the class ShareConfigRecordDAOImpl method insert.

/**
 * Insert a new entry.
 *
 * @param conn database connection
 * @param rec the record to be inserted
 * @return the record inserted
 */
@Override
public ChangeRecordInfo insert(Connection conn, ChangeRecordInfo rec) throws BrokerException {
    String sql = null;
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Exception myex = null;
    try {
        CommDBManager mgr = getDBManager();
        if (conn == null) {
            conn = mgr.getConnection(mgr.isDerby() || mgr.isDB2());
            myConn = true;
        }
        String resetUUID = rec.getResetUUID();
        if (rec.isDuraAddRecord()) {
            Integer flag = hasLastSeqForUKeyType(conn, rec.getUKey(), rec.getType());
            if (flag != null) {
                String emsg = br.getKString(br.I_SHARECC_RECORD_UKEY_TYPE_EXIST, rec.getUKey()) + "[" + rec.getType() + "]" + ChangeRecordInfo.getFlagString(flag);
                logger.log(logger.INFO, emsg);
                if (flag.intValue() != rec.getFlag()) {
                    throw new BrokerException(emsg);
                }
            }
        }
        if (mgr.isOracle()) {
            sql = insertSQLOracle;
        } else {
            sql = insertSQL;
        }
        if (mgr.supportsGetGeneratedKey()) {
            if (mgr.isPostgreSQL() || mgr.isDB2()) {
                pstmt = mgr.createPreparedStatement(conn, sql, Statement.RETURN_GENERATED_KEYS);
            } else {
                pstmt = mgr.createPreparedStatement(conn, sql, new String[] { SEQ_COLUMN });
            }
        } else {
            pstmt = mgr.createPreparedStatement(conn, sql);
        }
        pstmt.setString(1, rec.getUUID());
        Util.setBytes(pstmt, 2, rec.getRecord());
        pstmt.setInt(3, rec.getType());
        pstmt.setString(4, rec.getUKey());
        pstmt.setLong(5, rec.getTimestamp());
        pstmt.setInt(6, rec.getFlag());
        pstmt.executeUpdate();
        Long seq = null;
        if (mgr.supportsGetGeneratedKey()) {
            rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                seq = Long.valueOf(rs.getLong(1));
            }
        } else if (mgr.isOracle()) {
            Statement st = conn.createStatement();
            rs = mgr.executeQueryStatement(st, selectSeqSQLOracle);
            if (rs.next()) {
                seq = Long.valueOf(rs.getLong(1));
            }
            rs.close();
            rs = null;
            st.close();
        } else {
            seq = getSequenceByUUID(conn, rec.getUUID());
        }
        if (seq == null) {
            throw new BrokerException(br.getKString(br.X_SHARECC_FAIL_GET_SEQ_ON_INSERT, rec));
        }
        String currResetUUID = getResetRecordUUID(conn);
        if (resetUUID != null && !currResetUUID.equals(resetUUID)) {
            throw new BrokerException(br.getKString(br.X_SHARECC_TABLE_RESET, "[" + resetUUID + ", " + currResetUUID + "]"), Status.PRECONDITION_FAILED);
        }
        if (myConn && !conn.getAutoCommit()) {
            conn.commit();
        }
        if (rec.isDuraAddRecord()) {
            Integer flag = checkLastUKeyHasSeq(conn, rec.getUKey(), seq.longValue());
            if (flag != null) {
                String emsg = br.getKString(br.I_SHARECC_RECORD_UKEY_TYPE_EXIST, rec.getUKey()) + "[" + rec.getType() + "]" + ChangeRecordInfo.getFlagString(flag);
                logger.log(logger.INFO, emsg);
                if (flag.intValue() != rec.getFlag()) {
                    throw new BrokerException(emsg);
                }
            }
        }
        if (resetUUID == null) {
            rec.setResetUUID(currResetUUID);
        }
        rec.setSeq(seq);
        return rec;
    } 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("[" + sql + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(br.X_SHARECC_INSERT_RECORD_FAIL, rec.toString(), 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 23 with CommDBManager

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

the class ShareConfigRecordDAOImpl method getResetRecordUUID.

/**
 * @param conn database connection
 */
public String getResetRecordUUID(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, selectResetRecordUUIDSQL);
        rs = pstmt.executeQuery();
        String uuid = null;
        if (rs.next()) {
            uuid = rs.getString(1);
        }
        if (uuid == null) {
            throw new BrokerException("No reset record found in database table " + getTableName());
        }
        return uuid;
    } 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("[" + selectResetRecordUUIDSQL + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(br.X_SHARECC_QUERY_RESET_RECORD_UUID_FAIL, 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 24 with CommDBManager

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

the class ShareConfigRecordDAOImpl method hasLastSeqForUKeyType.

private Integer hasLastSeqForUKeyType(Connection conn, String ukey, int type) throws BrokerException {
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Exception myex = null;
    String sql = selectTypeFlagByMaxSeqUKeySQL;
    try {
        // Get a connection
        CommDBManager mgr = getDBManager();
        if (conn == null) {
            conn = mgr.getConnection(true);
            myConn = true;
        }
        pstmt = mgr.createPreparedStatement(conn, sql);
        pstmt.setString(1, ukey);
        rs = pstmt.executeQuery();
        if (!rs.next()) {
            return null;
        }
        int t = rs.getInt(1);
        int flag = rs.getInt(2);
        if (t == type) {
            return Integer.valueOf(flag);
        }
        return null;
    } 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("[" + sql + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(br.X_SHARECC_QUERY_MAX_SEQ_UKEY, ukey + "[" + type + "]") + ": " + ex.toString(), 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)

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