Wednesday, March 28, 2012

singleton

Probably a newbie questions...
If I have a singleton class which read and write to a database, what happens
if 2 or more clients call the same methods at the same time?
If one method is:

public DataSet GetData(string sql_query){
some database operations...
return _dataset;
}

and client 1 call:
GetData("select * from users");
and client 2 call:
GetData("select * from cars");

is there any possibilities that client 1 receives the cars dataset or
viceversa?
Thanks, in advance
FabioHello Fabio R." fabio81(at)yahoo.com,

As long as your GetData method does not modify/user any class level variables,
you should be threadsafe.

--
Matt Berther
http://www.mattberther.com

> Probably a newbie questions...
> If I have a singleton class which read and write to a database, what
> happens
> if 2 or more clients call the same methods at the same time?
> If one method is:
> public DataSet GetData(string sql_query){
> some database operations...
> return _dataset;
> }
> and client 1 call:
> GetData("select * from users");
> and client 2 call:
> GetData("select * from cars");
> is there any possibilities that client 1 receives the cars dataset or
> viceversa?
> Thanks, in advance
> Fabio
I was wondering the same thing.
Does anyone know the scope of the class?
What about on multiple processors?
Separate threads/appdomains etc ?

Thanks
-Dwight
I have a couple articles discussing thread safety and static memebers:

http://odetocode.com/Articles/313.aspx
http://odetocode.com/Articles/314.aspx

From the code you've outlines you should not have any problems, as the
code appears to only use variables local to the method.

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

On Thu, 27 Jan 2005 01:02:40 +0100, "Fabio R." <fabio81(at)yahoo.com>
wrote:

>Probably a newbie questions...
>If I have a singleton class which read and write to a database, what happens
>if 2 or more clients call the same methods at the same time?
>If one method is:
>public DataSet GetData(string sql_query){
>some database operations...
>return _dataset;
>}
>and client 1 call:
> GetData("select * from users");
>and client 2 call:
> GetData("select * from cars");
>is there any possibilities that client 1 receives the cars dataset or
>viceversa?
>Thanks, in advance
>Fabio
Thanks to all.
So, a "design" question regarding this topic:
it's better to have, in example, all the 10/15 classes of the data layer as
singleton or let the users create their own istances at every request or put
all the istance in a "session singleton" as in
http://aspalliance.com/articleViewer.aspx?aId=70 article?
The classes has only methods with locale variables.
What's better for the server memory/performance?
Fabio

> From the code you've outlines you should not have any problems, as the
> code appears to only use variables local to the method.
> If I have a singleton class which read and write to a database, what
> happens if 2 or more clients call the same methods at the same time?

In fact, nothing on a computer happens at the same time. A computer can do
precisely one calculation at a time. Multi-tasking is accomplished by
looping in the OS. Network requests are queued. And each client is uniquely
identified within their own thread. So, nothing special happens when 2 or
more clients call the same methods "at the same time."

You might ask the same question of your Operating System. It is
multi-tasking, and many applications may be making API calls to the OS "at
the same time." However, each application somehow manages to keep track of
its own "stuff."

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Fabio R." <fabio81(at)yahoo.com> wrote in message
news:u6wv4NABFHA.2428@.TK2MSFTNGP14.phx.gbl...
> Probably a newbie questions...
> If I have a singleton class which read and write to a database, what
> happens if 2 or more clients call the same methods at the same time?
> If one method is:
> public DataSet GetData(string sql_query){
> some database operations...
> return _dataset;
> }
> and client 1 call:
> GetData("select * from users");
> and client 2 call:
> GetData("select * from cars");
> is there any possibilities that client 1 receives the cars dataset or
> viceversa?
> Thanks, in advance
> Fabio

0 comments:

Post a Comment