• HouseKeeper timing check idle connection, for the part of DB connection beyond minimumIdle, if idle time is greater than idleTimeout, shut downsoftEvictConnection()
  • The maximum lifetime of a DB connection exceeds maxLifeTimesoftEvictConnection()Remove the connection
  • Check the validity of the connection every keepaliveTime, if the DB connection is not available, it is directly closedcloseConnection
  • HikariPool#getConnection() if the connection is not used for more than 500ms and the connection availability failsreserse() + closeConnection()
  • The program explicitly calls HikariDataSource#evictConnection HikariPool#evict Connection
  • Data source HikariDataSource#close() closes, HikariPool#shutdown() closes all connections

Hikari closes a connection if it is currently idle (STATE_IN_USE) or reserved (STATE_RESERVED)

HikariPool#closeConnection directly closes the connection logic

void closeConnection(final PoolEntry poolEntry, final String closureReason)
  {
			// Remove poolEntry from the pool if the poolEntry state is STATE_IN_USE or STATE_RESERVED and the state changes to MOVED
      if (connectionBag.remove(poolEntry)) {
				 // Get the physical connection and give it to the thread pool responsible for closing the DB connection to close
         final Connection connection = poolEntry.close();
         closeConnectionExecutor.execute(() -> {
            quietlyCloseConnection(connection, closureReason);
						// trigger the logic to populate the connection
            if(poolState == POOL_NORMAL) { fillPool(); }}); }}Copy the code

HouseKeeper and maxLifetime such detection tasks to shut down threads, the need to ensure that the DB connection is free, is called softEvictConnection floppy drive method

private boolean softEvictConnection(final PoolEntry poolEntry, final String reason, final boolean owner)
{ **poolEntry.markEvicted(); **if (owner || connectionBag.reserve(poolEntry)) {
      closeConnection(poolEntry, reason);
      return true;
   }
 
   return false;
}
Copy the code

Connectionbag. reserve(poolEntry) The connectionbag. reserve(poolEntry) is executed successfully to change its state to RESERVED, and closeConnection is executed directly. Otherwise, the connection with EVICT =true will be closed when another thread applies for db connection

  • Hikari closes a connection if it is currently idle (STATE_IN_USE) or reserved (STATE_RESERVED)
  • Call after the physical connection is closedfillPool();Maintain the number of connection pools