Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 8 2004
Added on Dec 10 2003
3 comments
289 views