jpa – Yon Labs http://www.yonlabs.com Yon Labs Wed, 07 Sep 2022 00:57:03 +0000 en-US hourly 1 https://wordpress.org/?v=6.2 http://www.yonlabs.com/wp-content/uploads/2021/01/yonita-square.png jpa – Yon Labs http://www.yonlabs.com 32 32 How to Detect Long Running Queries in Hibernate? http://www.yonlabs.com/2021/08/how-to-detect-long-running-queries-in-hibernate/ http://www.yonlabs.com/2021/08/how-to-detect-long-running-queries-in-hibernate/#respond Wed, 18 Aug 2021 00:28:00 +0000 http://www.yonlabs.com/?p=368 Read more

]]>
Most Java applications use Jakarta Persistence API (formerly Java Persistence API) to access databases. Hibernate is the most popular ORM framework for Java. It is a straightforward and easy-to-use implementation of JPA. However, its simplicity of usage often becomes mischievous to developers and leads to serious performance issues in Hibernate-based applications. How to detect such issues? The answer might lay in smart logging.

Logging Slow Queries

Hibernate supports logging queries that run longer than a specified amount of time in miliseconds. To configure this feature you need to use parameter hibernate.session.events.log.LOG_QUERIES_SLOWER_THAN_MS. This parameter can be very helpful in detecting bottlenecks in production systems without producing excessive number of logs.

Example:

hibernate.session.events.log.LOG_QUERIES_SLOWER_THAN_MS=1000

Logging Queries with Parameters

When we tune or debug our applications, it often happens that we are interested not only in executed SQL/JPQL/HQL queries but also in parameters bound for a given query. Actually, that may be a crucial piece of information! Unfortunately, the widely-known show-sql logs only SQL queries without bound parameters.

In order to log queries along with bound parameters, you can use:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

]]>
http://www.yonlabs.com/2021/08/how-to-detect-long-running-queries-in-hibernate/feed/ 0