【问题标题】:Spring boot application.properties hibernate.generate_statisticsSpring boot application.properties hibernate.generate_statistics
【发布时间】:2017-08-07 08:42:33
【问题描述】:

遵循各种示例并阅读 Spring Boot 文档后,我仍然无法启用休眠统计信息。我在 application.properties 文件中设置属性。通过集成测试或Application.java 运行 Spring Boot 应用程序不会产生任何统计信息。

我的application.properties 文件。

    spring.datasource.jdbcUrl=jdbc:postgresql://localhost:5432/postgres
    spring.datasource.username = postgres
    spring.datasource.password = password

    spring.datasource.driver-class-name=org.postgresql.Driver
    spring.datasource.platform=postgres

    spring.datasource.testWhileIdle = true
    spring.datasource.validationQuery = SELECT 1

    spring.jpa.properties.hibernate.show_sql=true
    spring.jpa.properties.hibernate.format_sql=true
    spring.jpa.properties.hibernate.generate_statistics=true

    logging.level.org.hibernate.SQL=TRACE
    logging.level.org.hibernate.stat=TRACE

    logging.file=transaction-app.log

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

还有我的集成测试

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SqlMappingTest {

    @Autowired
    PlanService planService;

    @org.junit.Test
    public void findAll() {
        List<Plan> plans = planService.findAll();
        Assert.assertEquals(1, plans.size());

    }
}

我错过了什么?

【问题讨论】:

    标签: spring hibernate spring-boot spring-data-jpa


    【解决方案1】:

    它适用于我(使用 Spring Boot 2.1.6.RELEASE)。

    a) 在文件 application.properties 中:

    spring.jpa.properties.hibernate.generate_statistics=true
    

    b) 确保“org.hibernate.stat”的运行时调试级别至少为 DEBUG:

    curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "DEBUG"}' http://localhost:8080/actuator/loggers/org.hibernate.stat
    

    c) 签入日志如下:

    2019-07-03 21:27:27.739 DEBUG 11797 --- [nio-8080-exec-3] o.h.stat.internal.StatisticsImpl         : HHH000117: HQL: select count(*) from AppRequest x WHERE x.uuid = :uuid, time: 1ms, rows: 1    
    

    【讨论】:

    • 嘿,谢谢。在我意识到我的 app.properties 文件中需要 spring.jpa.properties.hibernate.generate_statistics 而不仅仅是 hibernate.generate_statistics 之前,我查看了六个答案。
    【解决方案2】:

    hibernate.properties 文件中定义属性hibernate.generate_statistics=true

    【讨论】:

      【解决方案3】:

      不确定以下是否正确,但可以正常工作。 我正在通过构造将属性强制到我的LocalContainerEntityManagerFactoryBean 上。

      DatabaseConfig sn-p:

          @Configuration
          @EnableTransactionManagement
          @ConfigurationProperties(prefix = "spring.datasource")
          public class DatabaseConfig extends HikariConfig {
      
              @Autowired
              @Bean
              public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env) {
                  LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
      
                  entityManagerFactory.setDataSource(dataSource);
      
                  // Classpath scanning of @Component, @Service, etc annotated class
                  entityManagerFactory.setPackagesToScan(new String[]{"com.test"});
      
                  // Vendor adapter
                  HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
                  entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
      
                  Properties jpaProperties = new Properties();
      
                  jpaProperties.put("hibernate.show_sql",
                          env.getRequiredProperty("spring.jpa.properties.hibernate.show_sql")
                  );
      
                  jpaProperties.put("hibernate.format_sql",
                          env.getRequiredProperty("spring.jpa.properties.hibernate.format_sql")
                  );
                  jpaProperties.put("hibernate.generate_statistics",
                          env.getRequiredProperty("spring.jpa.properties.hibernate.generate_statistics")
                  );
      
                  entityManagerFactory.setJpaProperties(jpaProperties);
                  return entityManagerFactory;
              }
          }
      

      配置后

      2017-03-16 11:41:02.881 DEBUG 7884 --- [main] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000117: HQL: select up from UserPlan up, time: 48ms, rows: 1
      2017-03-16 11:41:02.885  INFO 7884 --- [main] i.StatisticalLoggingSessionEventListener : Session Metrics {
          939077 nanoseconds spent acquiring 1 JDBC connections;
          0 nanoseconds spent releasing 0 JDBC connections;
          2848386 nanoseconds spent preparing 1 JDBC statements;
          12205063 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);
          18693 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
      }
      

      【讨论】:

      • 为了完整性,所有影响休眠的不同来源的设置都可以通过类路径“org.hibernate.cfg.Environment”访问。
      猜你喜欢
      • 2020-04-18
      • 1970-01-01
      • 2019-03-18
      • 2016-03-04
      • 2021-02-07
      • 2016-11-27
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多