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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations