【发布时间】:2021-03-10 22:16:39
【问题描述】:
能否请您帮助我了解如何有效地将实体插入到多个表中?
相应地,我有 3 个表和 3 个实体。 Pricebook 有一系列 SKU,每个 SKU 有 1 个价格。基本上我想要的是在事务中插入多个实体,以防出现约束,我必须更新链中的实体。
当我尝试将超过 1 个价格手册并行插入 DB 时,就会出现问题,因此我实际上遇到了 PostgreSQL 死锁。我发现一种解决方法是将它们一个一个地插入队列中,但我知道这不是一个好主意。
这可能是一个愚蠢的问题,之前已经回答过,但我希望有人能给我提示。
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "pricebook")
public class Pricebook {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
//....
}
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "sku")
public class Sku {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
//....
}
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "price")
public class Price {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@JoinColumn(name = "pricebook_id", referencedColumnName = "id", unique = true)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Pricebook pricebook;
@JoinColumn(name = "sku_id", referencedColumnName = "id", unique = true)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Sku sku;
//....
}
这是 upsert 的 PricebookService 逻辑。
@NonNull
@Transactional
public Pricebook createPricebook(@NonNull CreatePricebookRequest request) {
final Instant startDate = PricebookConverter.toDate(request.getStartDate());
final Instant expirationDate = PricebookConverter.toDate(request.getExpirationDate());
if (startDate.isAfter(expirationDate)) {
throw new InvalidParametersException("The pricebook's start date later then its expiration date.");
}
final Region region = regionService.findRegionByName(request.getRegion());
final Optional<Pricebook> isPricebookFound =
pricebookRepository.findByRegionAndPricebookTypeAndStartDateAndExpirationDate(region,
request.getPricebookName(), startDate, expirationDate);
final Pricebook savedOrUpdatedPricebook;
if (isPricebookFound.isPresent()) {
final Pricebook foundPricebook = isPricebookFound.get();
savedOrUpdatedPricebook = pricebookRepository.save(
new Pricebook(foundPricebook.getPricebookId(), request.getName(), foundPricebook.getPricebookName(), foundPricebook.getRegion(), foundPricebook.getStartDate(),
foundPricebook.getExpirationDate());
logger.info("pricebook is updated successfully, pricebook={}", savedOrUpdatedPricebook);
} else {
savedOrUpdatedPricebook = pricebookRepository.save(
new Pricebook(request.getName(), request.getPricebookType(), region, startDate, expirationDate);
logger.info("pricebook is created successfully, pricebook={}", savedOrUpdatedPricebook);
}
final List<Sku> skus = skuService.createSku(savedOrUpdatedPricebook, request.getSkus());
logger.debug("skus are saved successfully, skus={}", skus);
return savedOrUpdatedPricebook;
}
这是一个 upsert 的 SkuService 逻辑。 skuToCreateOrUpdate 基本上只是一个包装逻辑的方法,如果它被发现或新的并返回一个新对象。
@NonNull
public List<Sku> createSku(@NonNull Pricebook pricebook, @NonNull List<CreateSkuRequest> skus) {
return skus.stream().map(sku -> {
final Optional<Sku> foundSku = skuRepository.findByCode(sku.getCode());
final Sku savedOrUpdatedSku = skuRepository.save(skuToCreateOrUpdate(sku, foundSku.map(Sku::getSkuId).orElse(null)));
final List<Price> prices = priceService.createPrices(pricebook, savedOrUpdatedSku, sku.getPrice());
logger.debug("prices are saved successfully, prices={}", prices);
return savedOrUpdatedSku;
}).collect(toList());
}
这是 upsert 的 PriceService 逻辑。
@NonNull
public List<Price> createPrices(@NonNull Pricebook pricebook, @NonNull Sku sku, @NonNull CreatePriceRequest price) {
final Optional<Price> foundPrice = priceRepository.findByPricebookAndSku(pricebook, sku);
final Price savedOrUpdatedPrice;
if (foundPrice.isPresent()) {
final Price priceToUpdate = foundPrice.get();
savedOrUpdatedPrice = priceRepository.save(
new Price(priceToUpdate.getPriceId(),
pricebook,
sku);
logger.info("price is updated successfully, price={}", savedOrUpdatedPrice);
} else {
savedOrUpdatedPrice = priceRepository.save(
new Price(pricebook, sku);
logger.info("price is created successfully, price={}", savedOrUpdatedPrice);
}
return Collections.singletonList(savedOrUpdatedPrice);
}
我到处都在使用 JpaRepository。就这样..
@Repository
public interface PricebookRepository extends JpaRepository<Pricebook, Long> {}
@Repository
public interface SkuRepository extends JpaRepository<Sku, Long> {}
@Repository
public interface PriceRepository extends JpaRepository<Price, Long> {}
【问题讨论】:
-
是否有错误消息告诉您为什么您遇到了死锁?我的理解是,如果你在两个事务中运行相同的实体创建操作,你不应该遇到死锁,因为插入的顺序是相同的,所以没有循环等待。请发布服务方法,并可能启用 SQL 日志记录,以查看哪些语句已死锁
-
是的,当然,错误消息看起来很像。如下所示:
ERROR: deadlock detected Detail: Process 27988 waits for ShareLock on transaction 1996705435; blocked by process 27990. Process 27990 waits for ShareLock on transaction 1996705089; blocked by process 27988. Hint: See server log for query details. Where: while inserting index tuple (153,8) in relation "sku"; nested exception is org.postgresql.util.PSQLException: ERROR: deadlock detected -
能否请您发布用于插入
Pricebook的代码?你提到了关于更新插入和约束的事情,所以我想有一些条件逻辑?我也理解sku更像是一个查找表,两个事务中的实体是否共享任何skus?此外,这将有助于查看每个事务执行的实际查询,因为这样我们可以推断正在获取哪些锁 -
您可以尝试通过在
PESSIMISTIC_WRITE模式中为您的服务方法可能更新的任何实体预先获取锁(作为代码中的第一个查询)来解决问题。但是,如果没有看到代码和/或查询,我们将无法提供任何进一步的帮助 -
@crizzis 当然,谢谢。我添加了 3 种方法,来自 3 种服务。 Pricebook 为每个 sku 代码调用 SkuService.createSkus() 以保存或更新,并在其中调用 PriceService.createPrice() 相应地创建价格。
标签: sql postgresql spring-boot hibernate jpa