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;
the time:
elapsedTimeInSeconds1 -> 76
elapsedTimeInSeconds2 -> 0
elapsedTimeInSeconds3 -> 0
elapsedTimeInSeconds4 -> 0
Is it normal? Is there is something I can do configuring the Hikari pool to optimize this?
the 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>
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
Spring Boot uses HikariCP as the default connection pool, due to its remarkable performance and enterprise-ready features.
Hikari is a JDBC DataSource implementation that provides a connection pooling mechanism. Compared to other implementations, it promises to be lightweight and better performing.
The default is 600000 milliseconds, or 10 minutes. If idleTimeout+1 second > maxLifetime and maxLifetime>0, it will be reset to 0; If idleTimeout! =0 and less than 10 seconds, it will be reset to 10 seconds. If idleTimeout=0, idle connections will never be removed from the connection pool.
You should follow Hikari's MySQL Configuration:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With