繁体   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