java - Spring - 执行两个 hibernate 更新查询而不是一个

标签 java spring hibernate orm spring-transactions

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

公司服务

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 中的代码将触发两个 hibernate 更新,一个更新使用最新的 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;
}

我的问题是,当我什至没有在任何地方传递实体公司时,为什么会有两个查询..?

最佳答案

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

关于java - Spring - 执行两个 hibernate 更新查询而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51042739/

相关文章:

java - ValidatorUtils.getValueAsString() 什么时候可以返回 null?

java - 为什么 C++ 和 Java 中的动态数组具有不同的初始容量?

java - 如何在 Spring 3 中将 View 中表单中的对象列表绑定(bind)到 Controller

java - Ehcache、Redis 和 Gemfire 哪种缓存适用于哪种场景?

hibernate - 约束验证失败后,内存中的对象无效

java - 如何编排批量执行?

java - 为什么 "class"用于 Java 中的类?

java - Spring Framework应用中JDBC使用RowMapper的一些疑惑

java - Hibernate @ManyToMany 与复合键的映射

java - 无法延迟初始化角色集合。简单的 JPA findById