views:

100

answers:

2

Is Concurrent method access in singleton class thread safe?

I am using Spring framework (MVC) with the default scope as Singleton.

For example, if the controller of my web application is a Singleton class, are the methods declared to access Model/Business/DB classes thread safe, if accessed by multiple threads at the same time? How about calls to DB for database access/update from these methods in the Controller?

Need guidance and I aprreciate any help/suggestions for this issue.

+8  A: 

No, Singleton does not guarantee thread safety. You have to manage that yourself.

The best way to do it is to make Singleton's stateless. If there's no shared state you're fine.

duffymo
+1 but I would like to add that it does not guarantee non thread safety either. The best option is see what the documentation says.
Scott Chamberlain
A: 

Actually singletons are more likely to be not thread safe as their scope and life time are usually greater than other objects

vc 74