1

I use actions and child actions in my MVC4 project. İn a view I call nearly ten child actions and each child action opens and closes SqlConnection for itself. Each connection login consumes processor. How can I use a single SqlConnection object for all child actions?

Any helps would be appreciated.

Agnel Amodia
  • 765
  • 8
  • 18
Mesut
  • 1,845
  • 4
  • 24
  • 32
  • Use the ADO .NET Connection P- oh wait, you already are. – ta.speot.is Oct 23 '13 at 10:11
  • See my answer here: http://stackoverflow.com/questions/6334592/one-dbcontext-per-request-in-asp-net-mvc-without-ioc-container/10153406#10153406 – walther Oct 23 '13 at 10:11
  • @walther, this ooks like a write answer. Thanks. – Mesut Oct 23 '13 at 10:42
  • @walther, while analyzing with sql profiler, it seams that it logouts per two or three queries. However I keep it opened. At Application_EndRequest I close it. – Mesut Oct 23 '13 at 12:13

1 Answers1

3

Firstly: what is your concern here? Since SqlConnection uses connection pooling by default, this won't usually have any significant overhead - it isn't spinning up actual connections each time.

But: to fix this you could consider storing the connection in the request context; you would also need to add some code to fire at the end of the request to clean up (dispose) the connection.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • +1 But I think storing it inside the request context is a missed opportunity to instead use some IoC container with per-request lifecycle management. – ta.speot.is Oct 23 '13 at 10:28