You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Christian J. Dechery" <ch...@finep.gov.br> on 2002/06/11 15:33:07 UTC

problem with connections not closing...

I'm experiencing here a pretty serious problem, that we can't get trough...
 
We have several classes accessing and connecting via the oracle native driver... but so it happens, some of these connections aren't properly closed when the class "dies" (meaning, the execution of the JSP ends).
 
I've even debugged it, there are some JSPs that opens like 4 connections, and in my finalize() method there's a con.close() call, never all the 4 con.close() are successfull, there's always at least one that fails and gives an execption. I don't know why.
 
Would that "JDBC Realm" thing help that? What is that anyway, could someone explain to me?
 
thanks
 
.:| Christian J. Dechery
.:| FINEP - Depto. de Sistemas
.:| christian@finep.gov.br
.:| (21) 2555-0332


Re: problem with connections not closing...

Posted by Tim Funk <fu...@joedog.org>.
finalize() gets called on garbage collection. Which may occur a long 
time from when your are done with your connection. Which isn't the time 
to close your db connections - it should be done much earlier. Here is 
code I typically use:

Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
   con = magicalConnectionGetter();
   stmt = magicalStatementGetter();
   rs = magicalResultSetGetter();
} catch(Throwable e) {
     //do as needed
} finally {
   try {if (rs!=null) rs.close();}catch(Throwable e){}
   try {if (stmt!=null) stmt.close();}catch(Throwable e){}
   try {if (con!=null) con.close();}catch(Throwable e){}
}

Always use finally to close your database assets - it will save much 
heartache later. finally blocks always gets run  - even if you issue a 
return in the try block.

-Tim

Christian J. Dechery wrote:
> I'm experiencing here a pretty serious problem, that we can't get trough...
>  
> We have several classes accessing and connecting via the oracle native driver... but so it happens, some of these connections aren't properly closed when the class "dies" (meaning, the execution of the JSP ends).
>  
> I've even debugged it, there are some JSPs that opens like 4 connections, and in my finalize() method there's a con.close() call, never all the 4 con.close() are successfull, there's always at least one that fails and gives an execption. I don't know why.
>  
> Would that "JDBC Realm" thing help that? What is that anyway, could someone explain to me?
>  
> thanks
>  
> .:| Christian J. Dechery
> .:| FINEP - Depto. de Sistemas
> .:| christian@finep.gov.br
> .:| (21) 2555-0332
> 
> 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>