4

我的功能就像我想插入用户地址,用户可以有多个地址,即使用户可以将其中一个地址标记为默认地址,下面是我的代码。如果用户输入地址并将该地址标记为默认地址,我们会将其他地址默认标志重置为 false,如代码所示

@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
public Address store(Address address){
    Assert.notNull(address, "address can not be null");
    resetDefault(address);
    Address createdAddress = this.create(address);
    return createdAddress;
}

在哪里

private void resetDefault(Address address) {
    if (address.isDefault())
        resetDefaultAddress(address);
}

 private void resetDefaultAddress(Address address) {
    addressRepository.unsetDefaultAddress(address.getAssociationId(),
            address.getCategory());
}

我们使用了 JPA 存储库

@Modifying
@Query("update Address a set a.defaultAddress = false where a.defaultAddress = true and a.associationId =:associationId and a.category =:category")
int unsetDefaultAddress(@Param("associationId") String associationId,
        @Param("category") AddressCategory category);

现在我的问题是它在开发人员环境中运行良好,但是当我们对我们的应用程序进行负载测试时,我们完成了 100 个并发请求,我们得到了

org.springframework.dao.CannotAcquireLockException: Could not execute JDBC batch update; SQL [insert into wsc_address (address1, address2, address3, alias, associationId, attributes, category, city, country, defaultaddress, email1, email2, firstname, lastaccesstimestamp, lastname, phone1, phone2, phone3, postalcode, province, status, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]; nested exception is org.hibernate.exception.LockAcquisitionException: Could not execute JDBC batch update
Caused by: org.hibernate.exception.LockAcquisitionException: Could not execute JDBC batch update

我们正在使用 MySql 数据库。

或者任何可以解决我的目的并提高性能的单个查询。

我还注意到,在注释掉 resetDefault(address); 从商店方法它工作正常。

4

0 回答 0