In the first part of this article, we learned how to improve the performance of our applications, replacing Tomcat with Undertow, which is a high-performance web server, in addition to enabling and configuring data compression, to reduce the size of HTTP responses that travel over the network.
Now, we will talk about how to improve Spring Boot application performance in the persistence part, but first we need to understand what JPA, Hibernate and Hikari.
JPA or Java Persistence API, which was later renamed to Jakarta Persistence, is a Java language standard that describes a common interface for data persistence frameworks.
The JPA specification defines object relational mapping internally, rather than relying on vendor-specific mapping implementations.
Hibernate is one of the ORM frameworks that makes the concrete implementation of the JPA specification, In other words, if this specification describes the need for methods to persist, remove, update and fetch data, who is going to actually building these behaviors is Hibernate, as well as EclipseLink, which is another ORM .
Hikari is a connection pooling framework, which is responsible for managing connections to the database, keeping them open so that they can be reused, that is, it is a cache of connections for future requests, making access to the database faster and reducing the number of new connections to be created.
A configuration we may be performing to improve performance is as follows:
Using application.yml:
spring: hikari: auto-commit: false connection-timeout: 250 max-lifetime: 600000 maximum-pool-size: 20 minimum-idle: 10 pool-name: master jpa: open-in-view: false show-sql: true hibernate: ddl-auto: none properties: hibernate.connection.provider_disables_autocommit: true hibernate.generate_statistics: true
Using application.properties:
spring.datasource.hikari.auto-commit=false spring.datasource.hikari.connection-timeout=50 spring.datasource.hikari.max-lifetime=600000 spring.datasource.hikari.maximum-pool-size=20 spring.datasource.hikari.minimum-idle=10 spring.datasource.hikari.pool-name=master spring.datasource.jpa.open-in-view=false spring.datasource.jpa.show-sql=true spring.datasource.jpa.hibernate.ddl-auto=none spring.jpa.properties.hibernate.generate_statistics=true spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true
Now let's give a brief summary of the options:
spring.datasource.hikari.auto-commit: If false, every connection returned by the connection pool will come with auto-commit disabled.
spring.datasource.hikari.connection-timeout: Time, in milliseconds, that the client will wait for a connection from the pool. It is preferable to set a short timeout to fail quickly and return an error message, rather than keeping the client waiting indefinitely.
spring.datasource.hikari.max-lifetime: Maximum time that a connection can remain active. Configuring this parameter is crucial to avoid failures due to problematic connections and increase security, as connections that are active for a long time are more vulnerable to attacks.
spring.datasource.hikari.maximum-pool-size: Maximum size of the pool, including idle and in-use connections, determining the maximum number of active connections to the database. If the pool reaches this limit and there are no idle connections, calls to getConnection() will block for up to connectionTimeout milliseconds before failing.
spring.datasource.hikari.minimum-idle: Minimum number of connections that the pool maintains when demand is low. The pool can reduce connections up to 10 and recreate them as needed. However, for maximum performance and better response to demand spikes, it is recommended not to set this value, allowing Hikari to function as a fixed-size pool. Default: same as spring.datasource.hikari.maximum-pool-size.
spring.datasource.hikari.pool-name: User-defined name for the connection pool and appears primarily in registry management consoles and JMX to identify pools and their settings.
spring.datasource.jpa.open-in-view: When OSIV (Open Session In View) is enabled, a session is maintained throughout the request , even without the @Transactional annotation. This can cause performance problems, such as lack of application responses, as the session maintains the connection to the database until the end of the request.
spring.datasource.jpa.show-sql: Displays the SQL log that is being executed in our application. We generally leave it enabled in development, but disabled in production.
spring.datasource.jpa.hibernate.ddl-auto: Configures the behavior of Hibernate in relation to the database schema. It can have the following values:
spring.jpa.properties.hibernate.generate_statistics: Serves to collect detailed information about Hibernate, such as query execution times, number of queries executed, and other metrics.
spring.jpa.properties.hibernate.connection.provider_disables_autocommit: Informs Hibernate that we have disabled auto-commit of providers (PostgreSQL, MySQL, etc). This impacts performance because Hibernate will need to get a connection from the pool to know whether or not auto-commit is enabled or not. , for every transaction he makes.
With this, we close the second part of the article. Not all the settings present were about performance, but the ones that really impact are the Hikari settings like auto-commit and pool size , those of JPA and Hibernate like OSIV (Open Session In View) and inform you that we have disabled auto-commit from providers.
In the next part we will talk about exceptions and how they can be configured, to save JVM (Java Virtual Machine) resources.
References:
The above is the detailed content of Improving the performance of Spring Boot applications - Part II. For more information, please follow other related articles on the PHP Chinese website!