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

原创 2015年07月10日 09:14:32

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

缓存清除

缓存机制

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

hibernate无法获取最新数据

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

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

在用ssh框架的时候遇到一个问题(hibernate版本4.3) 问题描述:web端和应用程序都可以读写数据库,当应用程序修改数据库后,hibernate无法读取最新值,读出来的一直都是旧数据。 ...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

java获取数组元素最值及其下标

public class HuoQuZuiZhi { public static void main(String[] args){ int arr[] = new int[]{1,2,3,4,...

SQL 获取最新ID,scope_identity() ,@@identity,ident_current

--scope_identity ()用这个方法的返回值是null,因为它返回的是当前会话、作用域的identity的最后一个值     --@@identity 返回当前会话中任何作用域的id...

获取数组最大值跟最小值

/* 给定一个数组{33,5,7,3,35,6,0} 1 获取数组的最大值和最小值 */ class ArryTest { /* 获取数组的最大值 思路: 1,获取最值需要比较,每一次...

Django程序无法获取数据库更新过的记录

一、问题 之前尝试在django应用中写一个定期从数据库取数据处理的小程序. 这个小程序的代码大体像是这样子: import os, sys import time os.environ.setde...

ajax获取dataset数据 无刷新

//返回DataSet(XML) $.ajax({ type: "POST", ...

Sql获取数据集中各类型中的最大值(最新值)

最近在论坛中的MS Sql Server版块经常看到网友求助如何从一组数据集中获取每个类型中的某个字段最大的一条记录或者最新记录,首先大家都会想到采用group关键字分组,再利用max()函数获取最大...

获取一亿数据获取前100个最大值

获取一亿数据获取前100个最大值

jquery获取单选框复选框下拉框值

jquery获取单选框(radio)复选框(checkbox)下拉框(select)的值,亲测可用。有什么疑问可以留言 效果图: html代码: body...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)