Solved

ASP.Net / ADO connection management

Posted on 2004-03-29
2
572 Views
Last Modified: 2013-12-03
What is the best strategy for opening/reading from/closing connections in an ASP.net application? If you have code behind a page that pulls data from a DB, what happens when 2 users access that same code at the same time? Are 2 separate instances of your ado connection object created on the server?

What if you want to open a connection and use it across multiple pages? How would you go about that?
0
Comment
Question by:gallo47
2 Comments
 
LVL 12

Accepted Solution

by:
dfiala13 earned 250 total points
ID: 10708698
The best strategy is to open a connection, use it and close as soon as possible. .NET handles connection pooling for you (assuming you are always using the same connection string)

If you have code-behind in a page that does a database connection, you will have one instance of each page created and hence one connection object per page.  Two users, two pages, two connections. They are not shared among users.  As mentioned above .NET handles the pooling under the covers so the physcial connections made to the database are minimized.  As soon as you close a connection object , .NET throws the physical connection back into the pool for the next connection request.  To leverage this, it is important to not keep connections open for any longer than you need to. You definitely don't want to open a connection, read some data and wait for the user to update the data.  You get the data and close the connection. When the user submits their changes, open a connection, write to the db and close the connection.

I typically build a DataAccess class that handles creating connections and executing commands.  This is created and called either from an ASPX page or a middle-tier class.  But either way, it means I only have to write my database rotuines in one place and use them anywhere.  This keeps the page code simple and cuts down on cut-and-paste coding.
0
 
LVL 1

Author Comment

by:gallo47
ID: 10709240
Good answer. That's pretty much where my mind was heading but its great to have EE around to get the full scoop. Thanks!
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question