Search in sources :

Example 11 with SQLException

use of java.sql.SQLException in project elastic-job by dangdangdotcom.

the class JobEventRdbSearch method getJobExecutionEvents.

private List<JobExecutionEvent> getJobExecutionEvents(final Condition condition) {
    List<JobExecutionEvent> result = new LinkedList<>();
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = createDataPreparedStatement(conn, TABLE_JOB_EXECUTION_LOG, FIELDS_JOB_EXECUTION_LOG, condition);
        ResultSet resultSet = preparedStatement.executeQuery()) {
        while (resultSet.next()) {
            JobExecutionEvent jobExecutionEvent = new JobExecutionEvent(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4), resultSet.getString(5), JobExecutionEvent.ExecutionSource.valueOf(resultSet.getString(6)), Integer.valueOf(resultSet.getString(7)), new Date(resultSet.getTimestamp(8).getTime()), resultSet.getTimestamp(9) == null ? null : new Date(resultSet.getTimestamp(9).getTime()), resultSet.getBoolean(10), new JobExecutionEventThrowable(null, resultSet.getString(11)));
            result.add(jobExecutionEvent);
        }
    } catch (final SQLException ex) {
        // TODO 记录失败直接输出日志,未来可考虑配置化
        log.error("Fetch JobExecutionEvent from DB error:", ex);
    }
    return result;
}
Also used : JobExecutionEvent(com.dangdang.ddframe.job.event.type.JobExecutionEvent) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JobExecutionEventThrowable(com.dangdang.ddframe.job.event.type.JobExecutionEventThrowable) LinkedList(java.util.LinkedList) Date(java.util.Date)

Example 12 with SQLException

use of java.sql.SQLException in project elastic-job by dangdangdotcom.

the class JobEventRdbStorage method insertJobExecutionEvent.

private boolean insertJobExecutionEvent(final JobExecutionEvent jobExecutionEvent) {
    boolean result = false;
    String sql = "INSERT INTO `" + TABLE_JOB_EXECUTION_LOG + "` (`id`, `job_name`, `task_id`, `hostname`, `ip`, `sharding_item`, `execution_source`, `is_success`, `start_time`) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
        preparedStatement.setString(1, jobExecutionEvent.getId());
        preparedStatement.setString(2, jobExecutionEvent.getJobName());
        preparedStatement.setString(3, jobExecutionEvent.getTaskId());
        preparedStatement.setString(4, jobExecutionEvent.getHostname());
        preparedStatement.setString(5, jobExecutionEvent.getIp());
        preparedStatement.setInt(6, jobExecutionEvent.getShardingItem());
        preparedStatement.setString(7, jobExecutionEvent.getSource().toString());
        preparedStatement.setBoolean(8, jobExecutionEvent.isSuccess());
        preparedStatement.setTimestamp(9, new Timestamp(jobExecutionEvent.getStartTime().getTime()));
        preparedStatement.execute();
        result = true;
    } catch (final SQLException ex) {
        if (!isDuplicateRecord(ex)) {
            // TODO 记录失败直接输出日志,未来可考虑配置化
            log.error(ex.getMessage());
        }
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 13 with SQLException

use of java.sql.SQLException in project elastic-job by dangdangdotcom.

the class JobEventRdbStorage method insertJobExecutionEventWhenFailure.

private boolean insertJobExecutionEventWhenFailure(final JobExecutionEvent jobExecutionEvent) {
    boolean result = false;
    String sql = "INSERT INTO `" + TABLE_JOB_EXECUTION_LOG + "` (`id`, `job_name`, `task_id`, `hostname`, `ip`, `sharding_item`, `execution_source`, `failure_cause`, `is_success`, `start_time`) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
        preparedStatement.setString(1, jobExecutionEvent.getId());
        preparedStatement.setString(2, jobExecutionEvent.getJobName());
        preparedStatement.setString(3, jobExecutionEvent.getTaskId());
        preparedStatement.setString(4, jobExecutionEvent.getHostname());
        preparedStatement.setString(5, jobExecutionEvent.getIp());
        preparedStatement.setInt(6, jobExecutionEvent.getShardingItem());
        preparedStatement.setString(7, jobExecutionEvent.getSource().toString());
        preparedStatement.setString(8, truncateString(jobExecutionEvent.getFailureCause()));
        preparedStatement.setBoolean(9, jobExecutionEvent.isSuccess());
        preparedStatement.setTimestamp(10, new Timestamp(jobExecutionEvent.getStartTime().getTime()));
        preparedStatement.execute();
        result = true;
    } catch (final SQLException ex) {
        if (isDuplicateRecord(ex)) {
            return updateJobExecutionEventFailure(jobExecutionEvent);
        }
        // TODO 记录失败直接输出日志,未来可考虑配置化
        log.error(ex.getMessage());
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 14 with SQLException

use of java.sql.SQLException in project elastic-job by dangdangdotcom.

the class JobEventRdbStorage method updateJobExecutionEventWhenSuccess.

private boolean updateJobExecutionEventWhenSuccess(final JobExecutionEvent jobExecutionEvent) {
    boolean result = false;
    String sql = "UPDATE `" + TABLE_JOB_EXECUTION_LOG + "` SET `is_success` = ?, `complete_time` = ? WHERE id = ?";
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
        preparedStatement.setBoolean(1, jobExecutionEvent.isSuccess());
        preparedStatement.setTimestamp(2, new Timestamp(jobExecutionEvent.getCompleteTime().getTime()));
        preparedStatement.setString(3, jobExecutionEvent.getId());
        if (0 == preparedStatement.executeUpdate()) {
            return insertJobExecutionEventWhenSuccess(jobExecutionEvent);
        }
        result = true;
    } catch (final SQLException ex) {
        // TODO 记录失败直接输出日志,未来可考虑配置化
        log.error(ex.getMessage());
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 15 with SQLException

use of java.sql.SQLException in project elastic-job by dangdangdotcom.

the class JobEventRdbStorage method updateJobExecutionEventFailure.

private boolean updateJobExecutionEventFailure(final JobExecutionEvent jobExecutionEvent) {
    boolean result = false;
    String sql = "UPDATE `" + TABLE_JOB_EXECUTION_LOG + "` SET `is_success` = ?, `failure_cause` = ? WHERE id = ?";
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
        preparedStatement.setBoolean(1, jobExecutionEvent.isSuccess());
        preparedStatement.setString(2, truncateString(jobExecutionEvent.getFailureCause()));
        preparedStatement.setString(3, jobExecutionEvent.getId());
        if (0 == preparedStatement.executeUpdate()) {
            return insertJobExecutionEventWhenFailure(jobExecutionEvent);
        }
        result = true;
    } catch (final SQLException ex) {
        // TODO 记录失败直接输出日志,未来可考虑配置化
        log.error(ex.getMessage());
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

SQLException (java.sql.SQLException)17708 PreparedStatement (java.sql.PreparedStatement)7646 ResultSet (java.sql.ResultSet)6472 Connection (java.sql.Connection)6226 Statement (java.sql.Statement)3089 ArrayList (java.util.ArrayList)2495 Test (org.junit.Test)1525 IOException (java.io.IOException)1220 List (java.util.List)760 HashMap (java.util.HashMap)633 CallableStatement (java.sql.CallableStatement)506 Map (java.util.Map)471 Properties (java.util.Properties)426 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)387 Timestamp (java.sql.Timestamp)363 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)328 DatabaseMetaData (java.sql.DatabaseMetaData)326 ResultSetMetaData (java.sql.ResultSetMetaData)301 DataSource (javax.sql.DataSource)295 File (java.io.File)280