473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Singleton in web service?

Bob
Does anyone know of any adverse affects of using singleton data access
objects in an ASP.NET Web Service? I've been forced to implement it this way
but it just doesn't seem right to me. I haven't been able to find a single
example of someone doing it this way and that's usually not a good sign. I'm
afraid that there may be subtle problems that won't show up quickly because
the production environment will not be hitting the web service at a high
rate. Of course I'm also concerned about not subtle problems as well but
they're easier to find.

--
Thanks,
Bob

Nov 19 '05 #1
5 3951
As long as the singleton doesn't maintain state, it's a reasonable
design. The Microsoft Data Access block class uses all static methods
- which is not a textbook singleton design, but does function the
similarly.

The real problem is state. If the singleton uses fields to store data
between method calls, then you need to be multi-threaded safe, which
can be a performance hit. The DAB does maintain some state (it caches
stored procedure parameter information like names and types) and it
uses locking to avoid corruptions. I'd think MS tested this and
determined it was worth the additional complexity, and overall in this
case the code probably performs better with the caching.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 11:05:04 -0800, Bob
<Bo*@discussions.microsoft.com> wrote:
Does anyone know of any adverse affects of using singleton data access
objects in an ASP.NET Web Service? I've been forced to implement it this way
but it just doesn't seem right to me. I haven't been able to find a single
example of someone doing it this way and that's usually not a good sign. I'm
afraid that there may be subtle problems that won't show up quickly because
the production environment will not be hitting the web service at a high
rate. Of course I'm also concerned about not subtle problems as well but
they're easier to find.


Nov 19 '05 #2
Bob
Thanks Scott. I'm not maintaining state and the consruction is thread safe
so I'm alright there. Do you understand where a singleton is maintained
between calls to a web service? We're not storing the instance in
application state or anywhere else explicitly but it does seem to persist.
This is what worries me the most about this design, not knowing how it's
persisted. I always thought that web services should be designed without any
state between calls, except for remoting singletons which this is not. What
gets me is that the data access classes are tiny and the overhead to create
and destroy on each call it would be trivial compared to the actual data
access.

"Scott Allen" wrote:
As long as the singleton doesn't maintain state, it's a reasonable
design. The Microsoft Data Access block class uses all static methods
- which is not a textbook singleton design, but does function the
similarly.

The real problem is state. If the singleton uses fields to store data
between method calls, then you need to be multi-threaded safe, which
can be a performance hit. The DAB does maintain some state (it caches
stored procedure parameter information like names and types) and it
uses locking to avoid corruptions. I'd think MS tested this and
determined it was worth the additional complexity, and overall in this
case the code probably performs better with the caching.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 11:05:04 -0800, Bob
<Bo*@discussions.microsoft.com> wrote:
Does anyone know of any adverse affects of using singleton data access
objects in an ASP.NET Web Service? I've been forced to implement it this way
but it just doesn't seem right to me. I haven't been able to find a single
example of someone doing it this way and that's usually not a good sign. I'm
afraid that there may be subtle problems that won't show up quickly because
the production environment will not be hitting the web service at a high
rate. Of course I'm also concerned about not subtle problems as well but
they're easier to find.


Nov 19 '05 #3
Are there any static fields that reference the singleton? If there is
any outstanding reference to the object the garabage collector won't
reclaim the object. How do you get a reference to the singleton to
make a data access call?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 16:07:01 -0800, Bob
<Bo*@discussions.microsoft.com> wrote:
Thanks Scott. I'm not maintaining state and the consruction is thread safe
so I'm alright there. Do you understand where a singleton is maintained
between calls to a web service? We're not storing the instance in
application state or anywhere else explicitly but it does seem to persist.
This is what worries me the most about this design, not knowing how it's
persisted. I always thought that web services should be designed without any
state between calls, except for remoting singletons which this is not. What
gets me is that the data access classes are tiny and the overhead to create
and destroy on each call it would be trivial compared to the actual data
access.

"Scott Allen" wrote:
As long as the singleton doesn't maintain state, it's a reasonable
design. The Microsoft Data Access block class uses all static methods
- which is not a textbook singleton design, but does function the
similarly.

The real problem is state. If the singleton uses fields to store data
between method calls, then you need to be multi-threaded safe, which
can be a performance hit. The DAB does maintain some state (it caches
stored procedure parameter information like names and types) and it
uses locking to avoid corruptions. I'd think MS tested this and
determined it was worth the additional complexity, and overall in this
case the code probably performs better with the caching.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 11:05:04 -0800, Bob
<Bo*@discussions.microsoft.com> wrote:
>Does anyone know of any adverse affects of using singleton data access
>objects in an ASP.NET Web Service? I've been forced to implement it this way
>but it just doesn't seem right to me. I haven't been able to find a single
>example of someone doing it this way and that's usually not a good sign. I'm
>afraid that there may be subtle problems that won't show up quickly because
>the production environment will not be hitting the web service at a high
>rate. Of course I'm also concerned about not subtle problems as well but
>they're easier to find.



Nov 19 '05 #4
Bob
Actually I have been keeping the singleton in a non-static field in the class
that uses the singleton. The constructor of the using class gets the
singleton instance via an "Instance" method of the singleton and assigns it
to the field. Member methods then use the member field for data access. To
make matters a little more complicated there are 2 levels of singletons. The
web service exposes 2 classes, each class uses a singleton instance of a
manager class which in turn uses a singleton instance of a data access class
(all the singletons are to different classes). Do you think it is better to
just get the instance inside each method that is going to use a singleton?
That is easy to do, I was just trying to save a few lines of code because one
of the classes has quite a few methods exposed and each needs access to its
singleton. I understand how these things work in regular apps (although I
generally avoid them) but I don't really understand the life cycle and
application domain of web services. Thanks for your help, I really
appreciate it.

"Scott Allen" wrote:
Are there any static fields that reference the singleton? If there is
any outstanding reference to the object the garabage collector won't
reclaim the object. How do you get a reference to the singleton to
make a data access call?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 16:07:01 -0800, Bob
<Bo*@discussions.microsoft.com> wrote:
Thanks Scott. I'm not maintaining state and the consruction is thread safe
so I'm alright there. Do you understand where a singleton is maintained
between calls to a web service? We're not storing the instance in
application state or anywhere else explicitly but it does seem to persist.
This is what worries me the most about this design, not knowing how it's
persisted. I always thought that web services should be designed without any
state between calls, except for remoting singletons which this is not. What
gets me is that the data access classes are tiny and the overhead to create
and destroy on each call it would be trivial compared to the actual data
access.

"Scott Allen" wrote:
As long as the singleton doesn't maintain state, it's a reasonable
design. The Microsoft Data Access block class uses all static methods
- which is not a textbook singleton design, but does function the
similarly.

The real problem is state. If the singleton uses fields to store data
between method calls, then you need to be multi-threaded safe, which
can be a performance hit. The DAB does maintain some state (it caches
stored procedure parameter information like names and types) and it
uses locking to avoid corruptions. I'd think MS tested this and
determined it was worth the additional complexity, and overall in this
case the code probably performs better with the caching.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 11:05:04 -0800, Bob
<Bo*@discussions.microsoft.com> wrote:

>Does anyone know of any adverse affects of using singleton data access
>objects in an ASP.NET Web Service? I've been forced to implement it this way
>but it just doesn't seem right to me. I haven't been able to find a single
>example of someone doing it this way and that's usually not a good sign. I'm
>afraid that there may be subtle problems that won't show up quickly because
>the production environment will not be hitting the web service at a high
>rate. Of course I'm also concerned about not subtle problems as well but
>they're easier to find.


Nov 19 '05 #5
The app domain in a web service is a little more 'fragile' in a way,
or perhaps more 'robust' - depending on your point of view. :)

I say this because there are many forces that can control the lifetime
of the app domain. For instance, IIS can be configured to recycle the
app domain if certain thresholds are met, the runtime will start a new
app domain if the config file changes, and so on.

I discussed this a bit in an article, but I think you'll know this.
http://odetocode.com/Articles/305.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Wed, 22 Dec 2004 18:11:02 -0800, Bob
<Bo*@discussions.microsoft.com> wrote:
Actually I have been keeping the singleton in a non-static field in the class
that uses the singleton. The constructor of the using class gets the
singleton instance via an "Instance" method of the singleton and assigns it
to the field. Member methods then use the member field for data access. To
make matters a little more complicated there are 2 levels of singletons. The
web service exposes 2 classes, each class uses a singleton instance of a
manager class which in turn uses a singleton instance of a data access class
(all the singletons are to different classes). Do you think it is better to
just get the instance inside each method that is going to use a singleton?
That is easy to do, I was just trying to save a few lines of code because one
of the classes has quite a few methods exposed and each needs access to its
singleton. I understand how these things work in regular apps (although I
generally avoid them) but I don't really understand the life cycle and
application domain of web services. Thanks for your help, I really
appreciate it.

"Scott Allen" wrote:
Are there any static fields that reference the singleton? If there is
any outstanding reference to the object the garabage collector won't
reclaim the object. How do you get a reference to the singleton to
make a data access call?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 16:07:01 -0800, Bob
<Bo*@discussions.microsoft.com> wrote:
>Thanks Scott. I'm not maintaining state and the consruction is thread safe
>so I'm alright there. Do you understand where a singleton is maintained
>between calls to a web service? We're not storing the instance in
>application state or anywhere else explicitly but it does seem to persist.
>This is what worries me the most about this design, not knowing how it's
>persisted. I always thought that web services should be designed without any
>state between calls, except for remoting singletons which this is not. What
>gets me is that the data access classes are tiny and the overhead to create
>and destroy on each call it would be trivial compared to the actual data
>access.
>
>"Scott Allen" wrote:
>
>> As long as the singleton doesn't maintain state, it's a reasonable
>> design. The Microsoft Data Access block class uses all static methods
>> - which is not a textbook singleton design, but does function the
>> similarly.
>>
>> The real problem is state. If the singleton uses fields to store data
>> between method calls, then you need to be multi-threaded safe, which
>> can be a performance hit. The DAB does maintain some state (it caches
>> stored procedure parameter information like names and types) and it
>> uses locking to avoid corruptions. I'd think MS tested this and
>> determined it was worth the additional complexity, and overall in this
>> case the code probably performs better with the caching.
>>
>> --
>> Scott
>> http://www.OdeToCode.com/blogs/scott/
>>
>> On Tue, 21 Dec 2004 11:05:04 -0800, Bob
>> <Bo*@discussions.microsoft.com> wrote:
>>
>> >Does anyone know of any adverse affects of using singleton data access
>> >objects in an ASP.NET Web Service? I've been forced to implement it this way
>> >but it just doesn't seem right to me. I haven't been able to find a single
>> >example of someone doing it this way and that's usually not a good sign. I'm
>> >afraid that there may be subtle problems that won't show up quickly because
>> >the production environment will not be hitting the web service at a high
>> >rate. Of course I'm also concerned about not subtle problems as well but
>> >they're easier to find.
>>
>>



Nov 19 '05 #6

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

Similar topics

9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
21
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...
12
by: keepyourstupidspam | last post by:
Hi, I am writing a windows service. The code runs fine when I start the service when my machine is running but it fails to start automatically when the machine reboots. The code bombs out when...
2
by: mma | last post by:
hello everyone, i have a solution that contains the following project types: 1. Class library contains a singleton class that handles the data access (sql server 2005). 2. Windows service....
7
by: fredd00 | last post by:
Hi I'm just starting with singleton and would like to implement in my new web app. I have a question lets say i create a singleton DataHelper that holds a static SqlConnection object to...
9
by: oleggus | last post by:
I hope, I misunderstood some basics here and it is easy to solve.. I need a singleton object running on server which can be used(write and read) by different client interfaces - for example there...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.