If you don't run close connection in finalize, can you be sure that the
connection was closed if an exception is thrown?

Chris Catton
BioImage Database Development Manager
Department of Zoology
University of Oxford
OX1 3PS
�
Tel: +44 (0) 1865 281993
email: [EMAIL PROTECTED]
web site: www.bioimage.org


> -----Original Message-----
> From: Rosdi bin Kasim [mailto:[EMAIL PROTECTED]]
> Sent: 21 February 2003 04:13
> To: Tomcat Users List
> Subject: OT: Is it ok to close connection in finalize() ?
> 
> 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]

Reply via email to