Search in sources :

Example 1 with ConnectionException

use of com.iplanet.log.ConnectionException in project OpenAM by OpenRock.

the class DBHandler method connectToDatabase.

private void connectToDatabase(String userName, String password) throws ConnectionException, DriverLoadException {
    //Monit start
    if (MonitoringUtil.isRunning()) {
        if (dbLogHandlerForMonitoring == null) {
            logServiceImplForMonitoring = Agent.getLoggingSvcMBean();
            dbLogHandlerForMonitoring = logServiceImplForMonitoring.getHandler(SsoServerLoggingSvcImpl.DB_HANDLER_NAME);
        }
        if (dbLogHandlerForMonitoring != null) {
            dbLogHandlerForMonitoring.incHandlerConnectionRequests(1);
        }
    }
    //Monit end
    try {
        Class.forName(driver);
        this.conn = DriverManager.getConnection(databaseURL, userName, password);
    } catch (ClassNotFoundException e) {
        Debug.error(tableName + ":DBHandler: ClassNotFoundException " + e.getMessage());
        //Monit start
        if (MonitoringUtil.isRunning() && dbLogHandlerForMonitoring != null) {
            dbLogHandlerForMonitoring.incHandlerConnectionsFailed(1);
        }
        //Monit end
        throw new DriverLoadException(e.getMessage());
    } catch (SQLException sqle) {
        //
        //  if start up with Oracle DB down, can get sqle.getErrorCode()
        //  == 1034, "ORA-01034: ORACLE not available"
        //  MySQL returns error code 0, message:
        //  "unable to connect to any hosts due to
        //  exception: java.net.ConnectException: Connection refused
        //
        Debug.error(tableName + ":DBHandler: ConnectionException (" + sqle.getErrorCode() + "): " + sqle.getMessage());
        //Monit start
        if (MonitoringUtil.isRunning() && dbLogHandlerForMonitoring != null) {
            dbLogHandlerForMonitoring.incHandlerConnectionsFailed(1);
        }
        //Monit end
        throw new ConnectionException(sqle.getMessage());
    }
    //Monit start
    if (MonitoringUtil.isRunning() && dbLogHandlerForMonitoring != null) {
        dbLogHandlerForMonitoring.incHandlerConnectionsMade(1);
    }
//Monit end
}
Also used : SQLException(java.sql.SQLException) DriverLoadException(com.iplanet.log.DriverLoadException) ConnectionException(com.iplanet.log.ConnectionException)

Example 2 with ConnectionException

use of com.iplanet.log.ConnectionException in project OpenAM by OpenRock.

the class DBHandler method reconnectToDatabase.

//
//  detected that connection to the DB had failed previously;
//  this routine reestablishes the connection, and checks that
//  the table exists (creating it if it doesn't).
//
private void reconnectToDatabase() throws ConnectionException, DriverLoadException {
    //Monit start
    if (MonitoringUtil.isRunning() && dbLogHandlerForMonitoring != null) {
        dbLogHandlerForMonitoring.incHandlerConnectionRequests(1);
    }
    try {
        Class.forName(driver);
        this.conn = DriverManager.getConnection(databaseURL, userName, password);
    } catch (ClassNotFoundException e) {
        //Monit start
        if (MonitoringUtil.isRunning() && dbLogHandlerForMonitoring != null) {
            dbLogHandlerForMonitoring.incHandlerConnectionsFailed(1);
        }
        //Monit end
        throw new DriverLoadException(e.getMessage());
    } catch (SQLException sqle) {
        Debug.error(tableName + ":DBHandler:reconnect (" + sqle.getErrorCode() + "): " + sqle.getMessage());
        //Monit start
        if (MonitoringUtil.isRunning() && dbLogHandlerForMonitoring != null) {
            dbLogHandlerForMonitoring.incHandlerConnectionsFailed(1);
        }
        //Monit end
        throw new ConnectionException(sqle.getMessage());
    }
    //Monit start
    if (MonitoringUtil.isRunning() && dbLogHandlerForMonitoring != null) {
        dbLogHandlerForMonitoring.incHandlerConnectionsMade(1);
    }
//Monit end
}
Also used : SQLException(java.sql.SQLException) DriverLoadException(com.iplanet.log.DriverLoadException) ConnectionException(com.iplanet.log.ConnectionException)

Example 3 with ConnectionException

use of com.iplanet.log.ConnectionException in project OpenAM by OpenRock.

the class DBHandler method logRecords.

private void logRecords(LinkedList<LogRecord> records) {
    //
    if ((conn == null) || connectionToDBLost) {
        //
        try {
            reconnectToDatabase();
            Debug.error(tableName + ":DBHandler:logRecords:reconnectToDatabase successful.");
        } catch (DriverLoadException dle) {
            Debug.error(tableName + ":DBHandler:logRecords:reconnectToDatabase:DLE: " + dle.getMessage());
            //
            //  if the max mem buffer is exceeded, dump the records
            //
            clearBuffer(records);
            throw new AMLogException(AMLogException.LOG_DB_DRIVER + "'" + driver + "'");
        } catch (ConnectionException ce) {
            Debug.error(tableName + ":DBHandler:logRecords:reconnectToDatabase:CE: " + ce.getMessage());
            //
            //  if the max mem buffer is exceeded, dump the records
            //
            clearBuffer(records);
            throw new AMLogException(AMLogException.LOG_DB_CONNECT_FAILED);
        }
        //
        //  re-established the connection to the DB.  now
        //  check on the table.
        //
        connectionToDBLost = false;
        try {
            //
            //  any exception from createTable() might mean the
            //  table's not in the DB... just record the error, and
            //  let the insert let us know if the record didn't get
            //  logged.
            //
            createTable(tableName);
        } catch (SQLException se) {
            if (Debug.messageEnabled()) {
                Debug.message(tableName + ":DBHandler:logRecords:reconnect:cTable:SQLE (" + se.getErrorCode() + "): " + se.getMessage());
            }
        } catch (UnsupportedEncodingException usee) {
            if (Debug.messageEnabled()) {
                Debug.message(tableName + ":DBHandler:logRecords:reconnect:cTable:UE: " + usee.getMessage());
            }
        }
    }
    //
    //  when using oracle, and the db is down, you get an
    //  exception on the createStatement.  unfortunately,
    //  it's a TTC message (e.g. [ORA-]17310... the getErrorCode
    //  returns 17310), a vendor-specific error code.
    //
    //  MySQL db, on the other hand seems to return from
    //  the createStatement() call "ok".  catch it on the
    //  executeUpdate(), below.
    //
    Statement testConnectionStatement = null;
    try {
        testConnectionStatement = conn.createStatement();
    } catch (SQLException se) {
        //
        //  observed that when Oracle's down, it's detected here.
        //  error code 1034.
        //
        Debug.error(tableName + ":DBHandler:logRecords:cStatement:SQLE (" + se.getErrorCode() + "): " + se.getMessage());
        //
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException ex) {
            //
            if (Debug.messageEnabled()) {
                Debug.error(tableName + ":DBHandler:logRecords:cStatement:close:SQLE (" + ex.getErrorCode() + ")" + ex.getMessage());
            }
        }
        connectionToDBLost = true;
        try {
            reconnectToDatabase();
            Debug.error(tableName + ":DBHandler:logRecords:cStatement:reconnect successful.");
        } catch (DriverLoadException dle) {
            Debug.error(tableName + ":DBHandler:logRecords:cStatement:reconnect:DLE: " + dle.getMessage());
            //
            //  if the max mem buffer is exceeded, dump the records
            //
            clearBuffer(records);
            throw new AMLogException(AMLogException.LOG_DB_DRIVER + "'" + driver + "'");
        } catch (ConnectionException ce) {
            Debug.error(tableName + ":DBHandler:logRecords:cStatement:reconnect:CE: " + ce.getMessage());
            //
            //  if the max mem buffer is exceeded, dump the records
            //
            clearBuffer(records);
            throw new AMLogException(AMLogException.LOG_DB_CONNECT_FAILED);
        }
        connectionToDBLost = false;
        //
        try {
            createTable(tableName);
        } catch (SQLException sqle) {
            if (Debug.messageEnabled()) {
                Debug.message(tableName + ":DBHandler:logRecords:cStatement:reconnect:cTable:SQLE (" + sqle.getErrorCode() + "): " + sqle.getMessage());
            }
        } catch (UnsupportedEncodingException usee) {
            if (Debug.messageEnabled()) {
                Debug.message(tableName + ":DBHandler:logRecords:cStatement:reconnect: cTable:UE: " + usee.getMessage());
            }
        }
        try {
            testConnectionStatement = conn.createStatement();
        } catch (SQLException sqle) {
            //
            //  second time this failed (note that this whole block
            //  started with the createStatement()).
            //  log the error message, and continue on (for now)
            //
            Debug.error(tableName + ":DBHandler:logRecords:cStatement:reconnect:cSt:SQLE (" + sqle.getErrorCode() + "): " + sqle.getMessage());
            throw new AMLogException(AMLogException.LOG_DB_CSTATEMENT);
        }
    } finally {
        closeStatement(testConnectionStatement);
    }
    PreparedStatement insertStatement = null;
    for (LogRecord record : records) {
        List<String> values = getValues(record);
        try {
            insertStatement = getInsertPreparedStatement(values);
            insertStatement.executeUpdate();
            //Monit start
            if (MonitoringUtil.isRunning() && dbLogHandlerForMonitoring != null) {
                dbLogHandlerForMonitoring.incHandlerSuccessCount(1);
            }
        //Monit end
        } catch (SQLException sqle) {
            // Attempt to close this just in case it is holding resources.
            closeStatement(insertStatement);
            insertStatement = null;
            /*
                 *  as mentioned above, connection errors to oracle
                 *  seem to get caught in the createStatement(), while
                 *  with mysql, they get caught here.
                 *
                 *  the other thing that could happen is the table was
                 *  dropped, but not the connection.
                 */
            int sqleErrCode = sqle.getErrorCode();
            boolean tableDoesNotExist = false;
            if (Debug.messageEnabled()) {
                Debug.message(tableName + ":DBHandler:logRecords:SQLException (" + sqleErrCode + "): " + sqle.getMessage());
            }
            /*
                 *  unfortunately have to check which db and specific
                 *  error codes...
                 *  see if table's missing
                 *  MySQL: 1146
                 *  Oracle: 942
                 */
            if ((isMySQL && (sqleErrCode == 1146)) || (!isMySQL && (sqleErrCode == 942))) {
                /*
                     *  connection to DB's there, but table's missing
                     *
                     *  gotta make the table; try the executeUpdate()
                     *  again
                     */
                try {
                    createTable(tableName);
                } catch (SQLException se) {
                    //  just log the message and continue, for now
                    Debug.error(tableName + ":DBHandler:logRecords:execUpdate:cTable:SQLE (" + se.getErrorCode() + "): " + se.getMessage());
                } catch (UnsupportedEncodingException usee) {
                    //  just log the message and continue, for now
                    Debug.error(tableName + ":DBHandler:logRecords:execUpdate:cTable:UE: " + usee.getMessage());
                }
                try {
                    insertStatement = getInsertPreparedStatement(values);
                    insertStatement.executeUpdate();
                } catch (SQLException sqle2) {
                    //  guess NOW it's an error
                    Debug.error(tableName + ":DBHandler:flush:logRecords:exUpdate:SQLE (" + sqle2.getErrorCode() + "): " + sqle2.getMessage());
                    throw new AMLogException(AMLogException.LOG_DB_EXECUPDATE);
                } finally {
                    closeStatement(insertStatement);
                    insertStatement = null;
                }
            } else if ((isMySQL && (sqleErrCode == 0)) || (!isMySQL && ((sqleErrCode == 17002) || (sqleErrCode == 17410)))) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    //  log and continue
                    if (Debug.messageEnabled()) {
                        Debug.message(tableName + ":DBHandler:logRecords:execUpdate:close:SQLE (" + ex.getErrorCode() + "): " + ex.getMessage());
                    }
                }
                connectionToDBLost = true;
                try {
                    reconnectToDatabase();
                    Debug.error(tableName + ":DBHandler:logRecords:execUpdate:reconnect successful.");
                } catch (DriverLoadException dle) {
                    if (Debug.messageEnabled()) {
                        Debug.message(tableName + ":DBHandler:logRecords:execUpdate:reconnect:DLE: " + dle.getMessage());
                    }
                    /*
                         * if the max mem buffer is exceeded,
                         * dump the records
                         */
                    clearBuffer(records);
                    throw new AMLogException(AMLogException.LOG_DB_RECONNECT_FAILED);
                } catch (ConnectionException ce) {
                    if (Debug.messageEnabled()) {
                        Debug.message(tableName + ":DBHandler:logRecords:execUpdate:reconnect:CE: " + ce.getMessage());
                    }
                    /*
                         * if the max mem buffer is exceeded,
                         * dump the records
                         */
                    clearBuffer(records);
                    throw new AMLogException(AMLogException.LOG_DB_RECONNECT_FAILED);
                } finally {
                    closeStatement(insertStatement);
                    insertStatement = null;
                }
                connectionToDBLost = false;
                /*
                     *  bunch the createTable, createStatement, and
                     *  executeUpdate together because if any of these
                     *  fail, throw an exception.
                     */
                try {
                    createTable(tableName);
                    insertStatement = getInsertPreparedStatement(values);
                    insertStatement.executeUpdate();
                    //Monit start
                    if (MonitoringUtil.isRunning() && dbLogHandlerForMonitoring != null) {
                        dbLogHandlerForMonitoring.incHandlerSuccessCount(1);
                    }
                //Monit end
                } catch (SQLException sqe) {
                    Debug.error(tableName + ":DBHandler:logRecords:executeUpd:reconnect:stmt:SQE: (" + sqe.getErrorCode() + "): " + sqe.getMessage());
                    /*
                         *  if the max mem buffer is exceeded,
                         *  dump the records
                         */
                    clearBuffer(records);
                    throw new AMLogException(AMLogException.LOG_DB_EXECUPDATE);
                } catch (UnsupportedEncodingException usee) {
                    Debug.error(tableName + ":DBHandler:logRecords:execUpd:reconnect:stmt:UE: " + usee.getMessage());
                    /*
                         *  if the max mem buffer is exceeded,
                         *  dump the records
                         */
                    clearBuffer(records);
                    throw new AMLogException(AMLogException.LOG_DB_EXECUPDATE);
                } finally {
                    closeStatement(insertStatement);
                    insertStatement = null;
                }
            } else {
                /*
                     *  not sure what to do here yet.  log the error,
                     *  throw an exception, and see what happens next.
                     *
                     *  just for informational purposes, you get the
                     *  following if the columns don't exist:
                     *    if ((isMySQL && (sqleErrCode == 1054)) ||
                     *        (!isMySQL && ((sqleErrCode == 904) ||
                     *              (sqleErrCode == 913))))
                     */
                Debug.error(tableName + ":DBHandler:flush:executeUpdate failed (" + sqleErrCode + "): " + sqle.getMessage());
                // if the max mem buffer is exceeded, dump the
                // records
                clearBuffer(records);
                throw new AMLogException(AMLogException.LOG_DB_EXECUPDATE);
            }
        } finally {
            closeStatement(insertStatement);
            insertStatement = null;
        }
    }
}
Also used : SQLException(java.sql.SQLException) LogRecord(java.util.logging.LogRecord) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AMLogException(com.sun.identity.log.AMLogException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PreparedStatement(java.sql.PreparedStatement) DriverLoadException(com.iplanet.log.DriverLoadException) ConnectionException(com.iplanet.log.ConnectionException)

Aggregations

ConnectionException (com.iplanet.log.ConnectionException)3 DriverLoadException (com.iplanet.log.DriverLoadException)3 SQLException (java.sql.SQLException)3 AMLogException (com.sun.identity.log.AMLogException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 LogRecord (java.util.logging.LogRecord)1