After following various examples and reading the spring boot documentation I'm still unable to enable hibernate statistics. I'm setting the property in the application.properties file. Running the spring boot application via a integration test or Application.java
produces no stats.
My application.properties
file.
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>
And my integration test
@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());
}
}
What am I missing?
To test hibernate configuration with Spring boot, we need to autowire the EmployeeRepository dependency in a class and use it's method to save or fetch employee entities. Let's do this testing in @SpringBootApplication annotated class and using CommandLineRunner interface.
If you enable Hibernate statistics by setting the property hibernate. generate_statistics to true, Hibernate will expose a number of useful metrics. The page "Statistics" is used to expose these metrics.
By default, Spring uses Hibernate as the default JPA vendor. you can see hibernate related dependency in dependency hierarchy under the pom. xml, this will resolve all dependencies of your project.
It works for me (using Spring Boot 2.1.6.RELEASE).
a) In file application.properties:
spring.jpa.properties.hibernate.generate_statistics=true
b) Ensure runtime debug level for 'org.hibernate.stat' is at least DEBUG:
curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "DEBUG"}' http://localhost:8080/actuator/loggers/org.hibernate.stat
c) Check in log something like:
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
Not sure if the below is correct, but got it working.
I'm forcing the properties onto my LocalContainerEntityManagerFactoryBean
with construction.
DatabaseConfig snippet:
@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;
}
}
After the config
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With