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

缓存清除

缓存机制

hibernate4.3 无法获取数据库最新值的更多相关文章

  1. postman+xmysql实现postman与数据库的交互,获取数据库的值来作为参数进行请求

    安装nodejs和npm详细步骤:https://www.runoob.com/nodejs/nodejs-install-setup.html 安装xmysql 执行命令: npm install ...

  2. [Jacky] 解决Ext.Net GridPanel 选择的行数据刷新后不能获取最新值

    选择GridPanel中一行数据,当变更数据时并重新刷新之后不能获取最新值,需通过如下方式获取: var internalId = gridPanel.getSelectionModel().getL ...

  3. JS中获取数据库中的值

    在本次项目中,遇到很多问题,经过努力,都逐步得到解决.静下心来,做一个记录,以供以后学习. 在项目中遇到一个问题,需要在JS中读取数据库中的值,然后再把值返回到页面中,解决方案如下:使用Ajax方法来 ...

  4. grails项目获取前后台的值

    grails项目中前台传值给后台: 加入我有a.gsp这个页面,a.gsp中有如下代码: 姓名:<input type="text" name="xing" ...

  5. C#获取存储过程返回值和输出参数值的方法

    //转自网络,先留个底 1.获取Return返回值 //存储过程 //Create PROCEDURE MYSQL // @a int, // @b int //AS // return @a + @ ...

  6. 获取数据库表详细信息、存储过程、视图、的sql

    select s.[name] + '.' + t.[name] as tablename from sys.tables as t,sys.schemas as s where t.schema_i ...

  7. jmeter 性能测试 JDBC Request (查询数据库获取数据库数据) 的使用

    JDBC Request 这个Sampler可以向数据库发送一个jdbc请求(sql语句),并获取返回的数据库数据进行操作.它经常需要和JDBC Connection Configuration配置原 ...

  8. 【java 获取数据库信息】获取MySQL或其他数据库的详细信息

    1.首先是 通过数据库获取数据表的详细列信息 package com.sxd.mysqlInfo.test; import java.sql.Connection; import java.sql.D ...

  9. 通过jdbc获取数据库中的表结构

    通过jdbc获取数据库中的表结构 主键 各个表字段类型及应用生成实体类   1.JDBC中通过MetaData来获取具体的表的相关信息.可以查询数据库中的有哪些表,表有哪些字段,字段的属性等等.Met ...

随机推荐

  1. VB6学习笔记

    1.数据库读取 [工程]菜单的[引用]菜单项,打开引用对话框,选中[Microsoft ActiveX Data Objects 6.1 Library] [工程]菜单的[引用]菜单项,打开引用对话框 ...

  2. jQuery(三):样式操作

    一.DOM操作分类 DOM Core:任何一种支持DOM的编程语言都可以使用它,例如:getElementById(). HTML-DOM:用于处理HTML文档,例如:document.forms. ...

  3. sublime 之 vitage/emmet

    ctrl+shift+p 打开Package Controlctrl+shift+t 恢复已关闭标签ctrl+/ 代码注释/取消注释 --------------------------------- ...

  4. Qt中QString、QByteArray、int、double之间转换

    最近写Qt中的tcp网络编程,Socke连接后,接受到的数据类型是字节型,这就涉及到了大量的类型转换,在网上辗转几辄,总算有了点结果,特此跟大家分享.好了,不废话,下面细说. 方法/步骤     1. ...

  5. 自然语言交流系统 phxnet团队 创新实训 项目博客 (七)

    在本项目中使用到的“语音转文本”的技术总结: 语音转文本部分是调用的科大讯飞的在线语音,它的激发方式是按键,通过按钮触发开启安卓设备的录音,此部分需要在源码中写入关于安卓权限的要求,来调用安卓的录音权 ...

  6. Data source rejected establishment of connection, message from server: "Too many connections"

    详细错误信息: Caused by: com.MySQL.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source ...

  7. if 结构和三目运算和switch语句

    if语句需要注意的地方: if判断只能接一个语句,存在多个语句时,用块语句表示{},若在if判断后 直接加“:”相当于if判断后加一个空语句,即使条件成立什么也不会干! 1. if的第一种形态(真假) ...

  8. 【Centos】systemd入门教程

    systemd使用教程 常用指令 运行一个服务: systemctl start <服务名> 关闭一个服务: systemctl stop <服务名> 重启一个服务: syst ...

  9. selenium操作浏览器-窗口切换

    package seleniumLearn1; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openq ...

  10. 2014年第五届蓝桥杯C/C++B组省赛题目解析

    一.啤酒和饮料 啤酒每罐2.3元,饮料每罐1.9元.小明买了若干啤酒和饮料,一共花了82.3元. 我们还知道他买的啤酒比饮料的数量少,请你计算他买了几罐啤酒. 注意:答案是一个整数.请通过浏览器提交答 ...