connections to database in ASP.NET MVC

Last post 07-04-2009 9:21 AM by zielony. 3 replies.

Sort Posts:

  • connections to database in ASP.NET MVC

    07-04-2009, 3:34 AM
    • Member
      46 point Member
    • zielony
    • Member since 06-24-2009, 2:22 AM
    • Posts 164

    In each article about ASP.NET MVC we have that we should have queries to one table in other created by us model - this is ok but in each model we should also have:

    private databaseDataContext db = new databaseDataContext();

    What if we must use in action from for example 5 Models. It means 5 connections to database * for example 100 users = 500 connections to database in the same time. Is it ok ?

    In Symfony php framework in that case I have only 100 connections.

  • Re: connections to database in ASP.NET MVC

    07-04-2009, 8:06 AM
    Answer

    So long as the connections string is exactly the same for each of the Models, ADO.NET connection pooling will take effect and manage the connections for you - reusing ones as necessaary.  Bear in mind also, that just becasue you have 100 visitors currently on your site, that doens't mean they are all simultaneously opening a connection to the database.  That only happens for as long as it takes to execute a command as part of the page request.


    Regards Mike
    [MVP - ASP/ASP.NET]
    My site    Please help - URGENT!!!    What ASP.NET can and can't do
  • Re: connections to database in ASP.NET MVC

    07-04-2009, 9:10 AM
    Answer
    • Contributor
      6,620 point Contributor
    • paul.vencill
    • Member since 02-01-2006, 12:57 PM
    • Gaithersburg, MD
    • Posts 1,350

     zielony,

    As Mike said, there's connection pooling going on in teh background.  Also, see my answer on the other thread for more detail. The short of it is, that ADO.Net is going to have a pool of connections.  It dynamically creates and destroys them based on usage levels and such, but will reuse the ones it has in favor of creatign new ones.  It has a default cap of 100 concurrent connections, but as stated before, your goal if you're writign directly against ADO.Net is to hold onto a connection only as long as it takes to conduct a single transaction / query. 

    You can change the default min pool (0) and max pool (100) as needed to tune your app (by changing the connection string), but I wouldn't worry about that up front.  Make that a tuning event six months or a year into production. 

    Help those who have helped you... remember to "Mark as Answered"
  • Re: connections to database in ASP.NET MVC

    07-04-2009, 9:21 AM
    • Member
      46 point Member
    • zielony
    • Member since 06-24-2009, 2:22 AM
    • Posts 164

    ok thx :)

Page 1 of 1 (4 items)