簡體   English   中英

Quarkus Panache Hibernate:如何清除緩存,使實體世界與數據庫狀態匹配?

[英]Quarkus Panache Hibernate: How to clear the cache, make entity world match the database state?

我是 quarkus 的新手,似乎在單元測試中遇到了 hibernate 緩存的問題。 該測試注入了“UserTransaction”。 該測試應檢查數據庫清理任務。

這就是我要做的:

  1. 創建實體
  2. 開始交易
  3. 使用“persistAndFlush”保存遵循“Active Record”模式的實體
  4. 提交交易
  5. 嘗試通過“find(id)”從數據庫中獲取實體以確保它已被保存
  6. 運行清理任務(實體從數據庫中刪除)
  7. 再次嘗試通過“find(id)”從數據庫中獲取實體以確保它已被刪除
    Document doc;
    UUID uuid;
    doc = new Document();
    uuid = UUID.randomUUID();
    doc.uuid = uuid;
    doc.doc = new byte[0];
    doc.createdAt = Instant.now();
    transaction.begin();
    doc.persistAndFlush();
    transaction.commit();
    doc = Document.findById(uuid);
    Assertions.assertNotNull(doc);
    TimeUnit.SECONDS.sleep(Long.parseLong(maxAge)+1);
    scheduler.cleanUp();
    doc = Document.findById(uuid);
    Assertions.assertNull(doc);

第 7 步失敗,因為 'find(id)' 返回實體,盡管它不再在數據庫中。

如果我跳過第 5 步,這不會發生! 所以這對我來說似乎是緩存問題。

我試圖注入 'Session'、'SessionFactory' 和 'EntitiyManager' 以訪問當前的 Hibernate 會話,但如果成功則沒有。

也許整個方法缺少我沒有得到的東西? 如何在像我這樣的設置中使實體世界與數據庫匹配?

歡迎任何提示和想法。

TIA

問題有一個簡單的解決方法,在最后的find操作中添加一個事務:

transaction.begin();
doc = Document.findById(uuid);
transaction.commit();

暫無
暫無

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

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