aspose file tools*
The moose likes Java in General and the fly likes how to close connection object in finally block when an exception is raised in try bl Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to close connection object in finally block when an exception is raised in try bl" Watch "how to close connection object in finally block when an exception is raised in try bl" New topic
Author

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

Praveen palukuri
Ranch Hand

Joined: Feb 10, 2005
Posts: 65
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

Joined: Mar 20, 2001
Posts: 210

Usually this is the way to do it:
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Wirianto Djunaidi:
Usually this is the way to do it:


Except that will leave Connections open unless there is an Exception...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Praveen palukuri
Ranch Hand

Joined: Feb 10, 2005
Posts: 65
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

Joined: Apr 14, 2004
Posts: 10336

Check the connection is null in your finally block before trying to close it. That' how its normally done.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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.


There is no emoticon for what I am feeling!
Julien Grenier
Ranch Hand

Joined: Sep 01, 2005
Posts: 41
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

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    Originally posted by Julien Grenier:
    //you can close it without checking for null



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


    [Jess in Action][AskingGoodQuestions]
    Steve Morrow
    Ranch Hand

    Joined: May 22, 2003
    Posts: 657

    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

    Joined: Sep 01, 2005
    Posts: 41
    Why is it bad style? explain?

    The scope of the variable is more appropriate and you avoid the null-checking.
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    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


    Java API Documentation
    The Java Tutorial
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    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

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    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

    Joined: Jul 11, 2001
    Posts: 14112
    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.


    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to close connection object in finally block when an exception is raised in try bl