Thursday, March 29, 2012

Performance Question with Database Connections

I was just wondering what other people's opinions and experiences were in
regards to using Database Connections throughout a website. Supposing that a
single webpage accesses a database anywhere from 5-30 times throughout a
webpage do you prefer to open the database connection upon say
initialization and reuse it elsewhere whenever possible and then dispose of
it upon disposing or do you write 5 - 10 lines of code opening and closing
it every single time you want to use the database.It doesn't really matter much because of the built in database connection
pooling.
Creating a database connection 5 times in a page has no significant
performance penalty, so do it whichever way seems most intuitive for your
web site.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

<johndoe@.driver.net> wrote in message
news:u9lhwP8PEHA.3232@.TK2MSFTNGP11.phx.gbl...
> I was just wondering what other people's opinions and experiences were in
> regards to using Database Connections throughout a website. Supposing that
a
> single webpage accesses a database anywhere from 5-30 times throughout a
> webpage do you prefer to open the database connection upon say
> initialization and reuse it elsewhere whenever possible and then dispose
of
> it upon disposing or do you write 5 - 10 lines of code opening and closing
> it every single time you want to use the database.
I kind of assumed this but there are thousands of scenarios that are coming
to my mind for example if you have 150 - 350 concurrent users accessing a
webpage at once what type of load would this put on with having 350
connections open at one time versus 1000 - 3000 connections opening and
closing, etc so I thought I would see what other people would have to say.

does this
"Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
news:O6OStZ8PEHA.1048@.tk2msftngp13.phx.gbl...
> It doesn't really matter much because of the built in database connection
> pooling.
> Creating a database connection 5 times in a page has no significant
> performance penalty, so do it whichever way seems most intuitive for your
> web site.
> --
> I hope this helps,
> Steve C. Orr, MCSD, MVP
> http://Steve.Orr.net
>
> <johndoe@.driver.net> wrote in message
> news:u9lhwP8PEHA.3232@.TK2MSFTNGP11.phx.gbl...
>> I was just wondering what other people's opinions and experiences were in
>> regards to using Database Connections throughout a website. Supposing
>> that
> a
>> single webpage accesses a database anywhere from 5-30 times throughout a
>> webpage do you prefer to open the database connection upon say
>> initialization and reuse it elsewhere whenever possible and then dispose
> of
>> it upon disposing or do you write 5 - 10 lines of code opening and
>> closing
>> it every single time you want to use the database.
>>
>>
The connections aren't really opening and closing though, so from a
performance perspective the 2 scenarios scenarios are virtually identical.
So then it comes down to opinion about how you prefer to structure your
code.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

<johndoe@.driver.net> wrote in message
news:%23CE34T9PEHA.3944@.tk2msftngp13.phx.gbl...
> I kind of assumed this but there are thousands of scenarios that are
coming
> to my mind for example if you have 150 - 350 concurrent users accessing a
> webpage at once what type of load would this put on with having 350
> connections open at one time versus 1000 - 3000 connections opening and
> closing, etc so I thought I would see what other people would have to say.
Hmm do Connections Opened with .Open not correlate to SqlConnections
because I know that
New SqlConnection("...") ;
If a .Open is not called an exception is thrown stating the
connection is not thrown.

If the Page takes 1.3 seconds to render from the time Page_Load is
called and the DbConnection is .Open()ed to the time when the page is
finished doing its thinking and .Close()ed is the Connection not "open" ?
versus opening something 10 times.

Upon further reflection, logistically speaking it seems that Opening a
DbConnection might be good for a page that renders quickly versus a page
that might take an extended period of time to complete its rendering
(especially if there is the possibility of many users hitting it at the same
time).
Unless of course they just make us .Open and .Close and throw exceptions
when they are not done so properly just for the fun of it.

"Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
news:O4mjGk9PEHA.1348@.TK2MSFTNGP12.phx.gbl...
> The connections aren't really opening and closing though, so from a
> performance perspective the 2 scenarios scenarios are virtually identical.
> So then it comes down to opinion about how you prefer to structure your
> code.
> --
> I hope this helps,
> Steve C. Orr, MCSD, MVP
> http://Steve.Orr.net
>
> <johndoe@.driver.net> wrote in message
> news:%23CE34T9PEHA.3944@.tk2msftngp13.phx.gbl...
>> I kind of assumed this but there are thousands of scenarios that are
> coming
>> to my mind for example if you have 150 - 350 concurrent users accessing a
>> webpage at once what type of load would this put on with having 350
>> connections open at one time versus 1000 - 3000 connections opening and
>> closing, etc so I thought I would see what other people would have to
>> say.

0 comments:

Post a Comment