Professional Web Applications Themes

When to use a singleton class? - ASP.NET Web Services

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....

  1. #1

    Default When to use a singleton class?

    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 Guest

  2. #2

    Default RE: When to use a singleton class?

    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" <ben.ongnatoil.com>
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservic es
    >Subject: When to use a singleton class?
    >Date: 8 Mar 2005 12:31:44 -0800
    >Organization: [url]http://groups.google.com[/url]
    >Lines: 14
    >Message-ID: <1110313904.801246.63500l41g2000cwc.googlegroups. com>
    >NNTP-Posting-Host: 216.138.125.82
    >Mime-Version: 1.0
    >Content-Type: text/plain; cht="iso-8859-1"
    >X-Trace: posting.google.com 1110313909 3661 127.0.0.1 (8 Mar 2005 20:31:49
    GMT)
    >X-Complaints-To: [email]groups-abusegoogle.com[/email]
    >NNTP-Posting-Date: Tue, 8 Mar 2005 20:31:49 +0000 (UTC)
    >User-Agent: G2/0.2
    >Complaints-To: [email]groups-abusegoogle.com[/email]
    >Injection-Info: l41g2000cwc.googlegroups.com; posting-host=216.138.125.82;
    > posting-account=htBM7g0AAAA0Xqwoj1RzfblmXvGaELh4
    >Path:
    TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSF TFEED02.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.webservic es:28509
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservic es
    >
    >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 Guest

  3. #3

    Default Re: When to use a singleton class?

    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 Guest

  4. #4

    Default Re: When to use a singleton class?

    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
    [url]www.dotnetwire.com[/url]


    "Ben Ong" <ben.ongnatoil.com> wrote in message
    news:1110313904.801246.63500l41g2000cwc.googlegro ups.com...
    > 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 Guest

Similar Threads

  1. How do you build a singleton class in PHP? Can you?
    By DB McGee in forum PHP Development
    Replies: 2
    Last Post: February 12th, 06:54 PM
  2. Singleton pattern not working
    By Henrik S. Hansen in forum PHP Development
    Replies: 2
    Last Post: November 11th, 11:21 PM
  3. Calling singleton from C
    By Dwayne Achee in forum Ruby
    Replies: 1
    Last Post: November 7th, 09:51 AM
  4. Log4r and Ruby 1.8.0 in Singleton problems
    By David Heinemeier Hansson in forum Ruby
    Replies: 14
    Last Post: August 5th, 04:24 PM
  5. How to do this as a singleton select?
    By Ryan Gaffuri in forum Oracle Server
    Replies: 5
    Last Post: January 9th, 02:33 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139