?
Solved

ASP.Net / ADO connection management

Posted on 2004-03-29
2
Medium Priority
?
597 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
dfiala13 earned 1000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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/…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

762 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