How expensive is a DataAdapter?

Posts   
 
    
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 18-Jul-2005 22:30:55   

Hi,

Because of asp.net's db connection pooling I don't feel too anxious about opening and closing indepedent connections objects within multiple user controls on a single page.

Is a DataAdapter an expensive resource which needs to be shared around or can I create them when and where they're needed?

Cheers,

I.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Jul-2005 23:01:51   

Create when you need one, and dispose it right after that. Getting a connection from the pool is at most 20ms, and creating an object is even faster, and a dataadapter is actually just a datareader consumer. So you won't win much if you share such an object around simple_smile

Frans Bouma | Lead developer LLBLGen Pro
JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 19-Jul-2005 02:23:17   

Also adapter is not threadsafe, right Frans?

Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 19-Jul-2005 05:35:13   

Otis wrote:

Create when you need one, and dispose it right after that...

I've been wondering about this: why not just let the garbage collector dispose of the data adapter once it goes out of scope?

Jeff

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 19-Jul-2005 09:55:21   

I don't expect it to be threadsafe indeed, as it handles datareaders and connections internally.

I think disposing is a good thing in situations where you let the dataadapter open/close the connections.

Frans Bouma | Lead developer LLBLGen Pro
Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 19-Jul-2005 15:24:46   

Otis wrote:

I think disposing is a good thing in situations where you let the dataadapter open/close the connections.

Why?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 19-Jul-2005 16:14:29   

As it then holds resources which should disposed themselves, e.g. the connection objects. They will eventually, when the dataadapter is cleaned up, but that can take a while.

Frans Bouma | Lead developer LLBLGen Pro
Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 19-Jul-2005 17:34:36   

Otis wrote:

As it then holds resources which should disposed themselves, e.g. the connection objects. They will eventually, when the dataadapter is cleaned up, but that can take a while.

Good. I haven't been disposing my dataadapters and I was concerned that I was permanently tying up resources. I'll go back and edit my code to manually dispose.

Thanks.

Jeff

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 19-Jul-2005 20:22:02   

It's not always a big deal though. It's not as if your code will crawl all of a sudden if you don't call Dispose, it's common practise to do so simple_smile

If you're using C#, use the using(..) {} constuct, it will call Dispose for you simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 19-Jul-2005 22:21:46   

Otis wrote:

It's not always a big deal though. It's not as if your code will crawl all of a sudden if you don't call Dispose, it's common practise to do so simple_smile

If you're using C#, use the using(..) {} constuct, it will call Dispose for you simple_smile

Hey, I've been doing this all along... good to know I wasn't just hallucinating or wasting my time simple_smile

jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 19-Jul-2005 23:04:59   

Another reason not to share a single DataAccessAdapter is that transactions are bound to an adapter instance. From the docs:

"Because the transactional code is inside the DataAccessAdapter, every method of the DataAccessAdapter object you call after you've started a transaction is ran inside that transaction, including stored procedure calls, entity fetches, entity collection saves etc"

Jeff...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 19-Jul-2005 23:40:09   

Btw I was talking about ADO.NET dataadapter objects, not DataAccessAdapter's.

DataAccessAdapter objects you always have to dispose if you're using Firebird. You can get away with not disposing them on other db's, but always close MANUALLY opened connections. Though if possible, call dispose, it's cleaner and it gets rid of dangling transactions as well simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 20-Jul-2005 20:49:09   

DataAccessAdapter is what I was originally referring to! Should've been more precise.