title: How to close the database connection at the end of the program?
body: <p>In a Java program, I have a singleton class that holds the database connection, which is used by the entire program.</p>
  
  <p>How do I tell Java to close the connection when the program ends?</p>
  
  <p>I can put a connection.close() statement at the end of main, but this will not be executed if the program ends unexpectedly, e.g, by an uncaught exception or "exit" call somewhere within the program. How do I tell Java to close the connection regardless of how the program ends?</p>