Singleton pattern in asp.net application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gaensh

    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 DataAccessHelpe r
    {
    private static DataAccessHelpe r instance;
    /// <summary>
    /// public property that can only get the single instance of this
    class.
    /// </summary>
    public static DataAccessHelpe r 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.WaitOn e();
    if(instance == null)
    instance = new DataAccessHelpe r();
    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 Drebin

    #2
    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_OnS tart (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" <bgy75@lycos.co m> wrote in message news:6e338a5f.0 308262251.ae086 b6@posting.goog le.com...
    HI,
    I have a singleton pattern to acess my database the following is
    the sample code use to implement singleton pattern
    public class DataAccessHelpe r
    {
    private static DataAccessHelpe r instance;
    /// <summary>
    /// public property that can only get the single instance of this
    class.
    /// </summary>
    public static DataAccessHelpe r 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.WaitOn e();
    if(instance == null)
    instance = new DataAccessHelpe r();
    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

    Comment

    • John Saunders

      #3
      Re: Singleton pattern in asp.net application

      "Gaensh" <bgy75@lycos.co m> wrote in message
      news:6e338a5f.0 308262251.ae086 b6@posting.goog le.com...[color=blue]
      > Q1. Does asp.net/webservice generate multithreaded scenario. ?[/color]
      Yes. Each request is serviced by a thread from the thread pool.
      [color=blue]
      > Q2. Does using mutex slowes the reponse time of the request. ?[/color]
      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.
      [color=blue]
      > Q3. Should singleton be used in asp.net scenario ?[/color]
      It can be. There's no general rule or pattern about this.
      [color=blue]
      > Q4. Is ASP.Net / webservice is a multithreaded apllication ?[/color]
      Yes.
      --
      John Saunders
      Internet Engineer
      john.saunders@s urfcontrol.com


      Comment

      • Matt Hartman

        #4
        Re: Singleton pattern in asp.net application

        > Why only[color=blue]
        > let one car at a time onto a freeway, when the freeway can handle
        > thousands?[/color]

        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.

        Comment

        • Drebin

          #5
          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" <warty_2@yahoo. com> wrote in message
          news:c320a673.0 308270636.223af 29e@posting.goo gle.com...[color=blue][color=green]
          > > Why only
          > > let one car at a time onto a freeway, when the freeway can handle
          > > thousands?[/color]
          >
          > 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.[/color]


          Comment

          • Matt Hartman

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

            Comment

            • Drebin

              #7
              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" <warty_2@yahoo. com> wrote in message
              news:c320a673.0 308281218.6ac9a 640@posting.goo gle.com...[color=blue]
              > 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.[/color]


              Comment

              • Gaensh

                #8
                Re: Singleton pattern in asp.net application

                "Frank Drebin" <noemail@imsick ofspam.com> wrote in message news:<3603b.343 69$Vx2.14974252 @newssvr28.news .prodigy.com>.. .[color=blue]
                > Q1. Does asp.net/webservice generate multithreaded scenario. ?
                > A: It can, if you create your own threads.[/color]
                Q1.1 withing the application we are not creating any threads. will
                aspnet process create a thread for each request ?
                [color=blue]
                > 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?[/color]

                Q2.1 The question of mutex comes what is the best locking machanism
                to be use ?

                Q2.2 As you can see in the code that was sent if there is no
                instance then only mutex is created this would happen for the first
                request, am I wrong ?
                [color=blue]
                > 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?[/color]

                Q3.1 My assumption is that singleton class will be created for per
                process and the same process is being shared by all users ?
                [color=blue]
                > Q4. Is ASP.Net / webservice is a multithreaded apllication ?
                > A: Again, yes - if you create your own threads.
                >
                >
                > "Gaensh" <bgy75@lycos.co m> wrote in message
                > news:6e338a5f.0 308262251.ae086 b6@posting.goog le.com...
                > HI,
                > I have a singleton pattern to acess my database the following is
                > the sample code use to implement singleton pattern
                > public class DataAccessHelpe r
                > {
                > private static DataAccessHelpe r instance;
                > /// <summary>
                > /// public property that can only get the single instance of this
                > class.
                > /// </summary>
                > public static DataAccessHelpe r 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.WaitOn e();
                > if(instance == null)
                > instance = new DataAccessHelpe r();
                > 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[/color]

                Comment

                • John Saunders

                  #9
                  Re: Singleton pattern in asp.net application

                  You may already have seen my previous response, but just in case you
                  haven't:

                  "Gaensh" <bgy75@lycos.co m> wrote in message
                  news:6e338a5f.0 308282336.72e28 ad3@posting.goo gle.com...[color=blue]
                  > "Frank Drebin" <noemail@imsick ofspam.com> wrote in message[/color]
                  news:<3603b.343 69$Vx2.14974252 @newssvr28.news .prodigy.com>.. .[color=blue][color=green]
                  > > Q1. Does asp.net/webservice generate multithreaded scenario. ?
                  > > A: It can, if you create your own threads.[/color]
                  > Q1.1 withing the application we are not creating any threads. will
                  > aspnet process create a thread for each request ?[/color]

                  Yes! Each request is serviced on a thread from the thread pool.
                  [color=blue][color=green]
                  > > 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?[/color]
                  >
                  > Q2.1 The question of mutex comes what is the best locking machanism
                  > to be use ?[/color]

                  "It depends." (anon.)

                  ..NET has several mechanisms. I usually start off with the lock statement in
                  C# (synclock in VB.NET?) and then modify as performance requires, unless
                  another mechanism is obvious. I never do "lock(this){m_C ounter++;}, for
                  instance, but instead would use Interlocked.Inc rement .
                  [color=blue]
                  > Q2.2 As you can see in the code that was sent if there is no
                  > instance then only mutex is created this would happen for the first
                  > request, am I wrong ?[/color]

                  Actually, that doesn't look right. useMutex is a local variable. No other
                  thread will _ever_ use that same mutex. Instead, try:
                  if (instance == null)
                  {
                  lock (typeof(DataAcc essHelper))
                  {
                  if (instance == null)
                  {
                  instance = new DataAccessHelpe r();
                  }
                  }
                  }
                  [color=blue][color=green]
                  > > 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?[/color][/color]

                  I believe you'll find that there will be one instance of your static
                  "instance" per AppDomain. I'm not certain, though.

                  You can also use Application state:
                  Application["DataAccessHelp er.instance"].
                  [color=blue]
                  > Q3.1 My assumption is that singleton class will be created for per
                  > process and the same process is being shared by all users ?[/color]

                  See above.
                  [color=blue][color=green]
                  > > Q4. Is ASP.Net / webservice is a multithreaded apllication ?
                  > > A: Again, yes - if you create your own threads.[/color][/color]

                  Yes, always, regardless of whether you create your own threads.

                  --
                  John Saunders
                  Internet Engineer
                  john.saunders@s urfcontrol.com
                  [color=blue][color=green]
                  > > "Gaensh" <bgy75@lycos.co m> wrote in message
                  > > news:6e338a5f.0 308262251.ae086 b6@posting.goog le.com...
                  > > HI,
                  > > I have a singleton pattern to acess my database the following is
                  > > the sample code use to implement singleton pattern
                  > > public class DataAccessHelpe r
                  > > {
                  > > private static DataAccessHelpe r instance;
                  > > /// <summary>
                  > > /// public property that can only get the single instance of this
                  > > class.
                  > > /// </summary>
                  > > public static DataAccessHelpe r 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.WaitOn e();
                  > > if(instance == null)
                  > > instance = new DataAccessHelpe r();
                  > > 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[/color][/color]


                  Comment

                  Working...