/** * Test an initialized dataSourcePool by running a simple query. * @param dataSourcePool data source to validate * @throws SQLException if data source is invalid and cannot be used. */ private void testDataSource(HikariDataSource dataSourcePool) throws SQLException { try { try (Connection conn = dataSourcePool.getConnection()) { testConnection(conn); } } catch (SQLException e) { logConnectionFailure(); throw e; } }
/** * Attempts to acquire a valid database connection from the pool. * @param profiler Connection acquisition profiler for this attempt to acquire a connection * @return valid pooled connection * @throws SQLException if a connection cannot be acquired within the {@link ConnectionConfig#getCheckoutTimeout()} */ private Connection acquirePooledConnection(ConnectionAcquisitionProfiler profiler) throws SQLException { while (true) { HikariDataSource dataSourcePool = checkAndGetDataSourcePool(); Connection conn = dataSourcePool.getConnection(); profiler.logAcquisitionAndRestart(); try { testConnection(conn); profiler.logConnectionTest(); return conn; } catch (SQLException e) { log.error("[{}] Dropping connection which failed validation", connConfig.getConnectionPoolName(), e); dataSourcePool.evictConnection(conn); if (profiler.globalStopwatch.elapsed(TimeUnit.MILLISECONDS) > connConfig.getCheckoutTimeout()) { // It's been long enough that had hikari been // validating internally it would have given up rather // than retry throw e; } } } }
/** * Test an initialized dataSourcePool by running a simple query. * @param dataSourcePool data source to validate * @throws SQLException if data source is invalid and cannot be used. */ private void testDataSource(HikariDataSource dataSourcePool) throws SQLException { try { try (Connection conn = dataSourcePool.getConnection()) { testConnection(conn); } } catch (SQLException e) { logConnectionFailure(); throw e; } }
/** * Attempts to acquire a valid database connection from the pool. * @param profiler Connection acquisition profiler for this attempt to acquire a connection * @return valid pooled connection * @throws SQLException if a connection cannot be acquired within the {@link ConnectionConfig#getCheckoutTimeout()} */ private Connection acquirePooledConnection(ConnectionAcquisitionProfiler profiler) throws SQLException { while (true) { HikariDataSource dataSourcePool = checkAndGetDataSourcePool(); Connection conn = dataSourcePool.getConnection(); profiler.logAcquisitionAndRestart(); try { testConnection(conn); profiler.logConnectionTest(); return conn; } catch (SQLException e) { log.error("[{}] Dropping connection which failed validation", connConfig.getConnectionPoolName(), e); dataSourcePool.evictConnection(conn); if (profiler.globalStopwatch.elapsed(TimeUnit.MILLISECONDS) > connConfig.getCheckoutTimeout()) { // It's been long enough that had hikari been // validating internally it would have given up rather // than retry throw e; } } } }