Singleton pattern in asp.net application

  • Thread starter Thread starter Gaensh
  • Start date Start date
G

Gaensh

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


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 said:
Q1. Does asp.net/webservice generate multithreaded scenario. ?
Yes. Each request is serviced by a thread from the thread pool.
Q2. Does using mutex slowes the reponse time of the request. ?
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.
Q3. Should singleton be used in asp.net scenario ?
It can be. There's no general rule or pattern about this.
Q4. Is ASP.Net / webservice is a multithreaded apllication ?
Yes.
 
Why only
let one car at a time onto a freeway, when the freeway can handle
thousands?

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.
 
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!!
 
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.
 
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.
 
Frank Drebin said:
Q1. Does asp.net/webservice generate multithreaded scenario. ?
A: It can, if you create your own threads.
Q1.1 withing the application we are not creating any threads. will
aspnet process create a thread for each request ?
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?

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

Q3.1 My assumption is that singleton class will be created for per
process and the same process is being shared by all users ?
 
You may already have seen my previous response, but just in case you
haven't:

Gaensh said:
"Frank Drebin" <[email protected]> wrote in message
Q1.1 withing the application we are not creating any threads. will
aspnet process create a thread for each request ?

Yes! Each request is serviced on a thread from the thread pool.
Q2.1 The question of mutex comes what is the best locking machanism
to be use ?

"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_Counter++;}, for
instance, but instead would use Interlocked.Increment .
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 ?

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(DataAccessHelper))
{
if (instance == null)
{
instance = new DataAccessHelper();
}
}
}

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["DataAccessHelper.instance"].
Q3.1 My assumption is that singleton class will be created for per
process and the same process is being shared by all users ?

See above.

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

--
John Saunders
Internet Engineer
(e-mail address removed)
 
Back
Top