Home> Java> javaTutorial> body text

Improving the performance of Spring Boot applications - Part II

PHPz
Release: 2024-08-28 06:35:06
Original
388 people have browsed it

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, replacingTomcatwithUndertow, which is a high-performance web server, in addition to enabling and configuring data compression, to reduce the size of HTTP responses traveling across the network.

Now, we will talk about how to improve Spring Boot application performance in the persistence part, but first we need to understand whatJPA,HibernateandHikari.

JPA

JPA orJava Persistence API, which was later renamed toJakarta Persistence, is a Java language standard that describes a common interface for data persistenceframeworks.

TheJPA specificationdefines object relational mapping internally, rather than relying on vendor-specific mapping implementations.

Hibernate

TheHibernateis one of theORMframeworks that makes the concrete implementation of theJPAspecification, that is, if in this specification it is described that methods are needed topersist,remove,updateandfetch data, the one who will actually build these behaviors isHibernate, as well asEclipseLink, which is anotherORM.

Hikari

Hikariis aconnection poolingframework, which is responsible for managing connections to the database, keeping them open so they can be reused, in other words, it is acacheof connections for requests future ones, making database access 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
Copy after login
e

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
Copy after login
e

Now let's give a brief summary of the options:

Hikari

  • spring.datasource.hikari.auto-commit: If false, every connection returned by theconnection poolwill come withauto-commitdisabled.

  • spring.datasource.hikari.connection-timeout: Time, in milliseconds, that the client will wait for a connection from thepool. 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 a connection can remain active. Configuring this parameter is crucial to avoid failures due to problematic connections and increase security, as connections that have been active for a long time are more vulnerable to attacks.

  • spring.datasource.hikari.maximum-pool-size: Maximum size of thepool, 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 togetConnection()will block for up toconnectionTimeoutmilliseconds 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 ofthreadsin parallel using the 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 enoughthroughputto use all these connections.
    • It is important to understand that from the point of view of theRDBMS(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 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: 接続poolのユーザー定義名。主に、プールとその構成を識別するためにレジストリ管理コンソールとJMXに表示されます。

  • JPA

    spring.datasource.jpa.open-in-view:
  • OSIV

    (ビューでセッションを開く) が有効な場合、@Transactional アノテーションがなくても、リクエスト全体を通じてセッションが維持されます。セッションはリクエストが終了するまでデータベースへの接続を維持するため、アプリケーションの応答がなくなるなど、パフォーマンスの問題が発生する可能性があります。

  • spring.datasource.jpa.show-sql: アプリケーションで実行されている SQL ログを表示します。通常、開発時は有効のままにしておきますが、本番環境では無効にします。
  • spring.datasource.jpa.hibernate.ddl-auto: データベース
  • スキーマ

    に関連したHibernateの動作を設定します。次の値を指定できます:

    none: 何もしません。銀行スキーマは手動で管理します。
    • validate: データベース
    • スキーマ
    • を検証しますが、変更は行いません。これは、現在のスキーマがマッピングしたエンティティと確実に一致するようにするのに役立ちます。update: エンティティの変更を反映するためにデータベーススキーマ
    • を更新します。
    • create: データベーススキーマを作成します。
    • スキーマ
    • がすでに存在する場合は、削除して再度作成します。create-drop: データベースからスキーマを作成し、アプリケーションの終了時に
    • スキーマ
    • を削除します。各テストでクリーンなデータベースが必要なテストに役立ちます。
    • spring.jpa.properties.hibernate.generate_statistics: クエリの実行時間、実行されたクエリの数、その他のメトリクスなど、Hibernate に関する詳細情報を収集するのに役立ちます。
  • spring.jpa.properties.hibernate.connection.provider_disables_autocommit:

    プロバイダ
  • (
  • PostgreSQL

    MySQLなど)の自動コミットを無効にしたことをHibernateに通知します。Hibernateは、トランザクションごとにauto-commitが有効かどうかを知るためにpoolから接続を取得する必要があるため、これはパフォーマンスに影響します。これで記事の後半を終わります。存在するすべての設定がパフォーマンスに関するものではありませんが、実際に影響を与えるのは、自動コミット

    プールサイズ
  • などの

Hikari設定、JPAHibernate設定です。 OSIV(ビューでセッションを開く)プロバイダー自動コミットを無効にしたことをお知らせします。次のパートでは、例外と、JVM(Java 仮想マシン) のリソースを節約するための例外の設定方法について説明します。参考文献:

https://en.wikipedia.org/wiki/Jakarta_Persistencehttps://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!

source:dev.to
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!