• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Servlet: Multithreading Issues

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
From the specifications, it noted that a servlet container may snd concurrent requests through the service() method of the servlet and that to handle the requests the developer of the servlet must make adequate provisions for concurrent processing with multiple threads in the service method.

In the event that say we need to have an object shared concurrently by various users, what are the mechanisms to ensure that the state of this object is correct ?
Using SingleThreadModel or synchronize the servive() method do not resolve the issue and using ejb seems to be an overkill.
Any suggestions ?
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I dont know if my reply will help u in any way. Anyhow letme say something about my xperience with servlets.
What u can do is, donot have many private or public variables. If u have any private or public variables, assume that they may contains values when the servlet was called the previous time. So instead of having the servlet implement a singlethreadmodel or instead of synchronizing, better declare all such variables local to the service method
 
Kodo Tan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks for the reply. However, if we were to declare such variables local to the service method, then the state of that variable is not shared and persisted by all the requests.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was presented with a similiar scenario. I just started a new job and one of the applications that my company was using was experiencing problems with data collision. I reviewed the servlets that the application was using and noticed many private non-static variables declared. I immediately removed them and placed the variables into the methods: PROBLEM SOLVED.
What type of data are you trying to share across your servlets? I'm sure we can provide more help if we knew what you were trying to do.
SAF
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is also worth readingthis article on making Servlets Thread-safe at jGuru.
Dave
 
Kodo Tan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi SAFROLE
Thanks for the info. Just curious, how do you verify that your problem is resolved ? Do you simulate it with many multiple users accessing it ?
Basically what I would like to know is that if say we a have a object that gets updated each time there is a request e.g when user A calls servet A, the object gets updated and when B calls servlet A, the object gets updated based on what A has updated.
Would like to know how to enfore data integrity and concurrency here
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was able to verify that my problem was solved by recreating the error, then implementing the fix, then verifying that the problem was solved. Of course I had to use a multi threaded aproach to recreate the problem, so you will have to do the same.
If your servlet is updating another object each time it is accessed, then make sure the methods on that object are synchronized because multiple clients can access your servlet at the same time, therefore, you want to ensure that the object which is being updated is accessed in a serialized manner.
So basically you dont have to synchronize access to your servlet, you just need to synchronize access to your object that gets updated. Which brings up another issue. Make sure you have only one instance of the object that is being updated. If you have multiple instances, then there is obviously no need for synchronization. Only synchronize when you have multiple requests accessing and changing a single entity.
hope that helps..
SAF
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic