簡體   English   中英

Spring Data Jpa超時異常

[英]Timeout Exception with Spring Data Jpa

如果Spring Data Jpa存儲庫查詢超時,我可以捕獲哪個特定的異常?

可以說我有這樣的存儲庫:

public interface VoucherRepository extends CrudRepository<VoucherEntity, String> {

    @Transactional(readOnly = true, timeout = 30)
    VoucherEntity findByCode(String code);

    List<VoucherEntity> findAllByCodeIn(List<String> codes);
}

發生超時時,調用方會看到哪個異常?

我不知道如何模擬這種情況,獎金問題將是如何做到的?

使用的數據庫是PostrgreSQL。

您將獲得的異常是QueryTimeoutException ,它是DataAccessException的子類,它是通用的“使用Spring訪問數據庫時出了點問題”

如果已配置

 <jpa:repositories base-package="com.acme.repositories" />

從Spring文檔http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.create-instances

如創建存儲庫實例中所述,使用該元素可查找Spring Data存儲庫。 除此之外,它還為所有@Repository注釋的bean激活持久性異常轉換,以使JPA持久性提供程序引發的異常轉換為Spring的DataAccessException層次結構。

這樣您就可以捕獲DataAccessException來處理您的異常

使用Spring的@Transactional超時時,您將獲得“ org.springframework.transaction.TransactionSystemException”異常。

實際上,您可以通過在測試期間鎖定表來模擬這種情況,如下所示。

LOCK TABLE table_name IN ACCESS EXCLUSIVE MODE;

在完成測試並結束事務以釋放表上的鎖之前,請確保事務未完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM