Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to close connection object in finally block when an exception is raised in try bl

 
Praveen palukuri
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
would you please tell me the answer for the query?

In try block i've written code to establish database connection and in finally block i'd closed the connection.
If any exception is not raised then no problem.
If an exception is raised while creating database connection object then connection object is not created then to statements in finally block will be executed as default.

How it is possible that with out creating an obj how can we close it?
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually this is the way to do it:
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wirianto Djunaidi:
Usually this is the way to do it:


Except that will leave Connections open unless there is an Exception...
 
Praveen palukuri
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank U for showing interest to reply.
i know the code to close connection.
what my query is if an exception is raised while establishing connection, connection object is not created.But by default the statements in finally block are executed where we've written code to close the connection.
But connection object is not created.

What will happen at this situation?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the connection is null in your finally block before trying to close it. That' how its normally done.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find it useful to write a subroutine to do the closing because
having a try in a catch looks messy to me:

This way, too, if you decide to log SQL exceptions that
occur during close instead of ignoring them, for example,
you need only change your code in one place.
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to do it is like that :



that way :
  • You catch only once
  • the scope of connection is more representative
  • you are sure of closing the connection.

  •  
    Ernest Friedman-Hill
    author and iconoclast
    Marshal
    Pie
    Posts: 24211
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Julien Grenier:
    //you can close it without checking for null



    Yuk. Bad style. Gabby's is superior and also quite common.
     
    Steve Morrow
    Ranch Hand
    Posts: 657
    Clojure Spring VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yuk. Bad style. Gabby's is superior and also quite common.

    Seconded. Also handy is the DbUtils class from Jakarta Commons, if you're hand-rolling JDBC code...
     
    Julien Grenier
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why is it bad style? explain?

    The scope of the variable is more appropriate and you avoid the null-checking.
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Ernest Friedman-Hill:



    Yuk. Bad style. Gabby's is superior and also quite common.


    I recall another thread that a similar situation with files was discussed. In fact, I think I started this other thread, but I cannot find it. Someone posted in that thread something similar to Julien's code here and rattled off some reasons why it is preferred. If you know the thread I'm talking about, a link would be nice

    Layne
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay, I found the thread here, if you are interested.
    [ October 14, 2005: Message edited by: Layne Lund ]
     
    Ernest Friedman-Hill
    author and iconoclast
    Marshal
    Pie
    Posts: 24211
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I remember that too. It was Tony Morris, I think, who was saying that the nested try blocks were the way to go. Personally I don't like it -- but I can understand the logic. One reason I'm wary of nested try blocks is that I've seen decompilers and byte-code instrumentation tools trip over them. The generated code can be messy indeed.

    But what I was complaining about is actually unrelated -- not the nested try blocks, but allowing the NullPointerException to be thrown and then caught when you try to close the nonexistent Connection. Not only does this have bad performance (letting that easily-avoided exception be thrown when you know throwing exceptions is costly), but it also masks what the original exception was if there was an exception opening the connection!
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Ernest Friedman-Hill:
    But what I was complaining about is actually unrelated -- not the nested try blocks, but allowing the NullPointerException to be thrown and then caught when you try to close the nonexistent Connection.


    Take a look at the code again. The connection *cannot* be null in the finally block.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic