【问题标题】:Hibernate show sql is equal to StdoutHibernate show sql 等于 Stdout
【发布时间】:2014-11-18 21:14:13
【问题描述】:

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

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 如何在内部工作?

【问题讨论】:

    标签: hibernate jboss7.x


    【解决方案1】:

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

    来自documentation

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

    例如真实 |假的

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

    Hibernate 使用以下方法显示来自SQLStatementLogger class 的 SQL 语句,它在内部使用。

    /**
     * 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语句,否则将不应用格式化样式。

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

    一般来说,当应用程序处于生产状态时,日志级别不会设置为DEBUG,因为将消息写入日志文件需要一些时间。

    【讨论】:

    • " 将 SQL 语句写入控制台。"但它也需要一些毫秒来编写。
    • @ashokhein,用更多细节更新了我的答案,以使其更清楚,请检查。
    【解决方案2】:

    当show_sql为ture时,语句会打印到system.out;如果要将语句打印到文件 log4j 集中,则应将 log4j.logger.org.hibernate.SQL 的级别设置为 DEBUG

    【讨论】:

      猜你喜欢
      • 2011-10-25
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多