Originally posted by Mike Ottinger:
I have three layers to my web app, an actions, service and dao layer. In order to ensure synchronized threading throughout, if I synchronize off of my service object from within the action class, doesn't this cause my code within the service and dao layers to be threadsafe? My actions are the only entry points into these service and dao classes.
Do you have only a single instance of any service class?
Does only one service instance refers to a DAO instance?
If you replied yes to all of the above question, then your approach will work else not.
However, this is not the recommended way as this would mean that your whole application is single threaded and it will never scale.
Ideally, one should code with multi-threading in mind and its not a good idea to first write the whole code and then take up a task to make all the classes thread safe!
Enough of expecting an ideal world!
Now, the correct approach will be to run through all the classes and identify critical sections of the code i.e. the code that may fail in a multi-threaded scenario. Typically, it would be when you do a set of processing based on a state of an object shared between different threads. Then you need to synchronize these critical sections on appropriate object instances.
It is a huge topic to discuss as to how to make a code thread safe but probably the above will give you some idea.
Best of luck !
[ August 22, 2007: Message edited by: Nitesh Kant ]