singleton

F

Fabio R.

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
 
M

Matt Berther

Hello Fabio R." fabio81(at)yahoo.com,

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

dwight0

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
 
F

Fabio R.

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
 
K

Kevin Spencer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top