Should a database connection stay open all the time or only be opened when needed?
I have a bukkit plugin (minecraft) that requires a connection to the database. Should a database connection stay open all the time, or be opened and closed when needed? The database connection must be opened only when its needed and closed after doing all the necessary job with it. Code sample: Prior to Java 7: Connection con = null; try { con = ... //retrieve the database connection //do your work... } catch (SQLException e) { //handle the exception } finally { try { if (con != null) { con.close(); } } catch (SQLException shouldNotHandleMe) { //... } } Java 7: try (Connection con = ...) { }