commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John McNally <jmcna...@collab.net>
Subject Re: [DBCP] : DelegatingConnection/PoolableConnection Bug ?
Date Wed, 03 Apr 2002 03:18:15 GMT
Anjan wrote:
> 
> Hi Rodney/John,
> 
> >I'm not sure what is being proposed here, so if this is wrong let me
> >know.  But code that is something like the following is not a good idea:
> >
> >Connection con = pool.getConnection();
> >...do something...
> >con.close();
> >...
> >if (!con.isClosed())
> >  con.close();
> 
> As I said earlier, what I'm doing here is something like this
> 
> void func() {
>     Connection conn = null;
>     try {
>         for (i = 0; i < NUM_DATABASES; ++i) {
>             conn = DriverManager.getConnection(driverURL);
>             // do something with connection .......
>             conn.close();
>         }
>     } // end try
>     catch() {
>         //.
>     }
>     finally {
>         // Try to be sure to clean the resources
>         if (!conn.isClosed()) {
>             conn.close();
>         } // end if
>     } // end finally
> } //end func
> 

I would suggest inverting try/catch/finally and for loop so you have

for
  try
     conn = getConnection
  catch
     ...
  finally
     conn.close()
end for

or if you want the exception to abort the for loop

try
   for
      try
         conn = getConnection
      finally
         conn.close()
   end for
catch

These variation will guarantee that close be called once and only once.

john mcnally

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message