Little clarification

  • Thread starter Thread starter PokerMan
  • Start date Start date
P

PokerMan

Hi

Ok in my app i have some classes that will only ever have one instance. All
of which i make Singletons.

I also have another class that acts like an easy access wrapper for any db
access. Again this is a one time instance.

If it wasn't a one only instance is there a danger in db access? Such as two
threads calling the same method and updating something incorrectly?

Should i make that class a singleton, or make all the methods in that class
static? Since a singleton returns a static instance, does that not do the
same as setting all methods to static?

To be clear an example of one method is like this:

public DataSet GetCustomer()
{
return runProcedure("sp_Customers");
}

Thanks in advance
 
It is more difficult to create synchronized shared class than multiple
instance classes, and thus should be generally avoided.
There are reasons for singletons (or static classes. They are the same), but
quite limited. But they are design decisions, not a runtime matter.
Singleton or static does not mean only one thread at a time can execute the
method. If you want that, you need to synchronize the accesses (look for
lock keyword).
If you are programming for multiple threads, you need to be more concerned
with thread synchronization (shared data) than singletons or static
classes/methods.

For example, your function
public DataSet GetCustomer()
{
return runProcedure("sp_Customers");
}

could be (I don't know what runProcedure does) thread safe, since it does
not access any shared (public) data.
If also runProcedure() does not access any class level variables/data, you
can call this function from any number of threads you like.
And from as many instances you like. If the class in which the function
belongs does not have instance data (i.e. has no shared data), it makes sens
to make it a static class, because there would not be any instance data
making an instance quite useless.
 
Ah ok. I do use many threads yes. But so far i have been locking when i need
to.

Somy db accessobject should be a singleton, only evenr need one instance and
i dont want multiple ones all over the place so thats fine.

Butas for loicking the thread. A user reading back data, it doesnt matter.
Inserts and updates however these should be locked right?

An issue i once had was when 2 users in an app would do an update at a
similar time on different records of the database. Yet the data would
somehow cross, some of one users data went into that row and some of the
other users. I always assumed this was some kind of cross thread issue
causing it.

For safetys sake them, rule of thumb. Always lock access to the db method if
it is an updater, insert method. but not to worry so much on a select
statement method.
 

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

Back
Top