SpringBootの接続プールの設定方法

Spring Bootでは、application.properties あるいは application.yml ファイルを使用して接続プールを設定できます。以下は、application.propertiesファイルを使用して接続プールを設定する際の例です。1. (もしまだ追加していない場合) 以下の依存関係をプロジェクトに追加します:

spring-boot-starter-data-jpa
spring-boot-starter-web

2. src/main/resources ディレクトリに application.properties ファイルを作成します。3. application.properties ファイルに次の接続プール構成を追加します。

spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.initial-size=5
spring.datasource.max-active=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.validation-query=SELECT 1
spring.datasource.test-on-borrow=true
spring.datasource.test-while-idle=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

また、application.ymlファイルを使用して設定することもできます、application.ymlファイルに上記のプロパティを追加するだけです、上記の例ではMySQLデータベースを使用しましたが、必要に応じて他のデータベースに変更することができます、これは単なる簡単な設定の例です、実際のニーズに応じて調整することができます。

bannerAds