Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2007

Tip: Looking for answers? Try searching our database.

Create application level SQL connection, HOW?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JP - 11 Jul 2007 20:22 GMT
Is there a way to create an application wide connection object in C# so I
don’t have to create a new one for every page? My issue is that while I can
do this within the scope of a single aspx page. It can really drag down
performance because each portion of the page is made up of user controls,
each with a connection object within its scope. I like to have one public
connection object per user session and just open and close it as I need it,
but scope is preventing the pages from seeing the connection object    

SqlConnection objConnection = new
SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);

Signature

JP
.NET Software Developer

Damien - 12 Jul 2007 07:27 GMT
> Is there a way to create an application wide connection object in C# so I
> don't have to create a new one for every page? My issue is that while I can
[quoted text clipped - 10 lines]
> JP
> .NET Software Developer

Don't do it! Your application won't scale. Trust the connection pool.
What you should do is share your connection string (create a singleton
class that loads it from your config file and exposes it - so that
config is only accessed once). Consider what happens if 1000 users are
using you site simultaneously. Or 10000. Or the fact that the user can
just close the browser, giving you no clue when to close the
connection.

For your user controls, expose a property called ConnectionString (or
similar), which it is the pages responsibility to hook up (so that the
user controls are not dependent on how the underlying connection
string is stored).

Only thing to note is to make sure you close any connections you open
- think Catch blocks.

Damien
JP - 12 Jul 2007 17:08 GMT
I came across several articles that said a global connection was a very bad
idea so I wont do it. Just wigs me out to watch SQL generate 30+ connections
in the pool for a single user request post back when I know for a fact I’m
creating, opening, closing, and disposing of all the connections.  There are
8 user controls on my page, why 30 connections are created when I only need 8
or 10 is beyond me. I figured it was b/c each user control has a
SqlConnection objConnection = new

SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);.

Signature

JP
.NET Software Developer

> > Is there a way to create an application wide connection object in C# so I
> > don't have to create a new one for every page? My issue is that while I can
[quoted text clipped - 28 lines]
>
> Damien
Damien - 13 Jul 2007 08:37 GMT
> I came across several articles that said a global connection was a very bad
> idea so I wont do it. Just wigs me out to watch SQL generate 30+ connections
[quoted text clipped - 5 lines]
>
> SqlConnection(System.Configuration.ConfigurationManager.AppSettings["Connec­tionString"]);.

Yeah, but how many connections does it open for 100 users? Optimizing
for 1 user is rarely a model for a succesful website :-) I'd just
emphasize again to make sure that every opened connection does get
explicitly closed/disposed. Went mad once in one project trying to
find the connection leak once lots of users were using the site.
Course, couldn't reproduce it on my machine with only me using it.

Damien

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.