Functions called in a finallize

Discussion in 'Java' started by kikotores@yahoo.com, Aug 7, 2005.

  1. Guest

    Im doing a program that connects to a database and I want to put
    disconnect function in the finalize method in the class that does the
    communication to the database. Is that a good idea ( seems like this
    'destructor' does not work. Any good ideas how to do that?
     
    , Aug 7, 2005
    #1
    1. Advertising

  2. wrote:
    > Im doing a program that connects to a database and I want to put
    > disconnect function in the finalize method in the class that does the
    > communication to the database. Is that a good idea ( seems like this
    > 'destructor' does not work. Any good ideas how to do that?


    Finalisers are not destructors. They don't get called until sometime
    after the garbage collector happens to find them. They are generally
    useful for the implementation of resources that attempt to clean up even
    if the client code is broken.

    Instead use try/finally as:

    Connection connection = getConnection();
    try {
    ...
    } finally {
    connection.close();
    }

    Get a good book...

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 7, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apricot
    Replies:
    4
    Views:
    530
    velthuijsen
    Apr 16, 2004
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,604
    Victor Bazarov
    Jun 21, 2004
  3. Weng Tianxiang
    Replies:
    6
    Views:
    591
    glen herrmannsfeldt
    Sep 12, 2007
  4. S_K
    Replies:
    6
    Views:
    1,187
    Robert Dunlop
    Nov 8, 2007
  5. Jimmy Hartzell
    Replies:
    0
    Views:
    421
    Jimmy Hartzell
    May 19, 2008
Loading...

Share This Page