Search in sources :

Example 36 with DatabaseAccessException

use of com.axway.ats.log.autodb.exceptions.DatabaseAccessException in project ats-framework by Axway.

the class DbReadAccess method getMessages.

public List<Message> getMessages(int startRecord, int recordsCount, String whereClause, String sortColumn, boolean ascending) throws DatabaseAccessException {
    List<Message> messages = new ArrayList<Message>();
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd");
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss:S");
    String sqlLog = new SqlRequestFormatter().add("start record", startRecord).add("records", recordsCount).add("where", whereClause).add("sort by", sortColumn).add("asc", ascending).format();
    Connection connection = getConnection();
    CallableStatement callableStatement = null;
    ResultSet rs = null;
    try {
        callableStatement = connection.prepareCall("{ call sp_get_messages(?, ?, ?, ?, ?) }");
        callableStatement.setString(1, String.valueOf(startRecord));
        callableStatement.setString(2, String.valueOf(recordsCount));
        callableStatement.setString(3, whereClause);
        callableStatement.setString(4, sortColumn);
        callableStatement.setString(5, (ascending ? "ASC" : "DESC"));
        int numberRecords = 0;
        rs = callableStatement.executeQuery();
        // <parentMessageId, Message>
        Map<Integer, Message> splitMessages = new HashMap<Integer, Message>();
        while (rs.next()) {
            Message message = new Message();
            message.messageId = rs.getInt("messageId");
            message.messageContent = rs.getString("message");
            message.messageType = rs.getString("typeName");
            Timestamp timestamp = rs.getTimestamp("timestamp");
            message.date = dateFormat.format(timestamp);
            message.time = timeFormat.format(timestamp);
            message.machineName = rs.getString("machineName");
            message.threadName = rs.getString("threadName");
            message.parentMessageId = rs.getInt("parentMessageId");
            if (message.parentMessageId != 0) {
                // split message
                if (splitMessages.containsKey(message.parentMessageId)) {
                    // append to the message - result set is ordered by message ID
                    Message splitMessage = splitMessages.get(message.parentMessageId);
                    if (splitMessage.messageId < message.messageId) {
                        // append at the end
                        splitMessage.messageContent = splitMessage.messageContent + message.messageContent;
                    } else {
                        // append at the beginning
                        splitMessage.messageContent = message.messageContent + splitMessage.messageContent;
                    }
                } else {
                    // first part of the split message
                    splitMessages.put(message.parentMessageId, message);
                    messages.add(message);
                }
            } else {
                // single message
                messages.add(message);
            }
            numberRecords++;
        }
        logQuerySuccess(sqlLog, "messages", numberRecords);
    } catch (Exception e) {
        throw new DatabaseAccessException("Error when " + sqlLog, e);
    } finally {
        DbUtils.closeResultSet(rs);
        DbUtils.close(connection, callableStatement);
    }
    return messages;
}
Also used : Message(com.axway.ats.log.autodb.entities.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) DbConnection(com.axway.ats.core.dbaccess.DbConnection) Timestamp(java.sql.Timestamp) Checkpoint(com.axway.ats.log.autodb.entities.Checkpoint) SQLException(java.sql.SQLException) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) SimpleDateFormat(java.text.SimpleDateFormat) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException)

Example 37 with DatabaseAccessException

use of com.axway.ats.log.autodb.exceptions.DatabaseAccessException in project ats-framework by Axway.

the class DbWriteAccess method endCheckpoint.

public void endCheckpoint(CheckpointInfo runningCheckpointInfo, long endTimestamp, long transferSize, int result, boolean closeConnection) throws DatabaseAccessException {
    final String errMsg = "Unable to end checkpoint with name '" + runningCheckpointInfo.getName() + "', checkpoint summary id " + runningCheckpointInfo.getCheckpointSummaryId() + ", id " + runningCheckpointInfo.getCheckpointId();
    final int indexRowsInserted = 8;
    CallableStatement callableStatement = null;
    try {
        refreshInternalConnection();
        callableStatement = connection.prepareCall("{ call sp_end_checkpoint(?, ?, ?, ?, ?, ?, ?, ?) }");
        callableStatement.setInt(1, runningCheckpointInfo.getCheckpointSummaryId());
        callableStatement.setInt(2, runningCheckpointInfo.getCheckpointId());
        callableStatement.setInt(3, (int) (endTimestamp - runningCheckpointInfo.getStartTimestamp()));
        callableStatement.setLong(4, transferSize);
        callableStatement.setInt(5, result);
        callableStatement.setInt(6, checkpointLogLevel.toInt());
        callableStatement.setTimestamp(7, new Timestamp(endTimestamp));
        callableStatement.registerOutParameter(indexRowsInserted, Types.INTEGER);
        callableStatement.execute();
        if (callableStatement.getInt(indexRowsInserted) != 1) {
            throw new DatabaseAccessException(errMsg);
        }
    } catch (Exception e) {
        throw new DatabaseAccessException(errMsg, e);
    } finally {
        if (closeConnection) {
            DbUtils.close(connection, callableStatement);
        } else {
            DbUtils.closeStatement(callableStatement);
        }
    }
}
Also used : CallableStatement(java.sql.CallableStatement) Timestamp(java.sql.Timestamp) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) SQLException(java.sql.SQLException)

Example 38 with DatabaseAccessException

use of com.axway.ats.log.autodb.exceptions.DatabaseAccessException in project ats-framework by Axway.

the class DbWriteAccess method getTestcaseIdByLabel.

public int getTestcaseIdByLabel(String label, boolean closeConnection) throws DatabaseAccessException {
    CallableStatement callableStatement = null;
    final int indexTestcaseId = 2;
    try {
        refreshInternalConnection();
        callableStatement = connection.prepareCall("{ call sp_get_testcase_id_by_label(?, ?) }");
        callableStatement.setString(1, label);
        callableStatement.registerOutParameter(2, Types.INTEGER);
        callableStatement.execute();
        return callableStatement.getInt(indexTestcaseId);
    } catch (Exception e) {
        throw new DatabaseAccessException("Error getting testcase id for testcase label " + label, e);
    } finally {
        if (closeConnection) {
            DbUtils.close(connection, callableStatement);
        } else {
            DbUtils.closeStatement(callableStatement);
        }
    }
}
Also used : CallableStatement(java.sql.CallableStatement) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) SQLException(java.sql.SQLException) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException)

Example 39 with DatabaseAccessException

use of com.axway.ats.log.autodb.exceptions.DatabaseAccessException in project ats-framework by Axway.

the class DbWriteAccess method updateMachineInfo.

public void updateMachineInfo(String machineName, String machineInfo, boolean closeConnection) throws DatabaseAccessException {
    final String errMsg = "Unable to update the info about machine with name " + machineName;
    // then start the run
    final int indexRowsInserted = 3;
    CallableStatement callableStatement = null;
    try {
        refreshInternalConnection();
        callableStatement = connection.prepareCall("{ call sp_update_machine_info(?, ?, ?) }");
        callableStatement.setString(1, machineName);
        callableStatement.setString(2, machineInfo);
        callableStatement.registerOutParameter(indexRowsInserted, Types.INTEGER);
        callableStatement.execute();
        if (callableStatement.getInt(indexRowsInserted) != 1) {
            throw new DatabaseAccessException(errMsg);
        }
    } catch (Exception e) {
        throw new DatabaseAccessException(errMsg, e);
    } finally {
        if (closeConnection) {
            DbUtils.close(connection, callableStatement);
        } else {
            DbUtils.closeStatement(callableStatement);
        }
    }
}
Also used : CallableStatement(java.sql.CallableStatement) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) SQLException(java.sql.SQLException)

Example 40 with DatabaseAccessException

use of com.axway.ats.log.autodb.exceptions.DatabaseAccessException in project ats-framework by Axway.

the class DbWriteAccess method runDbSanityCheck.

public void runDbSanityCheck() throws DatabaseAccessException {
    DatabaseAccessException dbae = null;
    final String SANITY_PRODUCT = "SanityCheck(TestProduct)";
    final String SANITY_VERSION = "SanityCheck(TestVersion)";
    final String SANITY_BUILD = "SanityCheck(TestBuild)";
    final String SANITY_RUN = "SanityCheck(TestRun)";
    final String SANITY_OS = "SanityCheck(TestOS)";
    final String SANITY_SUITE = "SanityCheck(TestSuite)";
    final String SANITY_SCENARIO = "SanityCheck(TestScenario)";
    final String SANITY_TESTCASE = "SanityCheck(Testcase)";
    final String SANITY_LOADQUEUE = "SanityCheck(TestLoadqueue)";
    final String SANITY_CHECKPOINT = "SanityCheck(TestCheckpoint)";
    final String SANITY_MESSAGE = "SanityCheck(TestMessage)";
    final String SANITY_DESCRIPTION = "sanity description";
    final String SANITY_HOSTNAME = "SanityCheck(TestHostName)";
    boolean originalAutoCommitState = false;
    try {
        this.connection = getConnection();
        // in sanity mode, the connection will be reused
        sanityRun = true;
        String javaFrameworkVersion = AtsVersion.getAtsVersion();
        System.out.println("*** ATS *** ATS framework version is '" + javaFrameworkVersion + "'");
        System.out.println("*** ATS *** Checking for ATS log database connection with the following parameters: " + connection.toString());
        String databaseVersion = getDatabaseVersion();
        System.out.println("*** ATS *** ATS Log database version is '" + databaseVersion + "'");
        if (!javaFrameworkVersion.equalsIgnoreCase(databaseVersion)) {
            System.out.println("*** ATS WARNING *** You are using ATS version " + javaFrameworkVersion + " with Log database version " + databaseVersion + ". This might cause incompatibility problems!");
        }
        originalAutoCommitState = connection.getAutoCommit();
        // disable auto commit
        connection.setAutoCommit(false);
        long timestamp = Calendar.getInstance().getTimeInMillis();
        // start everything
        int runId = startRun(SANITY_RUN, SANITY_OS, SANITY_PRODUCT, SANITY_VERSION, SANITY_BUILD, timestamp, SANITY_HOSTNAME, false);
        int suiteId = startSuite("SANITY_PACKAGE", SANITY_SUITE, timestamp, runId, false);
        int testcaseId = startTestCase(SANITY_SUITE, SANITY_SCENARIO, SANITY_DESCRIPTION, SANITY_TESTCASE, timestamp, suiteId, false);
        // insert a test message
        insertMessage(SANITY_MESSAGE, 5, false, "machine0", "group1-thread2", timestamp, testcaseId, false);
        // insert a checkpoint
        int loadQueueId = startLoadQueue(SANITY_LOADQUEUE, 0, "127.0.0.1:8080", "AllAtOnce", 10, "localhost", timestamp, testcaseId, false);
        CheckpointInfo startedCheckpointInfo = startCheckpoint(SANITY_CHECKPOINT, "thread1", 1000, "KB", loadQueueId, false);
        endCheckpoint(startedCheckpointInfo, 2000, 100, CheckpointResult.PASSED.toInt(), false);
        int statisticId1 = populateSystemStatisticDefinition("running users", "", "", "count", "param1_1");
        int statisticId2 = populateSystemStatisticDefinition("standby users", "", "", "count", "param2_1");
        insertSystemStatistics(testcaseId, "localhost", statisticId1 + "_" + statisticId2, "30_1", System.currentTimeMillis(), false);
        endLoadQueue(LoadQueueResult.PASSED.toInt(), timestamp, loadQueueId, false);
        // end everything
        endTestCase(1, timestamp, testcaseId, false);
        endSuite(timestamp, suiteId, false);
        endRun(timestamp, runId, false);
    } catch (SQLException sqle) {
        String errorMessage = "Unable to insert sanity check sample data";
        System.err.println(DbUtils.getFullSqlException(errorMessage, sqle));
        dbae = new DatabaseAccessException(errorMessage, sqle);
    } finally {
        if (dbEventsCache != null) {
            // it is in batch mode, we want to cleanup the events cached while running the sanity check
            dbEventsCache.resetCache();
        }
        sanityRun = false;
        try {
            // rollback the connection
            if (connection != null) {
                connection.rollback();
            }
        } catch (SQLException sqle) {
            String errorMessage = "Unable to revert sanity check data";
            System.err.println(DbUtils.getFullSqlException(errorMessage, sqle));
            if (dbae == null) {
                dbae = new DatabaseAccessException(errorMessage, sqle);
            } else {
                log.error("The transaction could not be rolled back, possible cause '" + dbae.getMessage() + "'");
            }
        } finally {
            try {
                if (connection != null) {
                    connection.setAutoCommit(originalAutoCommitState);
                }
            } catch (SQLException e) {
                // do not hide the possible exception in the rollback() catch block
                System.err.println(DbUtils.getFullSqlException("Could not restore connection's autocommit state", e));
            } finally {
                DbUtils.closeConnection(connection);
            }
        }
    }
    // we check if there is thrown exception, the first thrown exception is with priority
    if (dbae != null) {
        throw dbae;
    }
}
Also used : SQLException(java.sql.SQLException) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException)

Aggregations

DatabaseAccessException (com.axway.ats.log.autodb.exceptions.DatabaseAccessException)57 SQLException (java.sql.SQLException)52 CallableStatement (java.sql.CallableStatement)45 DbConnection (com.axway.ats.core.dbaccess.DbConnection)35 Connection (java.sql.Connection)34 ResultSet (java.sql.ResultSet)28 Checkpoint (com.axway.ats.log.autodb.entities.Checkpoint)23 ArrayList (java.util.ArrayList)18 Timestamp (java.sql.Timestamp)14 PreparedStatement (java.sql.PreparedStatement)7 HashMap (java.util.HashMap)5 Statistic (com.axway.ats.log.autodb.entities.Statistic)4 Message (com.axway.ats.log.autodb.entities.Message)3 SimpleDateFormat (java.text.SimpleDateFormat)3 BackwardCompatibility (com.axway.ats.core.utils.BackwardCompatibility)2 Run (com.axway.ats.log.autodb.entities.Run)2 StatisticDescription (com.axway.ats.log.autodb.entities.StatisticDescription)2 Suite (com.axway.ats.log.autodb.entities.Suite)2 AgentException (com.axway.ats.agent.core.exceptions.AgentException)1 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)1