Open - Close JDBC connection
843854Dec 10 2003 — edited Dec 11 2003
Hi JDBC gurus. I have an simple question regarding best practice for opening and losing an JDBC connection. I am developing an web application that consist of many Java Beans and one Database class. Beans are instantiating an object of the Database class and getting some data from an MySQL DB.
Which of following two scenarios is better?
Scenario one:
Creating an Connection in constructor of the Database class, and closing this connection in class destructor �finalize()�. The Beans does not have to take care about closing DB connection. It is automatically done by destructor.
Scenario two:
Creating an Connection in constructor of the Database class, and closing this connection in an separated method �removeBD()�. The method is called in each bean after finishing JDBC job.
Which scenario is better, or is there any other better scenario I have not considered?
Thanks: NB