My aim is to use the try-with-resource construct with a single instance of a class, i.e. a singleton, that handles a pool of connections. I need this to ensure that the connection pool is closed at the end of everything, then I wanna use try-with-resource. E.g.
public class MyHandler implements java.io.Closeable{
public static ConnectionPool pool;
//Init the connection pool
public MyHandler(){
pool = ...;
}
@Override
public void close() {
pool().close();
}
}
where a possible main
is:
public static void main(String [] args){
try(MyHandler h = new MyHandler){
//execute my code
// somewhere I do MyHandler.pool.something();
}
}
How can I ensure that the MyHandler is used as singleton?