关闭

hibernate4.3 无法获取数据库最新值

标签: hibernateSSHJava缓存
2395人阅读 评论(0) 收藏 举报
分类:

在用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.

缓存清除

缓存机制

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

hibernate无法获取最新数据

遇到这样一个问题,有时候查询到的数据是修改之前的那条,并且这是个概率问题,有时候查出来的数据是对的,有时候还是以前的问题 代码是这样写的 @Override public Us...
  • qq_20925635
  • qq_20925635
  • 2017-02-17 09:45
  • 410

hibernate获取数据获取不到,没有报错

问题:使用hibernate4+mysql5,数据库中有内容,但hibernate获取不到,而且无报错。 解决方案: 确保输入参数正确; 看看hibernate扫描的entity包路径对...
  • danchaofan0534
  • danchaofan0534
  • 2017-01-14 14:30
  • 771

避免 Hibernate 中用get/load方法获取的实体调用set方法后自动更新

1、问题症状描述       最近在处理一个新需求问题,代码的大致逻辑是获取一个实体对象,调用该对象的set方法设置其中的某些字段,然后把修改后的实体作为参数供其他地方调用,根据返回值来决定是否更新这...
  • yang_lover
  • yang_lover
  • 2015-04-15 12:08
  • 5903

Hibernate开发实践笔记--对象在set属性时更新了数据库问题

之前一直没发现这个问题,后来是因为我的导航栏数据库中数据发生变动后,产生菜单的时候又update了一遍数据库中的数据,使得部分数据值为空了,当时觉得很奇怪,后来仔细看了下log4j打印的日志发现其中有...
  • wangyang1354
  • wangyang1354
  • 2016-01-28 21:06
  • 6740

Hibernate缓存非常烦人,想要最新数据连session.clear都不好使

一、hibernate一级缓存 (1)hibernate支持两个级别的缓存,默认只支持一级缓存; (2)每个Session内部自带一个一级缓存; (3)某个Session被关闭时,其对应的一级缓...
  • u012643122
  • u012643122
  • 2015-08-12 01:21
  • 1449

解决Android4.3版本下,手机短彩接收中文文件名附件,中文名字的附件无法保存(第二步:解决从从数据库中读取附件文件名,并在长按后保存附件时,中文乱码导致的无法保存附件)

从第一步我们发现,在第一步修改之后,在短彩绘画界面中中文附件名的附件已无法显示,经过打印堆栈我们发现还是中文乱码在作祟。下面我们接着进行分析,这次我们从UI层往逻辑处理层进行分析。
  • huangyabin001
  • huangyabin001
  • 2014-06-03 19:52
  • 1513

最新httpclient4.3 两种请求的两种方式

HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的Java 应用程序需要直接通过 HTTP协议来访问网络资源。        虽然在 JDK 的 java...
  • u012458817
  • u012458817
  • 2013-11-25 23:26
  • 2539

Linux下 Eclipse Standard 4.3.x Kepler 的安装 + 汉化方法(官方最新地址 )useful

1.下载Eclipse Standard 4.3.x   http://www.eclipse.org/downloads/ 2.然后安装Eclipse 4.3.x ...
  • duobaohongtu3
  • duobaohongtu3
  • 2014-01-13 10:10
  • 485

最新Eclipse配置Android开发环境SDK 并安装JellyBean 4.3的方法

1. 访问以下Android官方网站: http://developer.android.com/sdk/index.html 2. 点击右侧的“Download the SDK”按钮,Ubuntu...
  • ameyume
  • ameyume
  • 2013-08-19 21:38
  • 7457

2013/10/19 Android最新源码4.3下载-教程

Ubuntu12.04LTS环境,下载Android4.3.2源码,最新【2013/10/19】成功下载案例,谨此博文献给需要源码的战友们,当然其中肯定有描述不周到的地方,毕竟水平有限,请各位战友们多...
  • quanjin24k
  • quanjin24k
  • 2013-10-19 13:52
  • 2968
    个人资料
    • 访问:540437次
    • 积分:4468
    • 等级:
    • 排名:第7743名
    • 原创:74篇
    • 转载:80篇
    • 译文:2篇
    • 评论:14条
    文章分类
    最新评论