DataAccessAdapter lifetime in an ASP.NET Core Application

Posts   
 
    
dodyg
User
Posts: 45
Joined: 04-Dec-2014
# Posted on: 08-Mar-2021 09:21:53   

Does it make any difference between creating DataAccessAdapter with Scope lifetime and using/injecting it in multiple different methods in a single HTTP request compared to creating DataAccessAdapter every time it is needed in each method?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39888
Joined: 17-Aug-2003
# Posted on: 08-Mar-2021 09:28:17   

As long as a single thread handles all work on the adapter, you'll be fine, but I do recall asp.net requests might be handled by multiple threads (handled by X->paused-> handled by Y) which might cause problems. Although now that I think of it, all work is serial, so it's not the case that multiple threads at the same time work on the adapter.

You won't win much btw, creating an adapter should be very fast. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
dodyg
User
Posts: 45
Joined: 04-Dec-2014
# Posted on: 08-Mar-2021 11:03:58   

Does it matter though in terms of just using one connection to the database per request instead of multiple connections? Even with connection pooling.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39888
Joined: 17-Aug-2003
# Posted on: 08-Mar-2021 17:24:05   

dodyg wrote:

Does it matter though in terms of just using one connection to the database per request instead of multiple connections? Even with connection pooling.

I'd leave that to the ado.net provider so leave it to the pooler. The creation of a pooled connection is very fast, as it's just a .net object wrapping a live connection that's already initialized.

But if you want to, you could keep one adapter and one connection, the main issue to avoid is to have multiple threads using the same adapter and connection as you then run the risk that resultsets in flight over a live connection are destroyed by a command executed (or you get an error from ado.net... )

Frans Bouma | Lead developer LLBLGen Pro