Functions called in a finallize

K

kikotores

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?
 
T

Thomas Hawtin

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top