Hibernate对象没有被缓存(Hibernate object not getting cached)

编程入门 行业动态 更新时间:2024-10-22 10:46:35
Hibernate对象没有被缓存(Hibernate object not getting cached)

在我们的应用程序中,我们配置了Hibernate来处理EHcache。 目标是,一旦对象被加载到Cache中,除非对象被改变,否则不应调用每个db调用。

为了测试这个,我正在调用对象并打印identityhashcode [使用调用System.identityHashCode(this) ]的对象。 而且我注意到每次调用都会改变对象的identityhashcode ,这让我们觉得每次都会加载对象。

但是在日志中,我们没有看到Hibernate对数据库进行任何sql调用。

如果我的测试是否正确,有人可以指导吗?

In our application we have configured Hibernate to work with EHcache. Aim is that once object is loaded in Cache, no db call should every be invoked unless object is changed.

In order to test this, i am making call to object and printing identityhashcode [using call System.identityHashCode(this)] of object. And i notice that identityhashcode of object is changing in every call which makes us feel that object is loading everytime.

But in logs, we donot see Hibernate making any sql calls to database.

Can someone please guide, if my test is correct or not?

最满意答案

有很多事情可以解释这种差异。 此外,不击中数据库也可能意味着您从会话缓存(又称为一级缓存)中获取对象。 确保在一个会话中创建对象,并在另一个会话中检索两次(第一次可能打到数据库,第二次不应该)。

理想的做法是向Hibernate询问是否从缓存中检索到对象。 最简单的方法是启用统计信息收集,然后打印命中/未命中:

Statistics stats = sessionFactory.getStatistics(); stats.setStatisticsEnabled(true); ... // do your work long hitCount = stats.getQueryCacheHitCount(); long missCount = stats.getQueryCacheMissCount();

There are many things that might explain the difference. Also, not hitting the database might also mean that you are getting objects from the session cache (aka, first level cache). Make sure you create the object in one session and retrieve it twice in another session (the first might hit the database, the second shouldn't).

The ideal would be to ask Hibernate if the object was retrieved from the cache. The easiest way, is to enable the statistics collection and then print the hits/misses:

Statistics stats = sessionFactory.getStatistics(); stats.setStatisticsEnabled(true); ... // do your work long hitCount = stats.getQueryCacheHitCount(); long missCount = stats.getQueryCacheMissCount();

更多推荐

本文发布于:2023-04-28 02:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330288.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   对象   Hibernate   object   cached

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!