Category: Database, github

Thorben Janssen August 6, 2021 Developer Tips, Tricks & ResourcesThe Java Persistence API (JPA) is used in most Java applications to interact with a relational database. One of its most popular implementations is the Hibernate ORM, because it uses object-relational mapping to abstract database interactions and makes implementing simple CRUD operations very simple.

You can analyze your database logs, activate Hibernate’s internal logging, use a JDBC data source that logs all executed statements or use an external tool like Retrace to monitor your system.All of these options can help you better understand how Hibernate interacts with the database.

Hibernate uses the configured logging framework to write the messages to the log file, and you can change it at any time without changing your deployed application.You can see an example of a log4j configuration in the following code snippet, and you can fork the project with this and all following code snippets on github.When you activate this logging configuration, Hibernate will write a log message for each executed SQL statement.The logging of all executed queries provides basic information about all database interactions, but Hibernate can do a lot more.If you enable the Hibernate statistics component, it measures how long it takes to execute a query and summarizes the executed queries and execution times at the end of each session.

Hibernate will then write a log message with the SQL statement, the number of returned rows and each query’s execution time to the log file.With these settings, Hibernate provides you a lot of information that can help you find potential performance issues before they cause problems in production.A potential downside of this approach is identifying all log statements that were triggered by a specific use case.

Related Articles