• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to correctly close the Connection object?

 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am having a connection object and getting conectin from some class. my DbClass method getConnection throws an exception if it is unable to make a connection.

the code is like



Suppose the con is not establish in DbClass.getConnection, it comes to the catch block and then when it try to execute con.close(), would not it throw again an other exception of NullPointer? how should i close it with correct error handling.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By testing whether it is null:

If it is null, no closing is necessary.
[ August 21, 2006: Message edited by: Ulf Dittmer ]
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahan! ... I am already doing it this way.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4031
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Side track, but tell me the catching of "Exception" was just an example? No one should ever catch general Exceptions unless you really really want to (such as on the top of a facade or service layer).
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm.....I dont agree 100% with that last post. To the point made, you might not want to catch the exception as you have coded it and instead allow that to be throw to a controlling block of code.

But.......that said, it certainly is advisable to catch a useless exception thrown when the connection is closed in the event one occurs. In the example here, if the connection is not made, or if some other processing error occurs, thats the exception you care abbout. The second exception as you ask in your question......cleanup of a closed or null database connection (the connection object might not be not null but not connected for example) is a "dont care" exception and SHOULD be caught and discarded.

The correct way to do this is as follows:

in your finally block:

try{con.close();}
catch(Exception e){//note no code here, I just dont care about this exception}

this exception is caught and ignored and the only exception you have to deal with is the one that is relevant, i.e. no connection was made or a bad password or whatever.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4031
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should always catch the most specific exceptions possible so that you can handle each of their different types of errors appropriately. Inside an application, this might mean you convert specific sets of errors into general application exceptions (some class you implement for example). The only time I would catch "Exception" or "Throwable" is on the top most level of an application where throwing the exception to the user wouldn't make sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic