Hibernate show sql等于Stdout [英] Hibernate show sql is equal to Stdout

查看:99
本文介绍了Hibernate show sql等于Stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在休眠状态下声明了<property name="show_sql">true</property>.我在此链接中阅读了stdout问题.标准输出将占用更多内存.那么 show_sql 功能将如何工作?因为我在服务器日志中看到了stdout sql查询.

I declared <property name="show_sql">true</property> in hibernate. I read stdout issues in this link. Stdout will consume more memory. So How show_sql functionality will working?. Because of I have seen in server log stdout sql query.

13:06:53,323 INFO  [stdout] (http-/172.29.250.43:8080-7) Hibernate: select sampledata0_.idsample as idsample4_10_1_, sampledata0_.id as id1_9_1_, sampledata0_.id as id1_9_0_, sampledata0_.idsample as idsample4_9_0_, sampledata0_.sname as sname2_9_0_, sampledata0_.svalue as svalue3_9_0_ from sample_meta_data sampledata0_ where sampledata0_.idsample=?
13:06:53,324 INFO  [stdout] (http-/172.29.250.43:8080-7) Hibernate: select sampledata0_.idsample as idsample4_10_1_, sampledata0_.id as id1_9_1_, sampledata0_.id as id1_9_0_, sampledata0_.idsample as idsample4_9_0_, sampledata0_.sname as sname2_9_0_, sampledata0_.svalue as svalue3_9_0_ from sample_meta_data sampledata0_ where sampledata0_.idsample=?

内部休眠时假定使用System.out.println.那会消耗更多的内存吗? format_sqlshow_sql在内部如何工作?

Internally hibernate suppose to use System.out.println. Then Will it consume more memory? How format_sql and show_sql working internally?

推荐答案

show_sqlformat_sql设置true不会消耗更多的内存,但是它们会将SQL语句写入控制台,因此非常耗时

Setting true for show_sql and format_sql will not consume more memory but they will write the SQL statements to console so it is time consuming.

来自文档:

将所有SQL语句写入控制台.这是设置的替代方法 日志类别org.hibernate.SQL进行调试.

Write all SQL statements to console. This is an alternative to setting the log category org.hibernate.SQL to debug.

例如真实|错误

因此,当您将属性show_sql设置为true时,休眠将使用以下方法将SQL语句打印到控制台:

So when you set the property show_sql to true then hibernate prints the SQL statement to console using below method:

Hibernate使用以下方法从

Hibernate uses below method for displaying the SQL statement from SQLStatementLogger class that it uses internally.

/**
 * Log a SQL statement string.
 *
 * @param statement The SQL statement.
 * @param style The requested formatting style.
 */
public void logStatement(String statement, FormatStyle style) {
    if ( log.isDebugEnabled() || logToStdout ) {
        style = determineActualStyle( style );
        statement = style.getFormatter().format( statement );
    }
    log.debug( statement );
    if ( logToStdout ) {
        System.out.println( "Hibernate: " + statement );
    }
}

在上述方法中,如果format_sql设置为true,则将对SQL语句进行格式化,否则将不应用格式化样式.

In the above method if the format_sql is set to true then the SQL statement will be formatted or else the formatting style will not be applied.

现在,如果LOG级别设置为DEBUG,则将SQL语句写入日志文件.如果将show_sql设置为true,则它将消息打印到控制台.如您问题中提供的链接中所述,与将数据打印到日志文件相比,使用System.out.println更加耗时.

Now if the LOG level is set to DEBUG then the SQL statement is written to log file. If you set the show_sql to true then it prints the message to console. As mentioned in the link provided in your question, using System.out.println is more time consuming compared to printing data to log file.

通常,当应用程序在生产中时,日志级别将不会设置为DEBUG,因为将消息写入日志文件需要一些时间.

In general when the application is in production then the log level will not be set to DEBUG as writing messages to log files takes some time.

这篇关于Hibernate show sql等于Stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆