HikariDataSource cannot be initialized at application startup

See original GitHub issue

Spring Boor version: 2.2.2.RELEASE

HikariCP version: 3.4.1

Now, I only initialize the database connection when the application is accessed. If the database connection has errors, I can’t find the problem quickly. I tried some methods and still can’t solve it. E.g

    @Bean
    public LazyInitializationExcludeFilter excludeFilter() {
        return LazyInitializationExcludeFilter.forBeanTypes(HikariDataSource.class);
    }

E.g

spring:
  main:
    lazy-initialization: false

I have two apps with this problem now. One of them runs on JDK8 and the other runs on JDK13.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
snicollcommented, Jan 14, 2020

Thanks for the update, I am with you now. I got a little bit confused as I thought this was happening by default as well.

Let’s take a step back. This problem has nothing to do with lazy initialization. As I’ve indicated above, this feature is disabled by default. What’s happening is that the connection pool for the DataSource (Hikari) is created but nothing is requiring a Connection. The connection pool does not attempt to reach the database until a connection is requested.

Adding the following bean is enough to force validation on startup:

@Bean
public ApplicationRunner validateDataSource(DataSource dataSource) {
	return args -> {
		try (Connection connection = dataSource.getConnection()) {
		}
	};
}

It looks like your other projects are not affected and the Hikari pool is starting when the application starts. Can you check the differences with the samples you’ve attached?

Also flagging for team attention as I don’t know what the next steps for this one should be.

1reaction
MrXionGecommented, Jan 14, 2020

@snicoll Thank you for the example.

    @Bean
    public ApplicationRunner runner(DataSource dataSource) {
        return args -> {
            log.info("dataSource: {}", dataSource);
            Connection connection = dataSource.getConnection();
            log.info("connection: {}", connection);
        };
    }

I added this code. When this code is run, a connection pool is created. It’s really fun. image

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to configure hikari pool for eager initialization?
I want to configure the Hikari pool to eagerly initialize on application startup and not when first query is issued.
Read more >
Stack trace switching over to Hikari Datasource - Google Groups
Had success converting an application using HikariConfig and another user Hikari Datasource within Tomcat but the next app on my list is failing...
Read more >
DriverManagerDataSource (Spring Framework 6.0.2 API)
This driver will get initialized on startup, registering itself with the JDK's DriverManager. NOTE: DriverManagerDataSource is primarily intended for accessing ...
Read more >
Introduction to HikariCP - Baeldung
HikariConfig is the configuration class used to initialize a data source. It comes with four well-known, must-use parameters: username, password ...
Read more >
Connection Pool Configuration in Spring Boot. | by Thanh Tran
And now we have both common-dbcp and HikariCP on the application classpath, ... therefore I cannot mention all of it in this post...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found