com.zaxxer.hikari.HikariPoolMXBean Java Examples

The following examples show how to use com.zaxxer.hikari.HikariPoolMXBean. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConnectionManagerTest.java    From pxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testPoolExpirationNoActiveConnections() throws SQLException {
    MockTicker ticker = new MockTicker();
    ConnectionManager.DataSourceFactory mockFactory = mock(ConnectionManager.DataSourceFactory.class);
    HikariDataSource mockDataSource = mock(HikariDataSource.class);
    when(mockFactory.createDataSource(anyObject())).thenReturn(mockDataSource);
    when(mockDataSource.getConnection()).thenReturn(mockConnection);

    HikariPoolMXBean mockMBean = mock(HikariPoolMXBean.class);
    when(mockDataSource.getHikariPoolMXBean()).thenReturn(mockMBean);
    when(mockMBean.getActiveConnections()).thenReturn(0);
    manager = new ConnectionManager(mockFactory, ticker, ConnectionManager.CLEANUP_SLEEP_INTERVAL_NANOS);

    manager.getConnection("test-server", "test-url", connProps, true, poolProps, null);

    ticker.advanceTime(ConnectionManager.POOL_EXPIRATION_TIMEOUT_HOURS + 1, TimeUnit.HOURS);
    manager.cleanCache();

    Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);

    verify(mockMBean, times(1)).getActiveConnections();
    verify(mockDataSource, times(1)).close(); // verify datasource is closed when evicted
}
 
Example #2
Source File: ConnectionManagerTest.java    From pxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testPoolExpirationWithActiveConnections() throws SQLException {
    MockTicker ticker = new MockTicker();
    ConnectionManager.DataSourceFactory mockFactory = mock(ConnectionManager.DataSourceFactory.class);
    HikariDataSource mockDataSource = mock(HikariDataSource.class);
    when(mockFactory.createDataSource(anyObject())).thenReturn(mockDataSource);
    when(mockDataSource.getConnection()).thenReturn(mockConnection);

    HikariPoolMXBean mockMBean = mock(HikariPoolMXBean.class);
    when(mockDataSource.getHikariPoolMXBean()).thenReturn(mockMBean);
    when(mockMBean.getActiveConnections()).thenReturn(2, 1, 0);
    manager = new ConnectionManager(mockFactory, ticker, TimeUnit.MILLISECONDS.toNanos(50));

    manager.getConnection("test-server", "test-url", connProps, true, poolProps, null);

    ticker.advanceTime(ConnectionManager.POOL_EXPIRATION_TIMEOUT_HOURS + 1, TimeUnit.HOURS);
    manager.cleanCache();

    // wait for at least 3 iteration of sleeping
    Uninterruptibles.sleepUninterruptibly(2500, TimeUnit.MILLISECONDS);

    verify(mockMBean, times(3)).getActiveConnections();
    verify(mockDataSource, times(1)).close(); // verify datasource is closed when evicted
}
 
Example #3
Source File: DataSourceTerminationTask.java    From apollo-use-cases with Apache License 2.0 6 votes vote down vote up
/**
 * @see <a href="https://github.com/brettwooldridge/HikariCP/issues/742">Support graceful shutdown of connection
 * pool</a>
 */
private boolean terminateHikariDataSource(HikariDataSource dataSource) {
  HikariPoolMXBean poolMXBean = dataSource.getHikariPoolMXBean();

  //evict idle connections
  poolMXBean.softEvictConnections();

  if (poolMXBean.getActiveConnections() > 0 && retryTimes < MAX_RETRY_TIMES) {
    logger.warn("Data source {} still has {} active connections, will retry in {} ms.", dataSource,
        poolMXBean.getActiveConnections(), RETRY_DELAY_IN_MILLISECONDS);
    return false;
  }

  if (poolMXBean.getActiveConnections() > 0) {
    logger.warn("Retry times({}) >= {}, force closing data source {}, with {} active connections!", retryTimes,
        MAX_RETRY_TIMES, dataSource, poolMXBean.getActiveConnections());
  }

  dataSource.close();

  return true;
}
 
Example #4
Source File: ConnectionManagerTest.java    From pxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testPoolExpirationWithActiveConnectionsOver24Hours() throws SQLException {
    MockTicker ticker = new MockTicker();
    ConnectionManager.DataSourceFactory mockFactory = mock(ConnectionManager.DataSourceFactory.class);
    HikariDataSource mockDataSource = mock(HikariDataSource.class);
    when(mockFactory.createDataSource(anyObject())).thenReturn(mockDataSource);
    when(mockDataSource.getConnection()).thenReturn(mockConnection);

    HikariPoolMXBean mockMBean = mock(HikariPoolMXBean.class);
    when(mockDataSource.getHikariPoolMXBean()).thenReturn(mockMBean);
    when(mockMBean.getActiveConnections()).thenReturn(1); //always report pool has an active connection
    manager = new ConnectionManager(mockFactory, ticker, TimeUnit.MILLISECONDS.toNanos(50));

    manager.getConnection("test-server", "test-url", connProps, true, poolProps, null);

    ticker.advanceTime(ConnectionManager.POOL_EXPIRATION_TIMEOUT_HOURS + 1, TimeUnit.HOURS);
    manager.cleanCache();

    // wait for at least 3 iteration of sleeping (3 * 50ms = 150ms)
    Uninterruptibles.sleepUninterruptibly(150, TimeUnit.MILLISECONDS);

    ticker.advanceTime(ConnectionManager.CLEANUP_TIMEOUT_NANOS + 100000, TimeUnit.NANOSECONDS);

    // wait again as cleaner needs to pick new ticker value
    Uninterruptibles.sleepUninterruptibly(150, TimeUnit.MILLISECONDS);

    verify(mockMBean, atLeast(3)).getActiveConnections();
    verify(mockDataSource, times(1)).close(); // verify datasource is closed when evicted
}
 
Example #5
Source File: HikariDatabaseConnectionPool.java    From bitcoin-verde with MIT License 4 votes vote down vote up
@Override
public Integer getInUseConnectionCount() {
    final HikariPoolMXBean hikariPoolMXBean = _dataSource.getHikariPoolMXBean();
    return hikariPoolMXBean.getActiveConnections();
}
 
Example #6
Source File: HikariDatabaseConnectionPool.java    From bitcoin-verde with MIT License 4 votes vote down vote up
@Override
public Integer getAliveConnectionCount() {
    final HikariPoolMXBean hikariPoolMXBean = _dataSource.getHikariPoolMXBean();
    return hikariPoolMXBean.getIdleConnections();
}
 
Example #7
Source File: HikariDatabaseConnectionPool.java    From bitcoin-verde with MIT License 4 votes vote down vote up
@Override
public Integer getCurrentPoolSize() {
    final HikariPoolMXBean hikariPoolMXBean = _dataSource.getHikariPoolMXBean();
    return hikariPoolMXBean.getTotalConnections();
}