This is the 21st day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Recently I built a very simple project using Spring Boot, but I don’t know why the console keeps appearing

Thread starvation or clock leap detected (housekeeper delta = 3 h24s779ms457 including s999ns).
Copy the code

We all know that Spring Boot relies on Hikari by default, and my JDK version is 11. There is a problem with the default version of Spring Boot and the official recommended JDK version. For JDK 11, Hikari of 5.0.0 is recommended.

Hikari is introduced

Hikari is a connection pool that is fast, simple, reliable, zero-overhead, and very light. Well, that sounds great, right? It’s said to be the fastest connection pool ever.

I’m quoting an official quote

The HikariCP design aesthetic is Minimalism. In keeping with the simple is better or less is more design philosophy, some configuration axis are intentionally left out.

Hikari’s design aesthetic is a minimalist, less is more philosophy, and for that reason it deliberately reduces parameters, which really hits home. less is more and keep it simple and stupid.

On GitHub, the author even introduces his optimization points, which are small points, but it’s those small points that add up to make Hikari’s performance so great. I’m not going to go into the specifics, like caching, static methods, overwriting ArrayList…

The use of Hikari

For us developers, using Hikari is fairly straightforward. In my MySQL case, JDK 11 has Hikari 5.0.0 configured.

1 Introducing dependencies

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>5.0.0</version>
</dependency>
Copy the code

2 the initialization

I didn’t know there were so many initialization methods until I looked at the official website. I want to list them one by one to expand your thinking.

HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");
config.setUsername("bart");
config.setPassword("51mp50n");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
HikariDataSource ds = new HikariDataSource(config);
Copy the code

Or you could just go like this

HikariDataSource ds = new HikariDataSource(); ds.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons"); ds.setUsername("bart"); ds.setPassword("51mp50n"); .Copy the code

Or initialization based on configuration files

HikariConfig config = new HikariConfig("/some/path/hikari.properties");
HikariDataSource ds = new HikariDataSource(config);

Copy the code

Or just use the Properties class

Properties props = new Properties();
props.setProperty("dataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");
props.setProperty("dataSource.user", "test");
props.setProperty("dataSource.password", "test");
props.setProperty("dataSource.databaseName", "mydb");
props.put("dataSource.logWriter", new PrintWriter(System.out));

HikariConfig config = new HikariConfig(props);
HikariDataSource ds = new HikariDataSource(config);
Copy the code

You can even configure environment variables

There is also a System property available, hikaricp.configurationFile,
Copy the code

See above these initialization methods, straight call satisfying.

Description of common parameters in project configuration

As an example of my Spring Boot project, take a look at my configuration.

**

spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost/database? useUnicode=true&characterEncoding=utf8 username: root password: root hikari: minimum-idle: 10 idle-timeout: 30000 maximum-pool-size: 20 max-lifetime: 120000 connection-timeout: 30000Copy the code

According to the design philosophy of less is more, configurations that start with Hikari are optional and have default values.

  • AutoCommit: The default is true and automatically commits connections returned from the pool.

  • ConnectionTimeout: The maximum number of milliseconds to wait for a connection from the pool. The default value is 30000 ms = 30 s. The minimum allowed time is 250 ms.

  • IdleTimeout: maximum time that a connection is allowed to remain idle in the pool. The default value is 600,000, that is, 10 minutes. If idleTimeout + 1 second > maxLifetime and maxLifetime > 0, this will be reset to 0 (indicating never exit); If idleTimeout! If = 0 and less than 10 seconds, it is reset to 10 seconds. This parameter takes effect only when minimumIdle is smaller than maximumPoolSize. When the number of idle connections exceeds minimumIdle and the idle time exceeds idleTimeout, minimumIdle is removed.

  • KeepaliveTime: indicates the connection lifetime. The value must be smaller than maxLifetime. Keepalive “only happens on idle connections. When it is time to “keepalive “a given connection, it is removed from the pool. The minimum allowable value is 30000 ms (30 seconds), but the optimal value is in the minute range. Default value: 0

  • MaxLifetime: indicates the maximum lifetime of a connection in the pool. The default value is 1800000. If the value is not 0 and less than 30 seconds, the value is reset to 30 minutes. Setting this parameter is highly recommended.

  • MinimumIdle: Controls the minimum number of idle connections in the connection pool. When the number of idle connections in the connection pool is less than minimumIdle and the total number of connections is less than maximumPoolSize, HikariCP will try to add new connections. For performance reasons, it is not recommended to set this value. Instead, HikariCP treats the connection pool as fixed size. By default, minimumIdle is the same as maximumPoolSize. When minIdle < 0 or minIdle > maxPoolSize, the value is reset to maxPoolSize, which defaults to 10.

  • MaximumPoolSize: specifies the maximum number of connections in the pool, including idle and active connections. The default value is 10. If maxPoolSize is less than 1, it is reset. When minIdle <=0 is reset to DEFAULT_POOL_SIZE, it is 10. If minIdle > 0, reset the value to minIdle.

  • PoolName: The user-defined name of the connection pool, which appears primarily in the logging and JMX administrative console to identify the pool and pool configuration. The default value is hikaripool-1.

  • ReadOnly: Whether the connection obtained from the pool is in read-only mode by default. The default is false. Whether this property works depends on the implementation of the database.

  • ConnectionTestQuery: We strongly recommend not setting this property if your driver supports JDBC4. This is for “legacy” drivers that do not support the JDBC4 Connection.isValid() API. This is a query that is executed before a connection is given to you from the pool to verify that the connection to the database is still valid. Also, try running the database pool without this property, and if your driver does not meet the JDBC4 standard, HikariCP will log an error and let you know. Default value: none

The last

Idle-timeout is less than max-lifetime!!