Performance Question with Database Connections

G

Guest

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.
 
S

Steve C. Orr [MVP, MCSD]

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.
 
G

Guest

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
 
S

Steve C. Orr [MVP, MCSD]

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.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top