在本文的第一部分中,我們學習瞭如何提高應用程式的效能,除了啟用和設定資料壓縮之外,還使用高效能Web 伺服器Undertow取代Tomcat,減少透過網路傳輸的HTTP響應的大小。
現在,我們將在持久化部分討論如何提高 Spring Boot 應用程式效能,但首先我們需要了解什麼是JPA、Hibernate和Hikari。
JPA 或Java Persistence API,後來更名為Jakarta Persistence,是一種 Java 語言標準,描述了資料持久化frameworks的通用介面。
JPA 規範在內部定義了物件關係映射,而不是依賴特定於供應商的映射實作。
Hibernate是ORMHibernate是ORM框架之一,它使JPA規範具體實現,也就是說,如果在這個規範中描述了需要方法persistfetch data,真正建構這些行為的是Hibernate,以及EclipseLink,這是另一個ORM
ORM光Hikari是一個連接池
框架,它負責管理與資料庫的連接,保持它們打開以便可以重用,換句話說,它是一個連接的
我們可能為提高效能而執行的配置如下:
使用application.yml:
使用 application.properties:
spring.datasource.hikari.auto-commit:如果為 false,連接池
回傳的每個連線都會停用spring.datasource.hikari.connection-timeout:客戶端等待來自
spring.datasource.hikari.maximum-pool-size:pool的最大大小,包括空閒和使用中的連接,決定資料庫的最大活動連接數。如果池達到此限制且沒有空閒連接,則對getConnection()
的呼叫將阻塞最多spring.datasource.hikari.pool-name: User-defined name for the connectionpooland appears primarily in registry management consoles andJMXto identifypoolsand their configurations.
spring.datasource.jpa.open-in-view: WhenOSIV(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 logging that is being executed in our application. We usually leave it enabled in development, but disabled in production.
spring.datasource.jpa.hibernate.ddl-auto: Configures the behavior ofHibernatein relation to the databaseschema. 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: InformsHibernatethat we have disabled theauto-commitof theproviders(PostgreSQL,MySQL, etc). This impacts performance, becauseHibernatewill need to get a connection from thepoolto know whetherauto-commitis enabled or not, for every transaction it 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 theHikarisettings likeauto-commitandpool size, theJPAandHibernatesettings likeOSIV(Open Session In View) and inform you that we have disabled theauto-commitof theproviders.
In the next part we will talk about exceptions and how they can be configured, to save resources of theJVM(Java Virtual Machine).
References:
以上是提高 Spring Boot 應用程式的效能 - 第二部分的詳細內容。更多資訊請關注PHP中文網其他相關文章!