@Override public Connection getConnection() throws SQLException { ConnectionAcquisitionProfiler profiler = new ConnectionAcquisitionProfiler(); try { return acquirePooledConnection(profiler); } finally { profiler.logQueryDuration(); } }
private State normalState() throws SQLException { HikariDataSource dataSourcePool = getDataSourcePool(); boolean keep = false; try { // Setup monitoring HikariPoolMXBean poolProxy = initPoolMbeans(); testDataSource(dataSourcePool); keep = true; return new State(StateType.NORMAL, dataSourcePool, poolProxy, null); } finally { if (!keep) { IOUtils.closeQuietly(dataSourcePool); } } }
/** * 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; } } } }
public static ConnectionManagerAwareDbKvs create(DbKeyValueServiceConfig config) { HikariCPConnectionManager connManager = new HikariCPConnectionManager(config.connection()); ReentrantManagedConnectionSupplier connSupplier = new ReentrantManagedConnectionSupplier(connManager); SqlConnectionSupplier sqlConnSupplier = getSimpleTimedSqlConnectionSupplier(connSupplier); return new ConnectionManagerAwareDbKvs(DbKvs.create(config, sqlConnSupplier), connManager, sqlConnSupplier); }
/** * Initializes a connection to the provided database. */ @Override public synchronized void init() throws SQLException { if (state.type == StateType.ZERO) { state = normalState(); } }
/** * 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; } } } }
@Before public void initConnectionManager() { manager = new HikariCPConnectionManager(createConnectionConfig(3)); }
private HikariDataSource checkAndGetDataSourcePool() throws SQLException { while (true) { // Volatile read state to see if we can get through here without locking. State stateLocal = state; switch (stateLocal.type) { case ZERO: // Not yet initialized, no such luck. synchronized (this) { if (state == stateLocal) { // The state hasn't changed on us, we can perform // the initialization and start over again. state = normalState(); } else { // Someone else changed the state on us, just start over. } } break; case NORMAL: // Normal state, good to go return stateLocal.dataSourcePool; case CLOSED: throw new SQLException("Hikari connection pool already closed!", stateLocal.closeTrace); } // fall throughs are spins } }
private State normalState() throws SQLException { HikariDataSource dataSourcePool = getDataSourcePool(); boolean keep = false; try { // Setup monitoring HikariPoolMXBean poolProxy = initPoolMbeans(); testDataSource(dataSourcePool); keep = true; return new State(StateType.NORMAL, dataSourcePool, poolProxy, null); } finally { if (!keep) { IOUtils.closeQuietly(dataSourcePool); } } }
/** * 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; } }
public static ConnectionManagerAwareDbKvs create(DbKeyValueServiceConfig config) { HikariCPConnectionManager connManager = new HikariCPConnectionManager(config.connection()); ReentrantManagedConnectionSupplier connSupplier = new ReentrantManagedConnectionSupplier(connManager); SqlConnectionSupplier sqlConnSupplier = getSimpleTimedSqlConnectionSupplier(connSupplier); return new ConnectionManagerAwareDbKvs(DbKvs.create(config, sqlConnSupplier), connManager, sqlConnSupplier); }
/** * Initializes a connection to the provided database. */ @Override public synchronized void init() throws SQLException { if (state.type == StateType.ZERO) { state = normalState(); } }
@Override public Connection getConnection() throws SQLException { ConnectionAcquisitionProfiler profiler = new ConnectionAcquisitionProfiler(); try { return acquirePooledConnection(profiler); } finally { profiler.logQueryDuration(); } }
private HikariDataSource checkAndGetDataSourcePool() throws SQLException { while (true) { // Volatile read state to see if we can get through here without locking. State stateLocal = state; switch (stateLocal.type) { case ZERO: // Not yet initialized, no such luck. synchronized (this) { if (state == stateLocal) { // The state hasn't changed on us, we can perform // the initialization and start over again. state = normalState(); } else { // Someone else changed the state on us, just start over. } } break; case NORMAL: // Normal state, good to go return stateLocal.dataSourcePool; case CLOSED: throw new SQLException("Hikari connection pool already closed!", stateLocal.closeTrace); } // fall throughs are spins } }