• Post Reply Bookmark Topic Watch Topic
  • New Topic

is my reasoning correct?  RSS feed

 
Mike Ottinger
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks.

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.
 
Mike Ottinger
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, all of the services and daos are singletons within their respective actions and service classes. These are all wired up using Spring. As to your last comment, I would assume the preferred method then would be to go through each action, service and dao class and properly synchronize each one so as not to place a bottleneck on the action classes.

Thanks
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you have mentioned before is a brut force approach to handle multithreading.
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!