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 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.
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.
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.
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 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.
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に表示されます。
(ビューでセッションを開く) が有効な場合、@Transactional アノテーションがなくても、リクエスト全体を通じてセッションが維持されます。セッションはリクエストが終了するまでデータベースへの接続を維持するため、アプリケーションの応答がなくなるなど、パフォーマンスの問題が発生する可能性があります。
に関連したHibernateの動作を設定します。次の値を指定できます:
none: 何もしません。銀行スキーマは手動で管理します。spring.jpa.properties.hibernate.connection.provider_disables_autocommit:
プロバイダ、MySQLなど)の自動コミットを無効にしたことをHibernateに通知します。Hibernateは、トランザクションごとにauto-commitが有効かどうかを知るためにpoolから接続を取得する必要があるため、これはパフォーマンスに影響します。これで記事の後半を終わります。存在するすべての設定がパフォーマンスに関するものではありませんが、実際に影響を与えるのは、自動コミットや
プールサイズHikari設定、JPAやHibernate設定です。 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/556The 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!