Example usage for org.springframework.dao OptimisticLockingFailureException OptimisticLockingFailureException

List of usage examples for org.springframework.dao OptimisticLockingFailureException OptimisticLockingFailureException

Introduction

In this page you can find the example usage for org.springframework.dao OptimisticLockingFailureException OptimisticLockingFailureException.

Prototype

public OptimisticLockingFailureException(String msg) 

Source Link

Document

Constructor for OptimisticLockingFailureException.

Usage

From source file:org.cloudfoundry.identity.uaa.scim.jdbc.JdbcScimUserProvisioning.java

@Override
public ScimUser update(final String id, final ScimUser user) throws InvalidScimResourceException {
    validate(user);//w  w w  .j a  v a 2  s .c om
    logger.debug("Updating user " + user.getUserName());
    final String origin = hasText(user.getOrigin()) ? user.getOrigin() : OriginKeys.UAA;
    final String zoneId = IdentityZoneHolder.get().getId();
    int updated = jdbcTemplate.update(UPDATE_USER_SQL, new PreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps) throws SQLException {
            int pos = 1;
            Timestamp t = new Timestamp(new Date().getTime());
            ps.setInt(pos++, user.getVersion() + 1);
            ps.setTimestamp(pos++, t);
            ps.setString(pos++, user.getUserName());
            ps.setString(pos++, user.getPrimaryEmail());
            ps.setString(pos++, user.getName().getGivenName());
            ps.setString(pos++, user.getName().getFamilyName());
            ps.setBoolean(pos++, user.isActive());
            ps.setString(pos++, extractPhoneNumber(user));
            ps.setBoolean(pos++, user.isVerified());
            ps.setString(pos++, origin);
            ps.setString(pos++, hasText(user.getExternalId()) ? user.getExternalId() : null);
            ps.setString(pos++, user.getSalt());
            ps.setString(pos++, id);
            ps.setInt(pos++, user.getVersion());
            ps.setString(pos++, zoneId);
        }
    });
    ScimUser result = retrieve(id);
    if (updated == 0) {
        throw new OptimisticLockingFailureException(
                String.format("Attempt to update a user (%s) with wrong version: expected=%d but found=%d", id,
                        result.getVersion(), user.getVersion()));
    }
    if (updated > 1) {
        throw new IncorrectResultSizeDataAccessException(1);
    }
    return result;
}

From source file:org.cloudfoundry.identity.uaa.scim.jdbc.JdbcScimUserProvisioning.java

private ScimUser deactivateUser(ScimUser user, int version) {
    logger.debug("Deactivating user: " + user.getId());
    int updated;// w ww. j  a  v  a  2  s .c o m
    if (version < 0) {
        // Ignore
        updated = jdbcTemplate.update(DEACTIVATE_USER_SQL, false, user.getId(),
                IdentityZoneHolder.get().getId());
    } else {
        updated = jdbcTemplate.update(DEACTIVATE_USER_SQL + " and version=?", false, user.getId(),
                IdentityZoneHolder.get().getId(), version);
    }
    if (updated == 0) {
        throw new OptimisticLockingFailureException(
                String.format("Attempt to update a user (%s) with wrong version: expected=%d but found=%d",
                        user.getId(), user.getVersion(), version));
    }
    if (updated > 1) {
        throw new IncorrectResultSizeDataAccessException(1);
    }
    user.setActive(false);
    return user;
}

From source file:org.cloudfoundry.identity.uaa.scim.jdbc.JdbcScimUserProvisioning.java

@Override
public ScimUser verifyUser(String id, int version)
        throws ScimResourceNotFoundException, InvalidScimResourceException {
    logger.debug("Verifying user: " + id);
    int updated;/*  w w w.  j  av  a 2s  . c  om*/
    if (version < 0) {
        // Ignore
        updated = jdbcTemplate.update(VERIFY_USER_SQL, true, id, IdentityZoneHolder.get().getId());
    } else {
        updated = jdbcTemplate.update(VERIFY_USER_SQL + " and version=?", true, id,
                IdentityZoneHolder.get().getId(), version);
    }
    ScimUser user = retrieve(id);
    if (updated == 0) {
        throw new OptimisticLockingFailureException(
                String.format("Attempt to update a user (%s) with wrong version: expected=%d but found=%d",
                        user.getId(), user.getVersion(), version));
    }
    if (updated > 1) {
        throw new IncorrectResultSizeDataAccessException(1);
    }
    return user;
}

From source file:org.cloudfoundry.identity.uaa.scim.jdbc.JdbcScimUserProvisioning.java

private ScimUser deleteUser(ScimUser user, int version) {
    logger.debug("Deleting user: " + user.getId());
    int updated;//from  www  .ja  v  a 2  s. c  o  m

    if (version < 0) {
        updated = jdbcTemplate.update(DELETE_USER_SQL, user.getId(), IdentityZoneHolder.get().getId());
    } else {
        updated = jdbcTemplate.update(DELETE_USER_SQL + " and version=?", user.getId(),
                IdentityZoneHolder.get().getId(), version);
    }
    if (updated == 0) {
        throw new OptimisticLockingFailureException(
                String.format("Attempt to update a user (%s) with wrong version: expected=%d but found=%d",
                        user.getId(), user.getVersion(), version));
    }
    return user;
}

From source file:org.cloudfoundry.identity.uaa.scim.JdbcScimUserProvisioning.java

@Override
public ScimUser updateUser(final String id, final ScimUser user) throws InvalidUserException {
    validate(user);//from ww w . j  a  v a 2s .  com
    logger.info("Updating user " + user.getUserName());

    int updated = jdbcTemplate.update(UPDATE_USER_SQL, new PreparedStatementSetter() {
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setInt(1, user.getVersion() + 1);
            ps.setTimestamp(2, new Timestamp(new Date().getTime()));
            ps.setString(3, user.getPrimaryEmail());
            ps.setString(4, user.getName().getGivenName());
            ps.setString(5, user.getName().getFamilyName());
            ps.setBoolean(6, user.isActive());
            ps.setLong(7, UaaAuthority.fromUserType(user.getUserType()).value());
            ps.setString(8, extractPhoneNumber(user));
            ps.setString(9, id);
            ps.setInt(10, user.getVersion());
        }
    });
    ScimUser result = retrieveUser(id);
    if (updated == 0) {
        throw new OptimisticLockingFailureException(
                String.format("Attempt to update a user (%s) with wrong version: expected=%d but found=%d", id,
                        result.getVersion(), user.getVersion()));
    }
    if (updated > 1) {
        throw new IncorrectResultSizeDataAccessException(1);
    }
    return result;
}

From source file:org.cloudfoundry.identity.uaa.scim.JdbcScimUserProvisioning.java

@Override
public ScimUser removeUser(String id, int version) {
    logger.info("Removing user: " + id);

    ScimUser user = retrieveUser(id);/*from  ww w .  j ava 2 s  . c o  m*/
    int updated;

    if (version < 0) {
        // Ignore
        updated = jdbcTemplate.update(DELETE_USER_SQL, id);
    } else {
        updated = jdbcTemplate.update(DELETE_USER_SQL + " and version=?", id, version);
    }
    if (updated == 0) {
        throw new OptimisticLockingFailureException(
                String.format("Attempt to update a user (%s) with wrong version: expected=%d but found=%d", id,
                        user.getVersion(), version));
    }
    if (updated > 1) {
        throw new IncorrectResultSizeDataAccessException(1);
    }
    user.setActive(false);
    return user;
}

From source file:org.libreplan.web.common.ConfigurationModel.java

@Override
@Transactional//w  w w  . ja va2s.com
public void confirm() {
    checkEntitySequences();
    configurationDAO.save(configuration);
    saveConnectors();
    try {
        storeAndRemoveEntitySequences();
    } catch (IllegalStateException e) {
        throw new OptimisticLockingFailureException("concurrency problem in entity sequences");
    }
}

From source file:org.opennms.netmgt.ticketer.quickbase.QuickBaseTicketerPlugin.java

public void saveOrUpdate(Ticket ticket) {

    try {/*w  w  w  .j  a  v  a  2 s  .  c o m*/

        Properties props = getProperties();

        QuickBaseClient qdb = createClient(getUserName(props), getPassword(props), getUrl(props));

        String dbId = qdb.findDbByName(getApplicationName(props));

        HashMap<String, String> record = new HashMap<String, String>();

        record.put(getSummaryField(props), ticket.getSummary());
        record.put(getDetailsField(props), ticket.getDetails());
        record.put(getStateField(props), getQuickBaseStateValue(ticket.getState(), props));

        if (ticket.getId() == null) {
            addAdditionCreationFields(record, props);
            String recordId = qdb.addRecord(dbId, record);
            ticket.setId(recordId);
        } else {
            Ticket oldTicket = get(ticket.getId());
            if (ticket.getModificationTimestamp().equals(oldTicket.getModificationTimestamp())) {
                qdb.editRecord(dbId, record, ticket.getId());
            } else {
                throw new OptimisticLockingFailureException(
                        "Ticket has been updated while this ticket was in memory! Reload and try again!");
            }

        }

    } catch (Throwable e) {
        throw new DataRetrievalFailureException("Failed to commit QuickBase transaction: " + e.getMessage(), e);
    }

}

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.//  w  w  w  .j a va 2  s . c om
 *
 * @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();
    }
}

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

@Override
public void updateStepExecution(StepExecution stepExecution) {

    validateStepExecution(stepExecution);
    Assert.notNull(stepExecution.getId(),
            "StepExecution Id cannot be null. StepExecution must saved" + " before it can be updated.");

    // Do not check for existence of step execution considering
    // it is saved at every commit point.

    String exitDescription = truncateExitDescription(stepExecution.getExitStatus().getExitDescription());

    // Attempt to prevent concurrent modification errors by blocking here if
    // someone is already trying to do it.
    synchronized (stepExecution) {

        Integer version = stepExecution.getVersion() + 1;
        Object[] parameters = new Object[] { stepExecution.getStartTime(), stepExecution.getEndTime(),
                stepExecution.getStatus().toString(), stepExecution.getCommitCount(),
                stepExecution.getReadCount(), stepExecution.getFilterCount(), stepExecution.getWriteCount(),
                stepExecution.getExitStatus().getExitCode(), exitDescription, version,
                stepExecution.getReadSkipCount(), stepExecution.getProcessSkipCount(),
                stepExecution.getWriteSkipCount(), stepExecution.getRollbackCount(),
                stepExecution.getLastUpdated(), stepExecution.getId(), stepExecution.getVersion() };
        int count = getJdbcTemplate().update(getQuery(UPDATE_STEP_EXECUTION), parameters,
                new int[] { Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.INTEGER, Types.INTEGER,
                        Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
                        Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.TIMESTAMP,
                        Types.BIGINT, Types.INTEGER });

        // Avoid concurrent modifications...
        if (count == 0) {
            int curentVersion = getJdbcTemplate().queryForObject(getQuery(CURRENT_VERSION_STEP_EXECUTION),
                    new Object[] { stepExecution.getId() }, Integer.class);
            throw new OptimisticLockingFailureException(
                    "Attempt to update step execution id=" + stepExecution.getId() + " with wrong version ("
                            + stepExecution.getVersion() + "), where current version is " + curentVersion);
        }//from www .j  av  a 2  s .  co  m

        stepExecution.incrementVersion();

    }
}