Static finally block in Java -


i have class uses static initialization block set connection database. class has many public static methods query db. close connection in static block executes before program terminates, kind of finally block in try/catch. not exist in java. best option open , close connection each query?

have @ : running method when closing program?

you try writing code close connection in method.

public static void main(string[] args) {     runtime.getruntime().addshutdownhook(new thread(new runnable() {         public void run() {             //code close connection         }     }, "shutdown-thread")); } 

Comments