Search in sources :

Example 16 with CommDBManager

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

the class DBTool method doDump.

static void doDump(Connection dbconn, String[] tables, CommDBManager mgrArg) throws BrokerException, SQLException {
    CommDBManager mgr = mgrArg;
    if (tables != null && tables.length > 0) {
        Statement stmt = null;
        Exception myex = null;
        try {
            stmt = dbconn.createStatement();
            for (int i = 0; i < tables.length; i++) {
                ResultSet r = null;
                String tname = tables[i];
                String sql = "SELECT COUNT(*) FROM " + tname;
                try {
                    r = mgr.executeQueryStatement(stmt, sql);
                    if (r.next()) {
                        System.out.println(tname + ": number of row=" + r.getInt(1));
                    }
                    r.close();
                } catch (SQLException e) {
                    SQLException ex = mgr.wrapSQLException("[" + sql + "]", e);
                    logger.log(Logger.ERROR, "failed to dump tables", ex);
                }
            }
        } catch (SQLException e) {
            myex = e;
            throw e;
        } finally {
            Util.close(null, stmt, null, myex, mgr);
        }
    } else {
        // Starting in 4.0, use info from BaseDAO.getDebugInfo()
        Iterator itr = mgr.allDAOIterator();
        while (itr.hasNext()) {
            BaseDAO dao = (BaseDAO) itr.next();
            System.out.println(dao.getDebugInfo(dbconn).toString());
        }
    }
}
Also used : CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) BaseDAO(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.BaseDAO)

Example 17 with CommDBManager

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

the class DBTool method createStoredProcs.

private static void createStoredProcs(Connection conn) throws BrokerException {
    ArrayList<BaseDAO> daos = new ArrayList<>();
    CommDBManager mgr = DBManager.getDBManager();
    Iterator itr = mgr.allDAOIterator();
    while (itr.hasNext()) {
        BaseDAO dao = (BaseDAO) itr.next();
        try {
            Util.RetryStrategy retry = null;
            do {
                try {
                    dao.createStoredProc(conn);
                    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);
            daos.add(dao);
        } catch (BrokerException be) {
            try {
                dropStoredProcs(conn, daos);
            } catch (Exception e) {
                Globals.getLogger().logStack(Logger.WARNING, be.getMessage(), be);
            }
            throw be;
        }
    }
}
Also used : CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) BaseDAO(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.BaseDAO) StringUtil(com.sun.messaging.jmq.util.StringUtil)

Example 18 with CommDBManager

use of com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager 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 19 with CommDBManager

use of com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager 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)

Example 20 with CommDBManager

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

the class ShareConfigRecordDAOImpl method getLockID.

/**
 * @return "" if no reset record entry
 */
@Override
public String getLockID(Connection conn) throws BrokerException {
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Exception myex = null;
    String sql = selectLockIDSQL;
    try {
        // Get a connection
        CommDBManager mgr = getDBManager();
        if (conn == null) {
            conn = mgr.getConnection(true);
            myConn = true;
        }
        pstmt = mgr.createPreparedStatement(conn, sql);
        rs = pstmt.executeQuery();
        if (!rs.next()) {
            return "";
        }
        return rs.getString(1);
    } 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_LOCKID, 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)

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