Search in sources :

Example 6 with ShareConfigChangeDBManager

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

the class DBTool method doCommand.

void doCommand(String[] args) throws SQLException, BrokerException, IOException {
    // print all
    if (args.length == 0) {
        System.out.println(version.getBanner(true));
        System.out.println(br.getString(BrokerResources.M_DBMGR_USAGE));
        exit(0);
    }
    // print help
    if (printHelp(args)) {
        System.out.println(br.getString(BrokerResources.M_DBMGR_USAGE));
        exit(0);
    }
    // print version
    if (printVersion(args)) {
        System.out.println(version.getBanner(true));
        System.out.println(br.getString(BrokerResources.I_JAVA_VERSION) + System.getProperty("java.version") + " " + System.getProperty("java.vendor") + " " + System.getProperty("java.home"));
        System.out.println(br.getString(BrokerResources.I_JAVA_CLASSPATH) + System.getProperty("java.class.path"));
        exit(0);
    }
    Properties props = null;
    try {
        props = parseArgs(args);
    } catch (ParserException e) {
        handleParserException(e);
        exit(1);
    }
    props.getProperty(DBManager.JDBC_PROP_PREFIX + DBConnectionPool.NUM_CONN_PROP_SUFFIX, "2");
    if (cliPasswdSpecified) {
        System.err.println(br.getString(BrokerResources.W_PASSWD_OPTION_DEPRECATED, OPT_PW));
        System.err.println("");
    }
    // 1st check existence of broker instance because Globals.init()
    // method will create an instance if it does not exist!
    String configName = props.getProperty(Globals.IMQ + ".instancename");
    if (configName != null && configName.length() > 0) {
        Globals.pathinit(null);
        String topname = Globals.getJMQ_INSTANCES_HOME() + File.separator + configName;
        if (!(new File(topname)).exists()) {
            System.err.println(br.getString(BrokerResources.E_INSTANCE_NOT_EXIST, configName));
            System.exit(1);
        }
    }
    Globals.init(props, false, false);
    config = Globals.getConfig();
    logger = Globals.getLogger();
    // Password in passfile override broker's properties object
    parsePassfile();
    // User/Password passed in from command line option take precedence.
    // Inorder to override the User/Password we need to construct the
    // correct property based on the dbVendor value; we do this after
    // calling Globals.init() so we can determine the dbVendor value.
    String dbVendor = config.getProperty(DBManager.JDBC_PROP_PREFIX + ".dbVendor");
    String vendorPropPrefix = DBManager.JDBC_PROP_PREFIX + "." + dbVendor;
    String user = props.getProperty(DBManager.FALLBACK_USER_PROP);
    if (user != null && user.length() > 0) {
        config.put(vendorPropPrefix + ".user", user);
    }
    String pwd = props.getProperty(DBManager.FALLBACK_PWD_PROP);
    if (pwd != null && pwd.length() > 0) {
        config.put(vendorPropPrefix + ".password", pwd);
    }
    String cmd = props.getProperty(CMD_NAME);
    CommDBManager mgr = null;
    if (DELETE_SHARECCTBL_CMD.equals(cmd) || RECREATE_SHARECCTBL_CMD.equals(cmd) || CREATE_SHARECCTBL_CMD.equals(cmd) || DUMP_SHARECCTBL_CMD.equals(cmd) || BACKUP_SHARECCTBL_CMD.equals(cmd) || RESTORE_SHARECCTBL_CMD.equals(cmd)) {
        if (debugSpecified) {
            System.out.println("cmd=" + cmd + ", use sharecc");
        }
        if (!Globals.useSharedConfigRecord()) {
            if (Globals.getHAEnabled()) {
                logger.logToAll(Logger.ERROR, br.getKString(BrokerResources.E_CONFIGURED_HA_MODE));
            } else {
                logger.logToAll(Logger.ERROR, br.getKString(BrokerResources.E_NOT_CONFIGURED_USE_SHARECC, Globals.NO_MASTERBROKER_PROP));
            }
            exit(1);
        }
        mgr = ShareConfigChangeDBManager.getDBManager();
    } else {
        dbmgr = DBManager.getDBManager();
        mgr = dbmgr;
    }
    // print out info messages
    String brokerid = null;
    if (mgr instanceof DBManager) {
        brokerid = ((DBManager) mgr).getBrokerID();
    }
    String url;
    if (CREATE_ALL_CMD.equals(cmd)) {
        url = mgr.getCreateDBURL();
    } else {
        url = mgr.getOpenDBURL();
    }
    if (url == null) {
        url = "not specified";
    }
    user = mgr.getUser();
    if (user == null) {
        user = "not specified";
    }
    if (!(mgr instanceof ShareConfigChangeDBManager)) {
        String[] msgArgs = { String.valueOf(JDBCStore.STORE_VERSION), brokerid, url, user };
        logger.logToAll(Logger.INFO, br.getString(BrokerResources.I_JDBC_STORE_INFO, msgArgs));
    } else {
        String[] msgArgs = { "", String.valueOf(JDBCShareConfigChangeStore.SCHEMA_VERSION), Globals.getClusterID(), url, user };
        logger.logToAll(Logger.INFO, br.getKString(BrokerResources.I_SHARECC_JDBCSTORE_INFO, msgArgs));
    }
    if (debugSpecified) {
        System.out.println("cmd=" + cmd);
    }
    if (CREATE_ALL_CMD.equals(cmd)) {
        // create database as well
        doCreate(true);
    } else if (CREATE_TBL_CMD.equals(cmd)) {
        // just tables
        doCreate(false);
    } else if (CREATE_SHARECCTBL_CMD.equals(cmd)) {
        doCreate(false, ShareConfigChangeDBManager.getDBManager());
    } else if (DELETE_TBL_CMD.equals(cmd)) {
        doDelete(props.getProperty(ARG_NAME), null, null);
    } else if (DELETE_SHARECCTBL_CMD.equals(cmd)) {
        doDelete(props.getProperty(ARG_NAME), ShareConfigChangeDBManager.getDBManager(), null);
    } else if (RECREATE_TBL_CMD.equals(cmd)) {
        doRecreate();
    } else if (RECREATE_SHARECCTBL_CMD.equals(cmd)) {
        doRecreate(ShareConfigChangeDBManager.getDBManager());
    } else if (REMOVE_BKR_CMD.equals(cmd)) {
        doRemoveBkr();
    } else if (REMOVE_JMSBRIDGE_CMD.equals(cmd)) {
        doRemoveJMSBridge(props.getProperty(JMSBRIDGE_NAME_PROPERTY));
    } else if (DUMP_CMD.equals(cmd)) {
        String arg = props.getProperty(ARG_NAME);
        doDump(arg, null);
    } else if (DUMP_SHARECCTBL_CMD.equals(cmd)) {
        doDump(null, ShareConfigChangeDBManager.getDBManager());
    } else if (DROPTBL_CMD.equals(cmd)) {
        String arg = props.getProperty(ARG_NAME);
        doDropTablesByPattern(arg);
    } else if (RESET_CMD.equals(cmd)) {
        doReset();
    } else if (BACKUP_CMD.equals(cmd)) {
        doBackup();
    } else if (BACKUP_SHARECCTBL_CMD.equals(cmd)) {
        doBackupSharecc();
    } else if (RESTORE_CMD.equals(cmd)) {
        doRestore();
    } else if (RESTORE_SHARECCTBL_CMD.equals(cmd)) {
        doRestoreSharecc();
    } else if (UPGRADE_STORE_CMD.equals(cmd)) {
        doUpgrade(false);
    } else if (UPGRADE_HASTORE_CMD.equals(cmd)) {
        doUpgrade(true);
    } else if (QUERY_CMD.equals(cmd)) {
        doQuery();
    } else if (cmd == null) {
        System.out.println(br.getString(BrokerResources.E_MISSING_DBMGR_CMD));
    } else {
        System.out.println(br.getString(BrokerResources.E_INVALID_DBMGR_CMD, cmd));
    }
}
Also used : CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) ShareConfigChangeDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigChangeDBManager) CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) ShareConfigChangeDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigChangeDBManager)

Example 7 with ShareConfigChangeDBManager

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

the class DBTool method doRestoreSharecc.

/**
 * Restore the shared table for cluster config change record from a file
 */
private void doRestoreSharecc() throws BrokerException {
    Properties props = System.getProperties();
    String clusterID = Globals.getClusterID();
    String backupfile = (String) props.get(ShareConfigChangeStore.CLUSTER_SHARECC_PROP_PREFIX + ".backupfile");
    logger.logToAll(Logger.INFO, br.getKString(br.I_SHARECC_RESTORE, clusterID, backupfile));
    ShareConfigChangeDBManager mgr = ShareConfigChangeDBManager.getDBManager();
    Connection conn = null;
    Exception myex = null;
    try {
        List<ChangeRecordInfo> records = ChangeRecord.prepareRestoreRecords(backupfile);
        ShareConfigRecordDAO dao = mgr.getDAOFactory().getShareConfigRecordDAO();
        Globals.getLogger().logToAll(Logger.INFO, br.getKString(br.I_SHARECC_RESTORE_RECORDS, String.valueOf(records.size()), backupfile));
        conn = mgr.getConnection(true);
        ChangeRecordInfo resetcri = ChangeRecord.makeResetRecord(true);
        try {
            mgr.lockTables(conn, true, resetcri);
            Connection myconn = null;
            Exception myee = null;
            try {
                myconn = mgr.getConnection(false);
                Util.RetryStrategy retry = null;
                do {
                    try {
                        Iterator itr = records.iterator();
                        ChangeRecordInfo cri = null;
                        while (itr.hasNext()) {
                            cri = (ChangeRecordInfo) itr.next();
                            if (cri.getType() == ChangeRecordInfo.TYPE_RESET_PERSISTENCE) {
                                itr.remove();
                                continue;
                            }
                            cri.setResetUUID(resetcri.getUUID());
                            cri.setTimestamp(System.currentTimeMillis());
                            dao.insert(myconn, cri);
                        }
                        myconn.commit();
                        break;
                    } catch (Exception e) {
                        if (retry == null) {
                            retry = new Util.RetryStrategy(mgr);
                        }
                        retry.assertShouldRetry(e);
                    }
                } while (true);
            } catch (BrokerException e) {
                myee = e;
                throw e;
            } finally {
                Util.close(null, null, myconn, myee, mgr);
            }
        } finally {
            try {
                mgr.lockTables(conn, false);
            } catch (Exception e) {
                logger.log(Logger.WARNING, br.getKString(br.X_SHARECC_UNLOCK_TABLE, e.toString()), e);
            }
        }
        logger.logToAll(Logger.INFO, br.getKString(br.I_SHARECC_RESTORE_RECORDS_SUCCESS, Globals.getClusterID(), backupfile));
    } catch (Exception e) {
        myex = e;
        String emsg = br.getKString(br.E_SHARECC_RESTORE_RECORDS_FAIL, e.getMessage());
        Globals.getLogger().logToAll(Logger.ERROR, emsg, e);
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        }
        throw new BrokerException(emsg, e);
    } finally {
        Util.close(null, null, conn, myex, mgr);
    }
}
Also used : StringUtil(com.sun.messaging.jmq.util.StringUtil) ShareConfigRecordDAO(com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigRecordDAO) ShareConfigChangeDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigChangeDBManager) ChangeRecordInfo(com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)

Example 8 with ShareConfigChangeDBManager

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

the class DBTool method createTables.

// if tableDAOs != null, only create these tables
// else create current version of persist store
static void createTables(Connection conn, boolean continueOnError, ArrayList tableDAOs, CommDBManager mgrArg) throws BrokerException {
    CommDBManager mgr = mgrArg;
    if (mgr == null) {
        mgr = DBManager.getDBManager();
    }
    Iterator itr = null;
    if (tableDAOs != null) {
        itr = tableDAOs.iterator();
    } else {
        itr = mgr.allDAOIterator();
    }
    while (itr.hasNext()) {
        BaseDAO dao = (BaseDAO) itr.next();
        try {
            Util.RetryStrategy retry = null;
            do {
                try {
                    dao.createTable(conn);
                    // table created so break from retry loop
                    break;
                } catch (Exception e) {
                    // Exception will be log & re-throw if operation cannot be retry
                    if (retry == null) {
                        retry = new Util.RetryStrategy(mgr);
                    }
                    retry.assertShouldRetry(e);
                }
            } while (true);
        } catch (BrokerException be) {
            if (Globals.getHAEnabled() || (mgr instanceof ShareConfigChangeDBManager)) {
                // or more brokers try to create the tables at the same time
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }
            }
            // Verify if the store has already been created
            if (mgr.checkStoreExists(conn) > 0) {
                if (tableDAOs == null) {
                    Globals.getLogger().log(Logger.WARNING, BrokerResources.E_CREATE_DATABASE_TABLE_FAILED, Globals.getBrokerResources().getString(BrokerResources.E_DATABASE_TABLE_ALREADY_CREATED));
                    continueOnError = true;
                } else {
                    Globals.getLogger().log(Logger.WARNING, BrokerResources.E_CREATE_DATABASE_TABLE_FAILED, Globals.getBrokerResources().getString(BrokerResources.E_THE_DATABASE_TABLE_ALREADY_CREATED, dao.getTableName()));
                }
                break;
            } else if (continueOnError) {
                // Just log msg and continue
                Globals.getLogger().log(Logger.WARNING, be.toString(), be.getCause());
            } else {
                throw be;
            }
        }
    }
    if (tableDAOs != null || !(mgr instanceof DBManager)) {
        return;
    }
    DAOFactory daoFactory = ((DBManager) mgr).getDAOFactory();
    // Insert version info in the version table
    VersionDAO versionDAO = daoFactory.getVersionDAO();
    try {
        if (continueOnError) {
            // Do this only if version is missing from version table
            int storeVersion = versionDAO.getStoreVersion(conn);
            if (storeVersion != JDBCStore.STORE_VERSION) {
                versionDAO.insert(conn, JDBCStore.STORE_VERSION);
            }
        } else {
            versionDAO.insert(conn, JDBCStore.STORE_VERSION);
        }
    } catch (BrokerException be) {
        if (Globals.getHAEnabled()) {
            // Re-check if version info has been added by another broker
            int storeVersion = versionDAO.getStoreVersion(conn);
            if (storeVersion != JDBCStore.STORE_VERSION) {
                // Re-throw the exception
                throw be;
            }
        } else {
            // Re-throw the exception
            throw be;
        }
    }
    // Insert a unique store session for the stand-alone broker
    if (!Globals.getHAEnabled()) {
        // Do this only if store session is missing from session table
        String brokerID = Globals.getBrokerID();
        StoreSessionDAO sessionDAO = daoFactory.getStoreSessionDAO();
        if (sessionDAO.getStoreSession(conn, brokerID) <= 0) {
            sessionDAO.insert(conn, brokerID, new UID().longValue(), true);
        }
    }
    PropertyDAO dao = daoFactory.getPropertyDAO();
    dao.update(conn, STORE_PROPERTY_SUPPORT_JMSBRIDGE, Boolean.TRUE);
    if (JDBCStore.ENABLE_STORED_PROC) {
        createStoredProcs(conn);
    }
}
Also used : BaseDAO(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.BaseDAO) StringUtil(com.sun.messaging.jmq.util.StringUtil) CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) DestinationUID(com.sun.messaging.jmq.jmsserver.core.DestinationUID) UID(com.sun.messaging.jmq.util.UID) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) ShareConfigChangeDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigChangeDBManager) CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) ShareConfigChangeDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigChangeDBManager)

Example 9 with ShareConfigChangeDBManager

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

the class DBTool method doCreate.

private void doCreate(boolean createdb, CommDBManager mgrArg) throws BrokerException {
    CommDBManager mgr = (mgrArg == null ? dbmgr : mgrArg);
    Connection conn = null;
    try {
        if (createdb) {
            conn = mgr.connectToCreate();
            conn.setAutoCommit(true);
        } else {
            // set autoCommit to true
            conn = mgr.getNewConnection(true);
        }
        // Check if store exist
        boolean continueOnError = false;
        int status = mgr.checkStoreExists(conn);
        if (status > 0) {
            if (!(mgr instanceof ShareConfigChangeDBManager)) {
                // All tables have already been created
                throw new BrokerException(br.getKString(BrokerResources.E_DATABASE_TABLE_ALREADY_CREATED));
            } else {
                throw new BrokerException(br.getKString(BrokerResources.E_SHARECC_TABLE_ALREADY_CREATED, Globals.getClusterID()));
            }
        } else if (status < 0) {
            // Some tables are missings so try to create the tables
            // but ignore error if table already exists
            continueOnError = true;
        }
        createTables(conn, continueOnError, mgr);
        if (standalone) {
            if (Globals.getHAEnabled()) {
                System.out.println(br.getString(BrokerResources.I_DATABASE_TABLE_HA_CREATED, Globals.getClusterID()));
            } else if (mgr instanceof ShareConfigChangeDBManager) {
                System.out.println(br.getString(BrokerResources.I_SHARECC_DATABASE_TABLE_CREATED, Globals.getClusterID()));
            } else {
                System.out.println(br.getString(BrokerResources.I_DATABASE_TABLE_CREATED));
            }
        }
    } catch (Throwable t) {
        String url;
        if (createdb) {
            url = mgr.getCreateDBURL();
        } else {
            url = mgr.getOpenDBURL();
        }
        throw new BrokerException(br.getKString(BrokerResources.E_CREATE_DATABASE_TABLE_FAILED, url), t);
    } finally {
        if (createdb && conn != null) {
            // Since the connection is not from the pool; we've to close it
            try {
                conn.close();
            } catch (SQLException e) {
                throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.E_INTERNAL_BROKER_ERROR, "Unable to close JDBC resources", e));
            }
        }
    }
}
Also used : CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) ShareConfigChangeDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigChangeDBManager)

Aggregations

ShareConfigChangeDBManager (com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigChangeDBManager)9 CommDBManager (com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager)7 StringUtil (com.sun.messaging.jmq.util.StringUtil)4 ChangeRecordInfo (com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)2 BaseDAO (com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.BaseDAO)2 ShareConfigRecordDAO (com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigRecordDAO)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