Spring - 执行了两个休眠更新查询而不是一个
Spring - Two hibernate update queries are executed instead of one
我正在 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);
}
但是服务中的上述代码将触发两个休眠更新,一个将 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);" 时,返回的公司返回 managed 状态。
因此,在案例 1 中,当您在调用 "companyRepository.updateRequestId(companyId, requestId, oldRequestId);" 之前设置请求 ID 时,将在公司存储库中创建一个事务对象,该对象执行托管实体的所有未决更新以及方法 "updateRequestId" 的查询被解雇。
在第二种情况下,由于您在调用 "companyRepository.updateRequestId(companyId, requestId, oldRequestId);" 之后编写了 set 语句,这就是为什么对托管对象的更新永远不会被触发,因为它没有获得任何事务
我正在 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);
}
但是服务中的上述代码将触发两个休眠更新,一个将 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);" 时,返回的公司返回 managed 状态。 因此,在案例 1 中,当您在调用 "companyRepository.updateRequestId(companyId, requestId, oldRequestId);" 之前设置请求 ID 时,将在公司存储库中创建一个事务对象,该对象执行托管实体的所有未决更新以及方法 "updateRequestId" 的查询被解雇。 在第二种情况下,由于您在调用 "companyRepository.updateRequestId(companyId, requestId, oldRequestId);" 之后编写了 set 语句,这就是为什么对托管对象的更新永远不会被触发,因为它没有获得任何事务