Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Singleton Usage

Author  Topic 

jwallz
Starting Member

14 Posts

Posted - 2009-03-31 : 19:25:46
I'm trying to get my head around the concept of the singleton design pattern as applied to managing a web application's database connection. I'm trying to understand it in theory. I know that with .net this isn't much of a concern to built in connection pooling with ado.net. I've read a lot online and there's a lot of different explanations.

From what i gather,the point of a singleton pattern for managing a web app db conn is to limit it to 1 conn per web app or web app thread? My problem is that this doesn't make sense to me. Why limit your app to one connection. I can understand not wanting to open too many but isn't limiting your app to one going to result in bottlenecks during traffic spikes on a busy site? What am i missing here? I'm thinking the point is rather to limit a single page request to one db connection where you may be making multiple db calls to support that page load?

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-03-31 : 20:25:20
Who says the Singleton pattern is the right choice for that? Software design patterns have been heinously misunderstood, and even more heinously misused, by lots of developers. If it doesn't make sense to you, you don't have to use it. I think your argument against it is completely valid.

Here's some opinion that may or may not help:

http://steve.yegge.googlepages.com/singleton-considered-stupid
Go to Top of Page

jwallz
Starting Member

14 Posts

Posted - 2009-03-31 : 21:26:26
Thanks for the link, it was a good read. However i'm still not clear here. I keep running into ambiguous explanations like this one on another forum:
Q - "am i to understand that using this singleton class to open and close a connection would only allow one user connected to the database at a time? Or would it create different instances giving each particular user their own instance of my connection object?"
A- "A singleton has only single instance of itself, but it can still dispense objects. E.g A ConnectionDispenser could be a singleton (only one instance exists), but its GetConnection() method returns a new SqlConnection object everytime it is called. The connection itself is not a singleton."

I'm sure i'm being extremely thick but damn, "a new sql connection object everytime"?? wtf? I'll keep scouring and post back if i ever find a clear explanation.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-03-31 : 21:39:07
I don't think you're being thick, no more than I am anyway. That post sounds like Architecture Wanking 101, or something a college professor/grad student would write, and only they would care about. Honestly I think you're wasting your time trying to understand it, because there's likely nothing to understand, unless you have to do this for a class.
Go to Top of Page

jwallz
Starting Member

14 Posts

Posted - 2009-03-31 : 22:23:17
Eureka! The answer, if it's correct :) is "one connection per web request, not one connection for the entire application". That's what i was trying to clarify.


Go to Top of Page
   

- Advertisement -