在Hibernate程序中如果配置了二级缓存,想查看一下二级缓存中存放的数据条数,查询时错过的数据条数,缓存命中率等信息,那么该怎么办呢?
在Hibernate的hibernate.cfg.xml文件中,提供了hibernate.generate_statistics属性,该属性提供了Hibernate应用中操作的统计信息,必须在hibernate.cfg.xml文件中进行配置。该属性在配置文件中的默认值是关闭的,因为它会消耗一些资源,但是它很适合在我们开发过程中调试程序时使用。hibernate.cfg.xml文件中的配置如下:
<property name="generate_statistics">true</property/>
这时我们已经将Hibernate的统计信息打开了,接下来我们应该怎样使用它呢?
在org.hibernate.SessionFactory类中提供了一个得到统计信息的方法,该方法返回一个Statistic类型的数据。之后我们通过Statistics对象取出数据操作时的缓存信息。它提供了取出全部统计信息和取出指定缓存的统计信息,例如:我们就想查看二级缓存中的统计信息,那么可以通过方法指定。例如:我们添加一条数据,该方法是addPet(),将该方法放到main()方法中测试,如下:
addPet()方法的实现:
public void addPet(){
// 创建一个Hibernate Configuration类的实例
Configuration config = new Configuration().configure() ;
// 创建SessionFactory对象
SessionFactory sf = config.buildSessionFactory() ;
// 调用调添加方法
PetDaoImpl petImpl = new PetDaoImpl();
petImpl.add();
// 创建Statistics对象,并通过SessionFactory对象获得统计信息
Statistics st = sf.getStatistics();
// 打印全部统计信息
System.out.println(st);
// 打印二级缓存信息
System.out.println(st.getSecondLevelCacheHitCount());
}
将addPet()方法添加到main()方法中进行测试,将会输出统计信息。
分享到:
相关推荐
In a near future release of the Motor Control SDK, support for these IDE will be dropped and it will not be possible anymore to generate a project for them from the Workbench. ◾Introduced a work...
Eclipse Modeling Tools(eclipse-modeling-2022-06-R-linux-gtk-x86_64.tar.gz) 适用于Linux x86_64: The Modeling package provides tools and runtimes for building model... and finally to generate Java code
WeChat Mini Program Live Version Backup Case Code
使用Generate_Kernel_Uboot_Project_forIDE工具,生成针对linux, u-boot的当前编译使用的文件列表,并提供多种格式,支持导入到Source Insight, Scitools Understand and SlickEdit等集成开发环境中.
keygen maker source code
代码生成器,很好的例子,方便程序的开发
CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the ...
generate_excel.vcproj为在vs2005下的工程文件,如果需要在vc6或Linux等其他编译环境下使用只需新建工程将.h文件和.cpp文件导入即可编译。 generate_excel |------include | |----gen_excel_file.h | |----input_...
Minecraft_Pre-generate_World 预先生成世界的Powershell脚本如何运行脚本在脚本的“设置”部分中更改值开始游戏,载入世界,进入菜单启动脚本。贡献随时编写一个Issue或发出请求请求来更改脚本。
generate_code_manager 项目介绍 我为什么想开源一个代码生成器? 现在的项目基本上属于前后端分离的 所以我们需要提供接口文档给前端。 没有API文档工具之前,大家都是手写API文档的,在什么地方书写的都有,我们...
Generate English Text To train: python train.py \ --input_file data/shakespeare.txt \ --name shakespeare \ --num_steps 50 \ --num_seqs 32 \ --learning_rate 0.01 \ --max_steps 20000 To sample: ...
yEd is a powerful desktop application that can be used to quickly and effectively generate high-quality diagrams. Create diagrams manually, or import your external data for analysis. Our automatic ...
- generate_data.py:利用提供的训练数据构造神经网络输入特征。 - generate_label.py:利用提供的训练数据生成噪声类型的真实标签。 - neural_network.py:定义神经网络结构,使用训练数据优化神经网络的参数,并...
Generate_SystemMatrix是一个计算OSEM,MLEM迭代图像算法中系统矩阵的函数,其用法Generate_SystemMatrix(pixel),为所要重建图像的大小,一般为64,128,256,pixel也大运算越慢。 里面的diedai-test文件是一个简单...
mysql_generate_series:MySQL的generate_series mysql_generate_series是PostgreSQL函数MySQL版本。 该版本(原始版本)经过(大量)改编,并试图简化方法调用,并在可能的情况下使MySQL版本参数遵循PostgreSQL...
jeecg-mybatis-generate 源码
生成已tk.mybatis封装的mybatis代码
matlab导入excel代码generate_50_million_alphanumeric_unique_keys 生成5000万个字母数字唯一键。 关键字:sas sql join合并大数据分析宏oracle teradata mysql sas社区stackoverflow statistics人工智慧AI Python ...
generate_patches_SIDD.py
Eclipse Modeling Tools(eclipse-modeling-2022-06-R-macosx-cocoa-x86_64.dmg) 适用于macOS x86_64: The Modeling package provides tools and runtimes for building model... and finally to generate Java code