java hibernate 缓存,如何停止Java或Hibernate缓存

I have an app to retrieve data from Database, and I monitor the time my app takes to retrieve data.

But I have an issue when I use the same data input set to retrieve data with my app, the second time retrieving will take much less time.

I assume Java or Hibernate has some cache or temp file to save the data, so second time run will be fast, but I don't want it happen. I need monitor the time it actually takes, not the time retrieve from cache or temp file.

I tried to forbid the cache and temp file generate in Java control Panel, I tried to disable the hibernate cache(first level or second level). But these are still not solve my problem. The second time run still takes less time than it should take.

Any idea the reason caused the second time run faster? it just a simple app to retrieve data from DB

解决方案

The Hibernate 1st level cache can not be disabled (see How to disable hibernate caching). You need to understand Hibernate's session cache if you want to force Hibernate querying to the database.

First level cache is associated with “session” object and other

session objects in application can not see it.

The scope of cache objects is of session. Once session is closed,

cached objects are gone forever.

First level cache is enabled by default and you can not disable it.

When we query an entity first time, it is retrieved from database

and stored in first level cache associated with hibernate session.

If we query same object again with same session object, it will be

loaded from cache and no sql query will be executed.

The loaded entity can be removed from session using evict() method.

The next loading of this entity will again make a database call if

it has been removed using evict() method.

The whole session cache can be removed using clear() method. It will

remove all the entities stored in cache.

You should therefor either use the evict() or clear() method to force a query to the database.

In order to verify this, you can turn on SQL output using the hibernate.show_sql configuration property (see https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch03.html#configuration-optional).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值