473,406 Members | 2,220 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,406 software developers and data experts.

Little clarification

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
Feb 9 '07 #1
2 1220
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.

"PokerMan" <no****@pokercat.co.ukha scritto nel messaggio
news:Ol**************@TK2MSFTNGP02.phx.gbl...
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

Feb 9 '07 #2
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.

"Laura T." <LT@NOWHERE.COMwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.

"PokerMan" <no****@pokercat.co.ukha scritto nel messaggio
news:Ol**************@TK2MSFTNGP02.phx.gbl...
>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


Feb 9 '07 #3

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

Similar topics

3
by: Wes | last post by:
I am trying to secure different files, mostly pdf, so only the person suppose to see the file that was designed for that individual can see it. I am using sessions to secure the actual web pages,...
4
by: Shea Martin | last post by:
Which of the following do I use delete instead of just delete. //1.) // not sure about this one, as char is of size 1 char *str = new char; //2.) //not sure about this one, as it is a...
3
by: John D. Sanders | last post by:
I have just upgraded MySQL from version 3.23 to version 4.1.8 and now I am getting the following error when I try to run a script that worked: install_driver(mysql) failed: Can't load...
2
by: Ethan | last post by:
This is a clarification of a previous message, which was not expressed very well. I have a set of checkboxes near the bottom of the page and a function that checks or unchecks all of them. But when...
9
by: Adam | last post by:
Hi, I am having problems having an include file rendered in my browser (IE6). The include file contains <A href> tags to be used as a navigation bar in the footer of a .html file. I am...
3
by: ma740988 | last post by:
Consider the 'C' source. void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal) { doorBellDetected = doorBellVal; } void slRcv() { starLinkOpenStruct myOpenStruct;
3
by: solomon_13000 | last post by:
> Wonthaggi Civic Theatre 'WCT' Case Study > > The town of Wonthaggi has a theatre which is owned and > operated by the local council, it is called the > Wonthaggi Civic Theatre (WCT) and a wide...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
2
by: ravir | last post by:
Hi, I am new to this group. I am working in Perl and shellscripts. I have a clarification regarding perl grep and pattern matching. I am writing a perl script to automate the process of code...
2
by: davidson1 | last post by:
Hai friends....the below code is working well for sending sms from VS2008 C# windows Application...........but i want the same to work in ASP.NET(vb)............can anyone help me....I converted the...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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,...
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
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,...
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
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...
0
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...

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.