Adapter's CloseConnection

Posts   
 
    
obzekt
User
Posts: 60
Joined: 29-Apr-2004
# Posted on: 22-Jun-2004 19:53:22   

Hi. I would like to ask if the adapter closes its internal db connection when an exception occurs and KeepConnectionOpen=true. In other words, is it safe for the the client to code like this:

DataAccessAdapter adp = new DataAccessAdapter(true);
adp.FetchEntity(..);
adp.FetchEntity(..);
adp.CloseConnection();

or is it expected to do the following to avoid connection leakage:

DataAccessAdapter adp = new DataAccessAdapter(true);
try {
  adp.FetchEntity(..);
  adp.FetchEntity(..);
}
finally { adp.CloseConnection(); }
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 22-Jun-2004 20:29:45   

The latter is the way to go. The DataAccessAdapter class does close/clean up at dispose, so eventually everything is cleaned up, you don't have control over when that will be (as the GC has to kick in), so cleaning up in a finally is the way to go simple_smile

Frans Bouma | Lead developer LLBLGen Pro
obzekt
User
Posts: 60
Joined: 29-Apr-2004
# Posted on: 22-Jun-2004 20:40:32   

I see, that makes sense.

But I assume that in the case of KeepConnectionOpen=false, the adapter closes the connection before throwing the exception back to the caller, so try/finally is not required there. Is this correct?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 22-Jun-2004 20:48:51   

obzekt wrote:

I see, that makes sense.

But I assume that in the case of KeepConnectionOpen=false, the adapter closes the connection before throwing the exception back to the caller, so try/finally is not required there. Is this correct?

True, if KeepConnectionOpen is false, every call to a method which will access the database will open a connection, use the connection and will close it. An exception internally will close the connection first.

A transaction always keeps a connection open.

Frans Bouma | Lead developer LLBLGen Pro