Hibernate中启用日志
Problem
How do you determine what SQL query is being executed by Hibernate? How can you see the Hibernate’ internal workings? How do you enable logging to troubleshoot complex issues related to Hibernate?
Solution
You have to enable Hibernate logging in the Hibernate configuration. Hibernate uses Simple Logging Facade for Java (SLF4J) to log various system events. SLF4J, which is distributed as a free software license, abstracts the actual logging framework that an application uses. SLF4J can direct your logging output to several logging frameworks:
- NOP: Null logger implementation
- Simple: A logging antiframework that is very simple to use and attempts to solve every logging problem in one package
- Log4j version 1.2: A widely used open–source logging framework
- JDK 1.4 logging: A logging API provided by Java
- JCL: An open-source Commons logging framework that provides an interface with thin wrapper implementations for other logging tools
- Logback: A serializable logger that logs after its deserialization, depending on the chosen binding
To set up logging, you need the slf4j-api.jar file in your classpath, together with the JAR file for your preferred binding: slf4j-log4j12.jar in the case of log4j. You can also enable a property called showsql to see the exact query being executed. You can configure a logging layer such as Apache log4j to enable Hibernate class- or package-level logging. And you can use the Statistics interface provided by Hibernate to obtain detailed information.
How It Works
You have to configure the Hibernate show_sql property to enable logging.
Inspecting the SQL Statements Issued by Hibernate
Hibernate generates SQL statements that enable you to access the database behind the scene. You can set the show_sql property to true in the hibernate.cfg.xml XML configuration file to print the SQL statements to stdout:
<property name="show_sql">true</property>
Enabling Live Statistics
You can enable live statistics by setting the hibernate.generate_statistics property in the configuration file:
<property name="hibernate.generate_statistics">true</property>
You can also access statistics programmatically by using the Statistics interfaces. Hibernate provides SessionStatistics and Statistics interfaces in the org.hibernate.stat package. The following code shows the use of some utility methods:
SessionFactory sessionFactory = SessionManager.getSessionFactory();
session = sessionFactory.openSession();
SessionStatistics sessionStats = session.getStatistics();
Statistics stats = sessionFactory.getStatistics();
tx = session.beginTransaction();
Publisher publisher = new Publisher();
publisher.setCode("apress");
publisher.setName("Apress");
publisher.setAddress("233 Spring Street, New York, NY 10013");
session.persist(publisher);
tx.commit();
logger.info("getEntityCount- " + sessionStats.getEntityCount());
logger.info("openCount- " + stats.getSessionOpenCount());
logger.info("getEntityInsertCount- " + stats.getEntityInsertCount());
stats.logSummary();
session.close();
The output of this code sample is shown here (the complete log is given for clarity):
Hibernate中启用日志的更多相关文章
- Hibernate使用Log4j日志记录(使用properties文件)
我们知道,Log4j和Logback框架可用于支持日志记录hibernate,使用log4j有两种执行日志记录的方法: 通过log4j.xml文件(或) 通过log4j.properties文件 在这 ...
- Hibernate使用Log4j日志记录(使用xml文件)
日志记录使程序员能够将日志详细信息永久写入文件. Log4j和Logback框架可以在hibernate框架中使用来支持日志记录. 使用log4j执行日志记录有两种方法: 通过log4j.xml文件( ...
- hibernate学习(9)——日志,一对一,二级缓存
1.Hibernate中的日志 1 slf4j 核心jar : slf4j-api-1.6.1.jar .slf4j是日志框架,将其他优秀的日志第三方进行整合. 整合导入jar包 log4j 核心 ...
- hibernate中的缓存机制
一.为什么要用Hibernate缓存? Hibernate是一个持久层框架,经常访问物理数据库. 为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能. 缓存内的数据是对物理数据源中的数 ...
- 【Azure Application Insights】在Azure Function中启用Application Insights后,如何配置不输出某些日志到AI 的Trace中
问题描述 基于.NET Core的Function App如果配置了Application Insights之后,每有一个函数被执行,则在Application Insights中的Logs中的tra ...
- 【Hibernate】解析hibernate中的缓存
Hibernate中的缓存一共有三种,一级缓存.二级缓存.查询缓存.缓存除了使用Hibernate自带的缓存,还可以使用redis进行缓存,或是MongoDB进行缓存. 所使用的Demo: User. ...
- Java三大框架之——Hibernate中的三种数据持久状态和缓存机制
Hibernate中的三种状态 瞬时状态:刚创建的对象还没有被Session持久化.缓存中不存在这个对象的数据并且数据库中没有这个对象对应的数据为瞬时状态这个时候是没有OID. 持久状态:对象经过 ...
- mysql的innodb中事务日志ib_logfile
mysql的innodb中事务日志ib_logfile事务日志或称redo日志,在mysql中默认以ib_logfile0,ib_logfile1名称存在,可以手工修改参数,调节开启几组日志来服务于当 ...
- ns3 Tutorial 中的日志模块(翻译)
转载地址:http://blog.sina.com.cn/s/blog_8ecca79b0101d7fe.html 1 日志模块的使用 在运行 first.cc 脚本时,我们已经简单 ...
随机推荐
- Eclipse之Failed to load the JNI shared library”……\jvm.dll”的解决方案
问题描述:java环境变量配置完全正确,但是运行eclipse时提示:Failed to load the JNI shared library " xxx\jva.dll" 原因 ...
- spyder常用快捷键
Ctrl + 1: 注释/反注释 Ctrl + 4/5: 块注释/块反注释 Ctrl + L: 跳转到行号 Tab/Shift + Tab: 代码缩进/反缩进 Ctrl +I:显示帮助
- 配置《算法 第四版》的Eclipse开发环境
1. 安装JAVA JAVA网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 配置环境变量(我把JAVA安装在 ...
- 内存分配函数malloc、realloc、calloc、_alloca
1.内存分配函数_alloca.malloc.realloc.calloc: _alloca 函数原型void * __cdecl _alloca(size_t); 头文件:malloc.h _all ...
- 实战Django:官方实例Part6
我们终于迎来了官方实例的最后一个Part.在这一节中,舍得要向大家介绍Django的静态文件管理. 现在,我们要往这个投票应用里面添加一个CSS样式表和一张图片. 一个完整的网页文件,除了html文档 ...
- 17.python自定义函数
什么是函数,函数说白了就是将一系列代码封装起来,实现代码的重用. 什么是代码重用? 假设我有这样的需求: 但是我还是觉得太麻烦了,每次想吃饭的时候都要重复这样的步骤.此时,我希望有这样的机器:
- 理解ruby on rails中的ActiveRecord::Relation
ActiveRecord::Relation是rails3中添加的.rails2中的finders, named_scope, with_scope 等用法,在rails3统一为一种Relation用 ...
- properties文件
properties文件也叫资源文件,以键值对的形式存放文本内容.一个properties对象代表一个资源文件 步骤:1.生成properties对象2.生成InputStream/Reader来读取 ...
- [转]ORACLE的ProC用法讲解
pro*c是高级的用法,OCI是oracle的基础用法 如何编译.pc文件: proc code=cpp parse=none iname=filename.pc oname=filename.cp ...
- hdu 2102 A计划
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸 ...