Closing a connection question

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
krohani
Posts: 31
Joined: Tue Oct 06, 2015 1:57 pm
Contact:

Closing a connection question

Post by krohani »

Okay from the questions and answer explanations (see below) I understand that closing a Connection renders the resultset and statment not usable however if that is the case then why is it that the "proper" way taught to close out everything is to explicitly in a try/catch close connection, resultset and statement? Why not just close the connection in a try catch?

If closing a connection takes care of the other two why are we being taught to close all three? Very curious.

From answer explanation:
In this case, although we are not closing the ResultSet directly, we are closing the Statement object from which the ResultSet was retrieved and when a Statement object is closed, its current ResultSet object, if one exists, is also closed.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Closing a connection question

Post by admin »

In most applications a connection is never closed explicitly because it is retrieved from a connection pool. (The pool manages the opening and closing of a connection). That is why it is a good idea to close the result sets and statements. Otherwise, there is no need.
HTH,
Paul.

krohani
Posts: 31
Joined: Tue Oct 06, 2015 1:57 pm
Contact:

Re: Closing a connection question

Post by krohani »

admin wrote:In most applications a connection is never closed explicitly because it is retrieved from a connection pool. (The pool manages the opening and closing of a connection). That is why it is a good idea to close the result sets and statements. Otherwise, there is no need.
HTH,
Paul.

Ah yes that is true and makes sense. Thank you sir!

Kirk

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests