春季-执行两个休眠更新查询,而不是一个

时间:2018-06-26 12:16:43

标签: java spring hibernate orm spring-transactions

我正在Spring和Hibernate中工作。我有一个需要在其中添加数字来更新特定字段的要求。由于多个线程可以同时执行它,因此在更新时,我用旧值检查字段值。因此,如果未进行任何更新,则意味着它被其他线程增加,我们将触发重试。

CompanyService

public Company getAndIncrementRequestId(final int companyId, int retry) throws Exception {
    Optional<Company> companyOptional = companyRepository.findById(companyId);
    if (!companyOptional.isPresent()) {
        throw new EntityNotFoundException("Company not found for given id" + companyId);
    }

    Company company = companyOptional.get();
    int oldRequestId = company.getRequestId();
    int requestId;
    if (oldRequestId == Integer.MAX_VALUE) {
        requestId = 1;
    } else {
        requestId = oldRequestId + 1;
    }
    company.setRequestId(requestId); //--------------------------> PROBLEM
    int result = companyRepository.updateRequestId(companyId, requestId, oldRequestId);
    if (result == 0) {
        if (retry < 0) {
            throw new Exception("Unable to get requestId");
        }
        LOG.warn("Retrying since there was some update on requestId by some other thread");
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            LOG.warn("Unexpected InterruptException occurred while trying to get requestId");
        }
        return getAndIncrementRequestId(companyId, retry - 1);
    }
    return company;
}

公司存储库

@Transactional
public interface CompanyRepository extends CrudRepository<Company, Integer> {
    Optional<Company> findById(String id);

    @Modifying(clearAutomatically = true)
    @Query("update Company c set c.requestId = :requestId WHERE c.id = :companyId AND c.requestId = :oldRequestId")
    int updateRequestId(@Param("companyId") Integer companyId, @Param("requestId") Integer requestId,@Param("oldRequestId") Integer oldRequestId);
}

但是上述Service中的上述代码将触发两个休眠更新,其中一个将requestId设置为最新的requestId,另一个则设置为实际更新。将show-sql设置为true后,可以在日志中观察到两个查询。

但是如果一行,

company.setRequestId(requestId);

在companyRepository.updateRequestId()正常运行后向下移动。

工作中的公司服务

public Company getAndIncrementRequestId(final int companyId, int retry) throws Exception {
    Optional<Company> companyOptional = companyRepository.findById(companyId);
    if (!companyOptional.isPresent()) {
        throw new EntityNotFoundException("Company not found for given id" + companyId);
    }

    Company company = companyOptional.get();
    int oldRequestId = company.getRequestId();
    int requestId;
    if (oldRequestId == Integer.MAX_VALUE) {
        requestId = 1;
    } else {
        requestId = oldRequestId + 1;
    }

    int result = companyRepository.updateRequestId(companyId, requestId, oldRequestId);
    if (result == 0) {
        if (retry < 0) {
            throw new Exception("Unable to get requestId");
        }
        LOG.warn("Retrying since there was some update on requestId by some other thread");
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            LOG.warn("Unexpected InterruptException occurred while trying to get requestId");
        }
        return getAndIncrementRequestId(companyId, retry - 1);
    }
    company.setRequestId(requestId); //--------------------------> PROBLEM DOES NOT EXISTS
    return company;
}

请问我什至没有通过实体Company ..为什么会有两个查询??

1 个答案:

答案 0 :(得分:1)

这是因为当您执行“ companyRepository.findById(companyId);”时返回的公司以受管理的状态返回。 因此,在情况1中,当您在调用“ companyRepository.updateRequestId(companyId,requestId,oldRequestId);”之前设置请求ID时,将在公司存储库中创建一个事务对象,该对象执行受管实体的所有未决更新以及查询。方法“ updateRequestId”也会被触发。 在第二种情况下,由于您在调用“ companyRepository.updateRequestId(companyId,requestId,oldRequestId);”之后编写了set语句,因此为什么对托管对象的更新永远不会被触发,因为它没有得到任何交易