This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prevention of Data Corruption in Servlets?

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know how can we prevent data corruptionn in servlets in real time scenarios. For example i have a servlet, and it may handle mulitple requests through multiple threads, so how can i prevent data corruption. May be through synchronization of doGet() method, but then there are limitaions with it, also Single thhread model do have limitations. So could any one tell me how to handle such scennarios? Any API is available?
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type of data corruption are you talking about??
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you actually *have* any data corruption?
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What data are you talking about?

If you have instance variables in the servlet, they will be shared across all requests, so you will want to synchronize your access to them. However, if you keep your state information in the session, then each client/session will have their own copy. You can still run into trouble if the same client makes two simultaneous requests (say from two separate browser windows), but this isn't as likely.

Chapter 3 (Servlet Basics) of the book Core Servlets and JavaServer Pages, which is available in PDF format here: http://pdf.coreservlets.com/Servlet-Basics.pdf goes into details about the pitfalls of using the SingleThreadModel interface and how and when to properly synchronize your code.

Best Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic