Link to home
Start Free TrialLog in
Avatar of twg
twg

asked on

clean-up resources

What is the best way to clean-up after an object that uses ResultSet,Connection and Statement objects?
I wrote close() method which closes these objects (by using their close method:Connection.close(),ResultSet.close() etc,
but I have to put all in a try block so what happens if an exception is thrown?
Another issue - what if  the class which uses those objects is the main class
and no one calls its close() method?
Avatar of CJ_S
CJ_S
Flag of Netherlands image

You use ASP, right, then you should set your objects to nothing after you've closed them.always nothing your objects, that's the way to clean up memory

Conn.Close
Conn = nothing
Avatar of twg
twg

ASKER

thanks CJ S but I don't use ASP.
Anyway, the problems still stay open(if an exception is thrown, and how can I garantee clean-up for the main class
then what do you use, VBscript? That's also nothing

javascript is null

javascript:
object = null
vbscript:
object = nothing
asp = vbscript:
object = nothing

that'll ganarantue the clean up.
Avatar of twg

ASKER

I wrote my application in Java and it uses the java.sql package. I'm sorry that I didn't write my question in the right section (java programming) so can you pass my question to the relevant people?
ASKER CERTIFIED SOLUTION
Avatar of CJ_S
CJ_S
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of twg

ASKER

I wrote my application in Java and it uses the java.sql package. I'm sorry that I didn't write my question in the right section (java programming) so can you pass my question to the relevant people?
PART 1
_______

Use the finally clause....

try{
 
}
catch(Exeption e){
   // handle exception
}
finally
{
  // can clean up stuff here
}

EXCEPTION THROWN
If an exception occurs in the try block, and there is an associated catch block to handle the exception , control transfers to the catch block and then to the finally block

TRY BLOCK COMPLETES
If control reaches the end of the try , it then proceeds to the finally block.


PART 2
_______
You should really make sure that the close is called. But you can use the finalize()method. This method is called by the garbage collector calls before it collects the objects in memory. But there is a problem, you have no gaurantee that any objects will be garbage collected...