When to use a singleton class?

B

Ben Ong

Hi,

I created a singleton class in my .NET app to solely handle opening and
closing a connection to my database. I was wondering if this is a
good design or not. And can someone confirm with me: that a singleton
class has only one instance of itself correct? So if i have multiple
users using the same app and they connect through my database
connection class, will only one of the users be able to connect to the
db at a time?

thanks,

ben.
 
D

Dan Rogers

Yeah, that's prolly not a good idea. Try letting the default connection
pooling behavior work. This is automatic if you don't disable it. What
happens is that IIS will keep a pool of connections open - one per
connect-string user/credential pair, and reuse an unoccupied connection
when another connection just like it comes along. If there are different
connects (different user creds) you don't get any benefit under high load -
but for cases where you are making your service log in with a specific set
of credentials, connection pooling is your friend.
--------------------
 
B

Ben Ong

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?

thanks,

ben
 
M

Manohar Kamath

Ben,

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top