Solved

ASP.Net / ADO connection management

Posted on 2004-03-29
2
576 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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