Example usage for org.springframework.batch.core.repository.dao NoSuchObjectException NoSuchObjectException

List of usage examples for org.springframework.batch.core.repository.dao NoSuchObjectException NoSuchObjectException

Introduction

In this page you can find the example usage for org.springframework.batch.core.repository.dao NoSuchObjectException NoSuchObjectException.

Prototype

public NoSuchObjectException(String message) 

Source Link

Usage

From source file:org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.java

/**
 * Update given JobExecution using a SQL UPDATE statement. The JobExecution
 * is first checked to ensure all fields are not null, and that it has an
 * ID. The database is then queried to ensure that the ID exists, which
 * ensures that it is valid./*from   w  w w .ja v a  2 s  .  c o  m*/
 *
 * @see JobExecutionDao#updateJobExecution(JobExecution)
 */
@Override
public void updateJobExecution(JobExecution jobExecution) {

    validateJobExecution(jobExecution);

    Assert.notNull(jobExecution.getId(),
            "JobExecution ID cannot be null. JobExecution must be saved before it can be updated");

    Assert.notNull(jobExecution.getVersion(),
            "JobExecution version cannot be null. JobExecution must be saved before it can be updated");

    synchronized (jobExecution) {
        Integer version = jobExecution.getVersion() + 1;

        String exitDescription = jobExecution.getExitStatus().getExitDescription();
        if (exitDescription != null && exitDescription.length() > exitMessageLength) {
            exitDescription = exitDescription.substring(0, exitMessageLength);
            if (logger.isDebugEnabled()) {
                logger.debug("Truncating long message before update of JobExecution: " + jobExecution);
            }
        }
        Object[] parameters = new Object[] { jobExecution.getStartTime(), jobExecution.getEndTime(),
                jobExecution.getStatus().toString(), jobExecution.getExitStatus().getExitCode(),
                exitDescription, version, jobExecution.getCreateTime(), jobExecution.getLastUpdated(),
                jobExecution.getId(), jobExecution.getVersion() };

        // Check if given JobExecution's Id already exists, if none is found
        // it
        // is invalid and
        // an exception should be thrown.
        if (getJdbcTemplate().queryForObject(getQuery(CHECK_JOB_EXECUTION_EXISTS), Integer.class,
                new Object[] { jobExecution.getId() }) != 1) {
            throw new NoSuchObjectException("Invalid JobExecution, ID " + jobExecution.getId() + " not found.");
        }

        int count = getJdbcTemplate().update(getQuery(UPDATE_JOB_EXECUTION), parameters,
                new int[] { Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
                        Types.INTEGER, Types.TIMESTAMP, Types.TIMESTAMP, Types.BIGINT, Types.INTEGER });

        // Avoid concurrent modifications...
        if (count == 0) {
            int curentVersion = getJdbcTemplate().queryForObject(getQuery(CURRENT_VERSION_JOB_EXECUTION),
                    Integer.class, new Object[] { jobExecution.getId() });
            throw new OptimisticLockingFailureException(
                    "Attempt to update job execution id=" + jobExecution.getId() + " with wrong version ("
                            + jobExecution.getVersion() + "), where current version is " + curentVersion);
        }

        jobExecution.incrementVersion();
    }
}