【问题标题】:Quarkus Panache Hibernate: How to clear the cache, make entity world match the database state?Quarkus Panache Hibernate:如何清除缓存,使实体世界匹配数据库状态?
【发布时间】:2020-07-05 19:47:37
【问题描述】:

我是 quarkus 的新手,似乎在单元测试中遇到了休眠缓存的问题。 测试注入了“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 Session 的访问权限,但如果成功则没有。

也许整个方法缺少我没有得到的东西? 如何在像我这样的设置中使实体世界与数据库匹配?

欢迎任何提示和想法。

TIA

【问题讨论】:

  • 你能告诉我们你的代码吗?
  • 问题已更新。

标签: java hibernate caching quarkus


【解决方案1】:

问题有一个简单的解决方法,在最后的find操作中添加一个事务:

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

【讨论】:

  • 也为我工作。我担心当前 Quarkus 版本 1.10.x 中的这种需求。甚至试图通过 EntityManager 来evict 缓存。非常感谢这个答案!
猜你喜欢
  • 1970-01-01
  • 2023-01-14
  • 2021-09-12
  • 2021-03-09
  • 2021-08-09
  • 2020-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多