473,796 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 17 '05 #1
8 2822
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" <bg***@lycos.co m> wrote in message news:6e******** *************** **@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
Nov 17 '05 #2
"Gaensh" <bg***@lycos.co m> wrote in message
news:6e******** *************** **@posting.goog le.com...
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.
--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
Nov 17 '05 #3
> 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.
Nov 17 '05 #4
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" <wa*****@yahoo. com> wrote in message
news:c3******** *************** ***@posting.goo gle.com...
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.

Nov 17 '05 #5
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.
Nov 17 '05 #6
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" <wa*****@yahoo. com> wrote in message
news:c3******** *************** ***@posting.goo gle.com...
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.

Nov 17 '05 #7
"Frank Drebin" <no*****@imsick ofspam.com> wrote in message news:<36******* *************** @newssvr28.news .prodigy.com>.. .
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 ?
Q4. Is ASP.Net / webservice is a multithreaded apllication ?
A: Again, yes - if you create your own threads.
"Gaensh" <bg***@lycos.co m> wrote in message
news:6e******** *************** **@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

Nov 17 '05 #8
You may already have seen my previous response, but just in case you
haven't:

"Gaensh" <bg***@lycos.co m> wrote in message
news:6e******** *************** ***@posting.goo gle.com...
"Frank Drebin" <no*****@imsick ofspam.com> wrote in message news:<36******* *************** @newssvr28.news .prodigy.com>.. .
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 ?


Yes! Each request is serviced on a thread from the thread pool.
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 ?


"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 .
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(DataAcc essHelper))
{
if (instance == null)
{
instance = new DataAccessHelpe r();
}
}
}
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?
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"].
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.
Q4. Is ASP.Net / webservice is a multithreaded apllication ?
A: Again, yes - if you create your own threads.
Yes, always, regardless of whether you create your own threads.

--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
"Gaensh" <bg***@lycos.co m> wrote in message
news:6e******** *************** **@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

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2500
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
21
2469
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this...
12
2454
by: solex | last post by:
Hello, I am trying to model a session object that is essentially a collection of different items (connection string, user name, maps etc.) I would like this session object to be available to other objects within my client application. I can do one of two things (1) make the session object a singleton (2) pass the session object to the methods that need them. Option 2 is a bit more complicated and messy then option 1. My other goal...
7
5708
by: INeedADip | last post by:
I want to get some feedback so don't hold back. I have a webservice that is responsible for Formatting a ton of information that is then queried by other applications (agents) that utilize it (the info). All this "formatting" and logic I am talking about needs to be done in a centralized place and accessed by the agents. That is why we chose to go with a web service. Now...all this formatting needs to be done and held in memory so...
2
2422
by: baba | last post by:
Hi all, I'm quite new to C#. I am trying to implement some basics reusable classes using this language and the .NET Framework technology. What I'm trying to do now is to implement a singleton class. I did have a look at the Microsoft "Patterns and Practices" article "Implementing Singleton in C#" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/ImpSingletonInCsharp.asp)
11
1868
by: donet programmer | last post by:
Can somebody comment on the usage of the singleton pattern for maintaining session variables?? Thanks
3
18255
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
4
1352
by: John Sheppard | last post by:
Hello, I am working on a 3teired project, data,bizlogic, userinterface...I have a global class that at current is being passed around as parameters, this is, to say the least messy... I am reading up about singletons, but I wonder where to place it? Should I stick it in with the userinterface....how do I make it globally accessable if its in a different layer/project...
2
3929
by: Tom | last post by:
In a web-application I need to read some configuration from a database. I like to write a configuration class which will be implemented as thread-safe singleton. I connect through ODBC to the database. So to be able to do that I need to pass the Datasource Name. How can I create a singleton that has a constructor with a parameter? Standard singleton pattern always use the default constructor. To provide an init(string datasource_name)...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.