Hibernate统计日志记录

问题描述 投票:3回答:1

我有点卡住解释hibernate会话日志。我的主要问题是许多查询都很慢 - 基于我实现的一些TimeWatch记录。为了进一步追踪问题,我已经启用了hibernate会话日志记录,目的是查看执行查询或获取连接的时间是否丢失(这意味着错误配置,我猜)。

关于用例的一点点 - Oracle DB,Spring,Hibernate。在“繁忙时期”,有一个最大值。 15个线程对数据库执行查询。所以没什么特别的,我猜。

现在我看到hibernate会话日志就像。

2017-03-30 13:35:13.834+0200 [process-documents-task-6] I [/] o.h.e.i.StatisticalLoggingSessionEventListener - Session Metrics {
    636713687 nanoseconds spent acquiring 1 JDBC connections;
    57993 nanoseconds spent releasing 1 JDBC connections;
    636859879 nanoseconds spent preparing 1 JDBC statements;
    2231526 nanoseconds spent executing 1 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    9261 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)

要么

2017-03-30 13:35:16.073+0200 [process-documents-task-8] I [/] o.h.e.i.StatisticalLoggingSessionEventListener - Session Metrics {
    2893793341 nanoseconds spent acquiring 1 JDBC connections;
    22196 nanoseconds spent releasing 1 JDBC connections;
    2893869403 nanoseconds spent preparing 1 JDBC statements;
    1509926 nanoseconds spent executing 1 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    4056 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)

但究竟是什么意思呢?

我现在解释的是什么

  • 执行查询的时间为1509926纳秒(1.5毫秒):似乎没问题
  • 2893793341纳秒获取连接(2.8秒):不行:(
  • 准备声明2893869403纳秒(2.8秒):不行:(

准备声明是什么意思?根据我从javadoc中读取的内容,它可能意味着将查询发送到数据库进行优化。但为什么它总是在获取连接需求的时候呢?

关于如何追查问题的任何建议,根本原因更进一步?

或任何有关问题的提示?

谢谢你的帮助!

最好的问候,Stefan

oracle hibernate session logging jdbc
1个回答
0
投票

默认的Hibernate统计信息不是很有用。我们计划在未来的Hibernate版本中增强这一方面。

但是,正如我在my book中所解释的那样,您可以提供基于Statistics构建的自己的Dropwizard Metrics实现。

简而言之,假设你有一个扩展TransactionStatisticsorg.hibernate.stat.internal.ConcurrentStatisticsImpl类:

public class TransactionStatistics extends ConcurrentStatisticsImpl {

    private static final ThreadLocal<AtomicLong> startNanos = new ThreadLocal<AtomicLong>() {
        @Override protected AtomicLong initialValue() {
            return new AtomicLong();
        }
    };

    private static final ThreadLocal<AtomicLong> connectionCounter = new ThreadLocal<AtomicLong>() {
        @Override protected AtomicLong initialValue() {
            return new AtomicLong();
        }
    };

    private StatisticsReport report = new StatisticsReport();

    @Override public void connect() {
        connectionCounter.get().incrementAndGet();
        startNanos.get().compareAndSet(0, System.nanoTime());
        super.connect();
    }

    @Override public void endTransaction(boolean success) {
        try {
            report.transactionTime(System.nanoTime() - startNanos.get().get());
            report.connectionsCount(connectionCounter.get().get());
            report.generate();
        } finally {
            startNanos.remove();
            connectionCounter.remove();
        }
        super.endTransaction(success);
    }
}

您需要创建一个StatisticsFactory实现:

public class TransactionStatisticsFactory implements StatisticsFactory {

    @Override
    public StatisticsImplementor buildStatistics(SessionFactoryImplementor sessionFactory) {
        return new TransactionStatistics();
    }
}

并配置如下:

就是这样!

现在,您可以监视统计数据并将其导出为Dropwizard Metrics支持的任何格式。而且,Dropwizard Metrics使用各种水库,因此您可以选择最适合您的用例。

要查看Dropwizard指标的真正力量,请查看FlexyPool

© www.soinside.com 2019 - 2024. All rights reserved.