Spring Boot2.Hikari连接池优化 [英] Spring Boot 2. Hikari Connection Pool optimization
问题描述
我有一个SpringBoot应用程序,我在控制器中进行了一些性能测试,并且我意识到,无论我对控制器进行的第一个查询是什么,与其他查询相比都要花些时间...(DB是一个远程连接) ,但我无法更改)
I have a SpringBoot app, I was making some performance test in the controller, and I realized that whatever is the first query I put the controller, It take ages compare to the others... (ths DB is a remote connection, but I can't change this)
long t1 = System.nanoTime();
menuPriceSummaryService.findAllVegan().stream();
long t2 = System.nanoTime();
long elapsedTimeInSeconds = (t2 - t1) / 1000000000;
System.out.println("elapsedTimeInSeconds1 -> " + elapsedTimeInSeconds);
t1 = System.nanoTime();
menuPriceSummaryService.findAllVegan();
t2 = System.nanoTime();
elapsedTimeInSeconds = (t2 - t1) / 1000000000;
System.out.println("elapsedTimeInSeconds2 -> " + elapsedTimeInSeconds);
t1 = System.nanoTime();
menuPriceSummaryService.findAllVegan().parallelStream();
t2 = System.nanoTime();
elapsedTimeInSeconds = (t2 - t1) / 1000000000;
System.out.println("elapsedTimeInSeconds3 -> " + elapsedTimeInSeconds);
t1 = System.nanoTime();
menuPriceSummaryService.findAllVegan().parallelStream().filter(this::notInMyFavourites);
t2 = System.nanoTime();
elapsedTimeInSeconds = (t2 - t1) / 1000000000;
时间:
elapsedTimeInSeconds1 -> 76
elapsedTimeInSeconds2 -> 0
elapsedTimeInSeconds3 -> 0
elapsedTimeInSeconds4 -> 0
正常吗? 我可以做一些事情来配置Hikari池来对此进行优化吗?
Is it normal? Is there is something I can do configuring the Hikari pool to optimize this?
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
application.properties:
the application.properties:
spring.datasource.url=jdbc:mysql://elcordelaciutat.awob1oxhu1so.eu-central-1.rds.amazonaws.com:3306/elcor
spring.datasource.username=elcor
spring.datasource.password=elcor2#$
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
推荐答案
您应遵循Hikari的 MySQL配置:
You should follow Hikari's MySQL Configuration:
用于HikariCP的典型MySQL配置可能看起来像这样:
A typical MySQL configuration for HikariCP might look something like this:
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
dataSource.useServerPrepStmts=true
dataSource.useLocalSessionState=true
dataSource.useLocalTransactionState=true
dataSource.rewriteBatchedStatements=true
dataSource.cacheResultSetMetadata=true
dataSource.cacheServerConfiguration=true
dataSource.elideSetAutoCommits=true
dataSource.maintainTimeStats=false
这篇关于Spring Boot2.Hikari连接池优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!