Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it essential to call shutdown() on HikariDataSource? #210

Closed
timmolter opened this issue Nov 28, 2014 · 6 comments
Closed

Is it essential to call shutdown() on HikariDataSource? #210

timmolter opened this issue Nov 28, 2014 · 6 comments
Labels

Comments

@timmolter
Copy link

No description provided.

@brettwooldridge
Copy link
Owner

shutdown() or close() is essential at application termination, otherwise you will leave database resources in use. Most IoC containers (Spring, JBoss, Hibernate, Tomcat) have a way to invoke a "destroy" method.

Somewhat riskier, but still workable, is to set minimumIdle connections to 0, set the actual database to timeout connections after, for example, 5 minutes, and set the idleTimeout and maxLifetime in HikariCP to 4 minutes. This way, if the application aborts "uncleanly", the database itself will terminate and cleanup the connections after 5 minutes.

Actually, even when minimumIdle is non-zero, is it not a bad idea to configure the native database timeouts to between 5-20 minutes, and the HikariCP maxLifetime about two minutes shorter than that. There will be little discernable impact on performance for your application, but you can actually avoid some memory leaks present in databases due to long-held connections.

@timmolter
Copy link
Author

Thanks for the explanation. Very helpful.

What if I have DataSource ds = new HikariDataSource();. How would you recommend closing ds since DataSource does not have a close() method? I noticed HikariDataSource implements Closable, but I'm not sure if that helps in my case.

@brettwooldridge
Copy link
Owner

Closeable will help if you have raw DataSource and you are sure it is a HikariDataSource, you can simply cast to Closeable and call close (and possibly have to suppress a casting warning in your IDE).

Issue #208 just before this one, when fixed, will allow you to call ds.unwrap(HikariDataSource.class).close().

@timmolter
Copy link
Author

Is it just me or is it odd that DataSource does not have a cleanup method? It seems like every DataSource impl needs to implement some clean up. My particular problem is that I don't know if the Datasource is a HikariDataSource or something else.

@brettwooldridge
Copy link
Owner

I agree, it is odd. It is even stranger that (at a minimum) the DataSource (or at least PooledDataSource), in Java 8 still does not implement Closeable. Would have been nice.

@sundhr
Copy link

sundhr commented Nov 5, 2018

When the OS takes care of cleaning up the resources when the process exits, why should I take care of closing it during termination of the program?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants