在用ssh框架的时候遇到一个问题(hibernate版本号4.3)

问题描写叙述:web端和应用程序都能够读写数据库。当应用程序改动数据库后。hibernate无法读取最新值,读出来的一直都是旧数据。

网上查找:初步定为是缓存引起,在关闭hibernate 的一级。二级缓存和查询缓存之后。依旧读不到最新值。

清除一级缓存方法:

Hibernate一级缓存又称为“Session的缓存”,是无法关闭的,仅仅能清除刷新,或者随着session的关闭而清除。

Session session = sessionFactory.getCurrentSession();
if (session != null) {
session.clear(); // internal cache clear
}
Cache cache = sessionFactory.getCache(); if (cache != null) {
cache.evictAllRegions(); // Evict data from all query regions.
}

当然也能够选择性清除

org.hibernate.Cache.evictCollectionRegions()
org.hibernate.Cache.evictDefaultQueryRegion()
org.hibernate.Cache.evictEntityRegions()
org.hibernate.Cache.evictQueryRegions()
org.hibernate.Cache.evictNaturalIdRegions()

刷新

getSessionFactory().getCurrentSession().flush();

关闭二级缓存和查询缓存方法:

在applicationContext.xml文件里加入下面代码:

<prop key="hibernate.cache.use_second_level_cache">false</prop>   <!--关闭二级缓存 -->
<prop key="hibernate.cache.use_query_cache">false</prop>   <!--关闭查询缓存 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <!--设置二级缓存的Provider类 -->
<prop key="hibernate.cache.provider_configuration_file_resource_path">WEB-INF/classes/ehcache.xml</prop> <!--设置缓存的配置文件路径 -->

以上操作均不能解决这个问题,排除缓存原因,终于定位到事务管理,因为读值的方法存在一个事务中,整个事务过程没有跑完导致无法刷新数据。

解决的方法:在applicationContext.xml文件里将 出问题的方法类移除。问题解决。

參考资料:

Summary: This cache is sometimes not really called a cache. However, in order to implement certain isolation levels the database itself may be caching some query results.

Lifecycle/Scope: This cache is scoped to a single Session/EntityManager. The lifecycle is bound to the transaction lifecycle.

Clearing the cache: No way I know of other than starting a new transaction

What gets cached: Queries and result (if isolation is at repeatable read or serializable level)

On by default: Depends on the default isolation level which comes from the database. By default, MySQL ships with repeatable read isolation and so yes, this is on by default for MySQL.

Turning it on/off: Can be specified when creating the transaction. Can also be changed by changing the default on the database.

Useful Information: Hibernate/JPA doesn't really have any control over the operation of this cache other than specifying which isolation level is desired.

Session Level (1st-Level) Cache

Summary: This cache is the EntityManager/Session cache. I believe this is also what is referred to as the persistence context.

Lifecycle/Scope: This cache is scoped to a single Session/EntityManager. The lifecycle is bound to the transaction lifecycle.

Clearing the cache: Calling clear() on
the EntityManager or Session clears the entire cache. Calling evict() on
the Session clears a single object from the cache.

What gets cached: Everything

On by default: Yes

Turning it on/off: Can't be turned off

Useful Information: This cache gets merged with the database whenever flush() is
called. Unless that happens other transactions will not be able to see things in this cache. The best way to guarantee a flush() is
to commit the transaction.

2nd-Level Cache

Summary: This is a secondary cache that can be enabled (usually to try and improve performance).

Lifecycle/Scope: I believe this is bound to the EntityManagerFactory/SessionFactory. Automatic eviction of this cache depends on the cache strategy. In a read-only strategy data is never evicted automatically.
In a read-write or nostrict read-write strategy data will be evicted when the session closes. Not 100% certain of this.

Clearing the cache: You can call getCache().evict(class) to
evict a specific class and getCache().evictAll() to
evict the entire cache. These methods are on the EntityManagerFactory.

What gets cached: You explicitly configure which entities should be cached.

On by default: No

Turning it on/off: Turned on/off in the Hibernate configuration

Useful Information:

Query Cache

Summary: Query Cache is a cache which stores queries, query parameters and results. If the query and query parameters are the same, you can expect the result to be the same.

Lifecycle/Scope: I have no idea when data in this cache is determined to be stale. I believe the scope is at the EntityManagerFactory/SessionFactory level. In addition, Hibernate keeps a list of "last
update by Hibernate" timestamps for each of the tables. Hibernate uses these timestamps to determine if query results are stale and evict stale queries automatically.

Clearing the cache: The evictQueries() method
on the SessionFactory can be used to manually evict the query cache.

What gets cached: Queries and their results

On by default: No

Turning it on/off: Turned on/off in the Hibernate configuration

Useful Information: The query cache only caches entity IDs. It must be used in conjunction with a 2nd-level cache to achieve a true (no DB access)
cache.

缓存清除

缓存机制

最新文章

  1. 浅谈我对C#中抽象类与接口的理解
  2. 挣值管理(PV、EV、AC、SV、CV、SPI、CPI) 记忆
  3. AOP programming paradiag
  4. jQuery offset,position,offsetParent,scrollLeft,scrollTop html控件定位 css position
  5. python学习笔记-(八)装饰器、生成器&amp;迭代器
  6. 只写104行代码!在nopCommerce中如何实现自动生成网站地图
  7. HBase Scan Timeout-OutOfOrderScannerNextException
  8. 鼎信通达gsm网关和asterisk对接的调试
  9. URL编码方法比较
  10. Java —— 时区(夏令时)问题
  11. Tiny210编译和烧写u-boot步骤
  12. 蓝桥网试题 java 算法训练 区间k大数查询
  13. Hibernate学习(二)保存数据
  14. 不可思议的纯 CSS 滚动进度条效果
  15. 安装setuptools 报错缺少zlib
  16. pygame-KidsCanCode系列jumpy-part2-加速度与摩擦力
  17. R语言-图的要素颜色
  18. Git Extensions system.invalidoperationexception尚未提供文件名,因此无法启动进程
  19. JDK 升级问题小结
  20. Python生成pyd文件

热门文章

  1. cocos2d-Lua02Lua面向对象
  2. symfony手动触发修饰html
  3. springcloud服务已经关但是Eureka还是显示up
  4. Paxos算法细节详解(一)
  5. PHP中“==”运算符的安全问题
  6. node学习笔记3——文件操作fs
  7. 前端最全的 API 集锦
  8. Java:集合与数组转换
  9. 关于Unity中的NGUI字体
  10. (笔记)Mysql命令grant on:增加新用户并控制其权限