1

I use Hikary connection pool with following settings:

  HikariDataSource dataSource = new HikariDataSource();
        dataSource.setMinimumIdle(0);
        dataSource.setMaximumPoolSize(Integer.MAX_VALUE);
        dataSource.setJdbcUrl(jdbcConnectionString);
        dataSource.setConnectionTestQuery("select 1");
        dataSource.setIdleTimeout(TimeUnit.SECONDS.toMillis(60));
    dataSource.getConnection();

After getConnection() hikari try to get 2 connections to instance, but put in connection pool just one connection. How can I fix it? The hikari version is 3.4.0

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • What is the point of having connection **pool** with only **one** connection? – Marmite Bomber Sep 20 '19 at 11:41
  • I don't know. I don't use it. Please, let me know if you find answer – Иван Гладуш Sep 20 '19 at 11:43
  • Do I get you right, that `getConnection` return *one* connection object, but you see *two* connections opened in the database? You may try to set [maximum-pool-size](https://stackoverflow.com/q/52420033/4808122) to 1, if you insist to have a *pool with one connection*. – Marmite Bomber Sep 20 '19 at 11:56
  • Yes, hikari somewhere creates two connection first time, but add only one in connection pool – Иван Гладуш Sep 20 '19 at 12:17
  • You should never do this: `setMaximumPoolSize(Integer.MAX_VALUE)`. Limit it to a reasonable number (eg 5 or 10 for low volume applications, and 'scale up' when your application needs it). – Mark Rotteveel Sep 20 '19 at 14:01
  • How do you observe that two connections are created and only one added to the pool? – Mark Rotteveel Sep 20 '19 at 14:02
  • I put breakpoint in SnowflakeConnection and noticed that constructor was invoked twice, both times from hikaryPool, after that I saw in log that one HikariPool was created and this pool has only one conneciton – Иван Гладуш Sep 20 '19 at 14:49
  • Can you try removing ` dataSource.setMaximumPoolSize(Integer.MAX_VALUE)` ? – Ori Marko Sep 20 '19 at 17:30

2 Answers2

1

I found the answer. Hikari creates first connection in checkFailFast method. I update this comment when find how to disable this method. The checkFailFast doesn't work if initializationFailTimeout<0. It helps me

0

After getConnection() hikari try to get 2 connections to instance, but put in connection pool just one connection. How can I fix it?

There is nothing to fix in this behavior. It simple means, that two conenctions were opened and one of them was closed.

The reason why the second connection was closed is that you set setMinimumIdle(0), i.e. no idle connection is maintainend in the pool and all idle connection are closed.

If you want to see both connection in the pool, simple set setMinimumIdle(1). After calling DataSource.getConnection() there will be two connection in the pool - one yours and one idle.

If you don't want to open the second connection at all, set

    config.setMinimumIdle( 1 );
    config.setMaximumPoolSize( 1 );

But think twice, why do you use a connection pool with only one connection.

You may anyway increase both parameters later, while the pool is running.

 HikariConfigMXBean bn = DataSource.ds.getHikariConfigMXBean()
 bn.setMaximumPoolSize(10)
 bn.setMinimumIdle(10)

This will (not instantly) open 9 additional connections to the database.

Note that while setting the MaximumPoolSize == MinimumIdle the number of connection in the pool remains stable, no connections are opened or closed, which is probably the thing you want to observe.

Tested with Hicari 3.4.0 and Oracle 12.2

Marmite Bomber
  • 19,886
  • 4
  • 26
  • 53