If you are writing a library that provides this functionality to someone else, I don't really see what else you can do besides providing a closePool()
method. You could perhaps have your classes inherit from java.io.Closeable
and/or AutoCloseable
, but other than advertising the need to clean up your resources, you mostly have to rely on the developers using your code to do the right thing.
Unexpected finalizers inside library code could be a nasty thing to run into if they aren't implemented correctly.
You could try to add a shutdown hook, but if the JVM is shutting down anyway then there isn't much of a point in forcibly closing the connection pool at that point.