Search in sources :

Example 6 with Timestamp

use of java.sql.Timestamp 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 7 with Timestamp

use of java.sql.Timestamp 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 8 with Timestamp

use of java.sql.Timestamp 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 9 with Timestamp

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

the class JobEventRdbStorage method insertJobExecutionEventWhenSuccess.

private boolean insertJobExecutionEventWhenSuccess(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`, `complete_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.setTimestamp(10, new Timestamp(jobExecutionEvent.getCompleteTime().getTime()));
        preparedStatement.execute();
        result = true;
    } catch (final SQLException ex) {
        if (isDuplicateRecord(ex)) {
            return updateJobExecutionEventWhenSuccess(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 10 with Timestamp

use of java.sql.Timestamp in project sharding-jdbc by dangdangdotcom.

the class ResultSetUtilTest method assertConvertDateValueSuccess.

@Test
public void assertConvertDateValueSuccess() {
    Date now = new Date();
    assertThat((Date) ResultSetUtil.convertValue(now, Date.class), is(now));
    assertThat((java.sql.Date) ResultSetUtil.convertValue(now, java.sql.Date.class), is(now));
    assertThat((Time) ResultSetUtil.convertValue(now, Time.class), is(now));
    assertThat((Timestamp) ResultSetUtil.convertValue(now, Timestamp.class), is(new Timestamp(now.getTime())));
}
Also used : Timestamp(java.sql.Timestamp) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Timestamp (java.sql.Timestamp)3153 Test (org.junit.Test)526 Date (java.util.Date)458 PreparedStatement (java.sql.PreparedStatement)450 SQLException (java.sql.SQLException)367 ResultSet (java.sql.ResultSet)353 BigDecimal (java.math.BigDecimal)351 ArrayList (java.util.ArrayList)236 Date (java.sql.Date)218 Connection (java.sql.Connection)216 HashMap (java.util.HashMap)201 GenericValue (org.apache.ofbiz.entity.GenericValue)194 Calendar (java.util.Calendar)184 Time (java.sql.Time)173 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)167 Delegator (org.apache.ofbiz.entity.Delegator)157 SimpleDateFormat (java.text.SimpleDateFormat)150 IOException (java.io.IOException)129 Locale (java.util.Locale)129 Map (java.util.Map)111