Professional Web Applications Themes

Singleton pattern in asp.net application - ASP.NET General

HI, I have a singleton pattern to acess my database the following is the sample code use to implement singleton pattern public class DataAccessHelper { private static DataAccessHelper instance; /// <summary> /// public property that can only get the single instance of this class. /// </summary> public static DataAccessHelper GetInstance { get { // Support multithreaded applications through // "Double checked locking" pattern which avoids // locking every time the method is invoked if(instance == null) { // Only one thread can obtain a mutex Mutex useMutex = new Mutex(true); useMutex.WaitOne(); if(instance == null) instance = new DataAccessHelper(); useMutex.Close(); } ...

  1. #1

    Default Singleton pattern in asp.net application

    HI,
    I have a singleton pattern to acess my database the following is
    the sample code use to implement singleton pattern
    public class DataAccessHelper
    {
    private static DataAccessHelper instance;
    /// <summary>
    /// public property that can only get the single instance of this
    class.
    /// </summary>
    public static DataAccessHelper GetInstance
    {
    get
    { // Support multithreaded applications through
    // "Double checked locking" pattern which avoids
    // locking every time the method is invoked
    if(instance == null)
    {
    // Only one thread can obtain a mutex
    Mutex useMutex = new Mutex(true);
    useMutex.WaitOne();
    if(instance == null)
    instance = new DataAccessHelper();
    useMutex.Close();
    } return instance;
    }
    }
    }
    I am using this in my data layer and our application is 3 tier
    application presentation is asp.net, business is webservice . ?
    Q1. Does asp.net/webservice generate multithreaded scenario. ?
    Q2. Does using mutex slowes the reponse time of the request. ?
    Q3. Should singleton be used in asp.net scenario ?
    Q4. Is ASP.Net / webservice is a multithreaded apllication ?

    your feedback is very imporatant as the client feels that using
    multithread scenario will not be encountered in asp.net/webservice and
    using mutex will slow down the response of the request.

    Thank you all in advance
    Gaensh Guest

  2. #2

    Default Re: Singleton pattern in asp.net application

    Q1. Does asp.net/webservice generate multithreaded scenario. ?
    A: It can, if you create your own threads.

    Q2. Does using mutex slowes the reponse time of the request. ?
    A: It likely will, because you don't need it. You can have as many people accessing (reading and inserting into) the database as you'd like. The only reason I can see doing this, is for an update or delete statement MAYBE - and preventing a deadlock. But that would be just to fix the deadlock problem, 99% of the time - that won't happen. Why only let one car at a time onto a freeway, when the freeway can handle thousands?

    Q3. Should singleton be used in asp.net scenario ?
    A: I don't *think* so, because singleton allows one instance of a class within a PROCESS. So, unless you are handling the singleton on Application_OnStart (which doesn't seem like a good idea) - then all connections will still get thier own instance of your class, which probably undermines what you meant to do. Singleton should be used when you NEED there to be only one instance of the class within a process, not on the whole machine. If you have similar things that you want all class to be able to use - perhaps use Static members?

    Q4. Is ASP.Net / webservice is a multithreaded apllication ?
    A: Again, yes - if you create your own threads.


    "Gaensh" <com> wrote in message news:google.com...
    HI,
    I have a singleton pattern to acess my database the following is
    the sample code use to implement singleton pattern
    public class DataAccessHelper
    {
    private static DataAccessHelper instance;
    /// <summary>
    /// public property that can only get the single instance of this
    class.
    /// </summary>
    public static DataAccessHelper GetInstance
    {
    get
    { // Support multithreaded applications through
    // "Double checked locking" pattern which avoids
    // locking every time the method is invoked
    if(instance == null)
    {
    // Only one thread can obtain a mutex
    Mutex useMutex = new Mutex(true);
    useMutex.WaitOne();
    if(instance == null)
    instance = new DataAccessHelper();
    useMutex.Close();
    } return instance;
    }
    }
    }
    I am using this in my data layer and our application is 3 tier
    application presentation is asp.net, business is webservice . ?
    Q1. Does asp.net/webservice generate multithreaded scenario. ?
    Q2. Does using mutex slowes the reponse time of the request. ?
    Q3. Should singleton be used in asp.net scenario ?
    Q4. Is ASP.Net / webservice is a multithreaded apllication ?

    your feedback is very imporatant as the client feels that using
    multithread scenario will not be encountered in asp.net/webservice and
    using mutex will slow down the response of the request.

    Thank you all in advance
    Frank Guest

  3. #3

    Default Re: Singleton pattern in asp.net application

    "Gaensh" <com> wrote in message
    news:google.com... 
    Yes. Each request is serviced by a thread from the thread pool.
     
    Of course. Everything a program does takes some time. The question is "how
    much time"? The answer is, "try it and find out". What may be too long for
    me may be just fine for your application.
     
    It can be. There's no general rule or pattern about this.
     
    Yes.
    --
    John Saunders
    Internet Engineer
    com


    John Guest

  4. #4

    Default Re: Singleton pattern in asp.net application

    > Why only 

    This is exactly why I see the singleton pattern as perfect for a
    database class (and why I use it myself). You're getting the best of
    both worlds.

    Because each web user gets their own copy of the singleton object, you
    can still have your thousands of cars on the freeway. However, that
    user doesn't recreate database objects (Connection, Command, etc) over
    and over and over to bog down the garbage collector with. They re-use
    the objects defined in the singleton.

    Essentially your user's database access is as free and fun as before
    with connection pooling and the whole bit, but memory is used much
    more conservatively. Instead of me creating a new connection,
    datatable, ........ for each sub/function I call, I just re-use what
    I've made before. So new allocating of memory, no additional garbage
    collecting. It's worked brilliantly for me so far.

    Matt.
    Matt Guest

  5. #5

    Default Re: Singleton pattern in asp.net application

    Sounds like you are trying to re-write connection pooling.... You might be
    interested in this round thing with spokes I'm building.. it will change
    everything!!


    "Matt Hartman" <com> wrote in message
    news:google.com... 
    >
    > This is exactly why I see the singleton pattern as perfect for a
    > database class (and why I use it myself). You're getting the best of
    > both worlds.
    >
    > Because each web user gets their own copy of the singleton object, you
    > can still have your thousands of cars on the freeway. However, that
    > user doesn't recreate database objects (Connection, Command, etc) over
    > and over and over to bog down the garbage collector with. They re-use
    > the objects defined in the singleton.
    >
    > Essentially your user's database access is as free and fun as before
    > with connection pooling and the whole bit, but memory is used much
    > more conservatively. Instead of me creating a new connection,
    > datatable, ........ for each sub/function I call, I just re-use what
    > I've made before. So new allocating of memory, no additional garbage
    > collecting. It's worked brilliantly for me so far.
    >
    > Matt.[/ref]


    Drebin Guest

  6. #6

    Default Re: Singleton pattern in asp.net application

    I'm not sure if you've completely missed the point or if you're just
    trying to be overly critical.

    Plain terms, connection pooling is still being used inside the
    singleton class. The main benefit is not having to instantiate and
    dispose of new SqlClient objects each time you access the database,
    saving both processor and memory use. Additionally, code is more
    concise because you only ever declare those objects once in the entire
    solution.

    Matt.
    Matt Guest

  7. #7

    Default Re: Singleton pattern in asp.net application

    To me, many things in .NET seem all or nothing, and when you take over a
    little bit of it - you end up having to take over ALL of it. And unless you
    are having a problem with connection pooling now, why are you spending time
    with this? And if you ARE having problems with connection pooling - post it
    up here and maybe others can help?

    I'd say so long as what you do, makes sense to you and is defendable - then
    go with it! And I was just joking, I didn't mean to offend - sorry.


    "Matt Hartman" <com> wrote in message
    news:google.com... 


    Drebin Guest

Similar Threads

  1. Replies: 42
    Last Post: May 31st, 06:26 PM
  2. Replies: 6
    Last Post: January 15th, 03:25 PM
  3. Replies: 14
    Last Post: December 15th, 02:14 AM
  4. Singleton pattern not working
    By Henrik S. Hansen in forum PHP Development
    Replies: 2
    Last Post: November 11th, 11:21 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