I've never heard of issues with putting things in finalize(), but I haven't really looked into it. However my issue is that you can't predict how quickly the objects will be collected (see other post).
I'm not a java expert, I just play one on tv. --mikej -=----- mike jackson [EMAIL PROTECTED] > -----Original Message----- > From: Jake Robb [mailto:[EMAIL PROTECTED]] > Sent: Thursday, February 20, 2003 8:36 PM > To: Tomcat Users List > Subject: Re: OT: Is it ok to close connection in finalize() ? > > > And now all these other guys say that using finalize() is bad practice. I > have to ask... Is it bad practice just for the reasons Mike Jackson > described, or does the use of finalize() in general actually > cause problems > with the JVM? I've not heard that before. The 1.4 API makes no > mention of > any issues. > > Details on issues with finalize() would be appreciated, if that is indeed > the case. > > -Jake > > > Jake Robb wrote: > > > I apologize for not being familiar with this, but I have some insight to > > offer. Bear with me: > > > > Is ConnCache your own class (i.e., did you write it)? If it > is, I'd put a > > call to close() in the finalize method of that ConnCache, not > GeneralConn. > > If it's not, I would expect that has already been done. > > > > If ConnCache not your class, and it does not close its own > connection, then > > yes, you're doing it right. There shouldn't be any issues with that > > approach. > > > > finalize() is indeed guaranteed to run upon garbage collection, which in > > turn is guaranteed to happen. However, garbage collection can > happen at any > > time, so the connection may not be closed immediately. If this > is an issue > > for you, you can call System.gc() to force it to garbage collect > > immediately. > > > > -Jake > > > > > > > > Rosdi bin Kasim wrote: > > > >> This is a bit off topic. > >> > >> I am using connection pooling, and in my code, I open all the > connection I > >> need in an object constructor. > >> Then I will close this connection in finalize(), which > (according to what I > >> read) will be executed during java garbage collect. > >> > >> --- sample code ------ > >> > >> public class GeneralConn { > >> > >> private ConnCache connCache; > >> > >> //open connection during object creation > >> public GeneralConn () { > >> try { > >> connCache = ConnCache.getInstance(); > >> dbConn = connCache.getConnection(); //grab a > connection from > >> the connection pool > >> }catch (Exception e) { > >> System.out.println("Fail to open connection: " + > >> e.getMessage()); > >> } > >> } > >> > >> //close when this object is destroyed > >> public void finalize () { > >> try{ > >> dbConn.close(); > >> dbConn = null; > >> }catch (Exception e) { > >> System.out.println("Fail to close connection: " + > >> e.getMessage()); > >> } > >> } > >> } > >> > >> Would this be okay? is it guaranteed that finalize() will be > executed during > >> garbage collect?? > >> > >> > >> Regards, > >> Rosdi bin Kasim. > >> > >> > >> > >> > >> --------------------------------------------------------------------- > >> To unsubscribe, e-mail: [EMAIL PROTECTED] > >> For additional commands, e-mail: [EMAIL PROTECTED] > >> > > > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
