When to use a singleton class?

Discussion in 'ASP .Net Web Services' started by Ben Ong, Mar 8, 2005.

  1. Ben Ong

    Ben Ong Guest

    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.
     
    Ben Ong, Mar 8, 2005
    #1
    1. Advertising

  2. Ben Ong

    Dan Rogers Guest

    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.
    --------------------
    >From: "Ben Ong" <>
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    >Subject: When to use a singleton class?
    >Date: 8 Mar 2005 12:31:44 -0800
    >Organization: http://groups.google.com
    >Lines: 14
    >Message-ID: <>
    >NNTP-Posting-Host: 216.138.125.82
    >Mime-Version: 1.0
    >Content-Type: text/plain; charset="iso-8859-1"
    >X-Trace: posting.google.com 1110313909 3661 127.0.0.1 (8 Mar 2005 20:31:49

    GMT)
    >X-Complaints-To:
    >NNTP-Posting-Date: Tue, 8 Mar 2005 20:31:49 +0000 (UTC)
    >User-Agent: G2/0.2
    >Complaints-To:
    >Injection-Info: l41g2000cwc.googlegroups.com; posting-host=216.138.125.82;
    > posting-account=htBM7g0AAAA0Xqwoj1RzfblmXvGaELh4
    >Path:

    TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED02.phx.gbl!tornado.fa
    stwebnet.it!tiscali!newsfeed1.ip.tiscali.net!news.glorb.com!postnews.google.
    com!l41g2000cwc.googlegroups.com!not-for-mail
    >Xref: TK2MSFTNGXA02.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webservices:28509
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    >
    >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.
    >
    >
     
    Dan Rogers, Mar 8, 2005
    #2
    1. Advertising

  3. Ben Ong

    Ben Ong Guest

    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
     
    Ben Ong, Mar 8, 2005
    #3
  4. 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.


    --
    Manohar Kamath
    Editor, .netWire
    www.dotnetwire.com


    "Ben Ong" <> wrote in message
    news:...
    > 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.
    >
     
    Manohar Kamath, Mar 8, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,334
    Proton Projects - Moin
    Mar 27, 2007
  2. Wilhelm
    Replies:
    1
    Views:
    185
  3. Trans
    Replies:
    12
    Views:
    304
    Robert Klemme
    Sep 14, 2007
  4. Paul McMahon
    Replies:
    3
    Views:
    221
    David A. Black
    Jun 9, 2008
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    220
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page