Solved

ASP.Net / ADO connection management

Posted on 2004-03-29
2
559 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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now