search
HomeJavajavaTutorialImproving the performance of Spring Boot applications - Part II

Melhorando o desempenho de aplicações Spring Boot - Parte II

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

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

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

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.

Configuring Hikari, JPA and Hibernate

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:

Hikari

  • 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.

    • Finding a suitable value is important, as many people think they will get great performance by setting 50, 70 or even 100. The ideal is to have a maximum of 20, which is the number of threads in parallel using connections.
    • The higher the value, the more difficult it will be for the database to manage these connections and most likely we will not be able to have enough throughput to use all these connections.
    • It is important to understand that from the point of view of the RDBMS (Relational Database Management System) it is difficult to maintain an open connection with itself, imagine n number of connections.
  • 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.

JPA

  • 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:

    • none: Does nothing. We manually manage the bank's schema.
    • validate: Validates the database schema, but makes no changes. This is useful to ensure that the current schema agrees with the entities we have mapped.
    • update: Updates the database schema to reflect changes in entities.
    • create: Creates the database schema. If the schema already exists, it will remove and create it again.
    • create-drop: Creates the schema from the database and, when the application ends, removes the schema. Useful for testing, where we want a clean database with each test.
  • 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:

  • https://en.wikipedia.org/wiki/Jakarta_Persistence
  • https://www.ibm.com/docs/pt-br/was/8.5.5?topic=SSEQTP_8.5.5/com.ibm.websphere.nd.multiplatform.doc/ae/cejb_persistence.htm
  • https://github.com/brettwooldridge/HikariCP
  • https://github.com/corona-warn-app/cwa-server/issues/556
  • https://medium.com/@rafaelralf90/open-session-in-view-is-evil-fd9a21645f8e

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How can I use Java's RMI (Remote Method Invocation) for distributed computing?How can I use Java's RMI (Remote Method Invocation) for distributed computing?Mar 11, 2025 pm 05:53 PM

This article explains Java's Remote Method Invocation (RMI) for building distributed applications. It details interface definition, implementation, registry setup, and client-side invocation, addressing challenges like network issues and security.

How do I use Java's sockets API for network communication?How do I use Java's sockets API for network communication?Mar 11, 2025 pm 05:53 PM

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i

How can I create custom networking protocols in Java?How can I create custom networking protocols in Java?Mar 11, 2025 pm 05:52 PM

This article details creating custom Java networking protocols. It covers protocol definition (data structure, framing, error handling, versioning), implementation (using sockets), data serialization, and best practices (efficiency, security, mainta

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.