• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Servlet synchronization question.....

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have a servlet that does NOT implement SingleThreadModel, and lets say that in the doPost() method, the servlet obtains a new Connection from DriverManager, starts a transaction, executes some SQL, and then commits the transaction, can I run into problems when concurrent access takes place on this servlet?
The Connection object is not an instance member of the servlet, this should be obvious since I said that the Connection object is created in the doPost() method, so it's essentially an automatic variable, created and destroyed in doPost().
Users of our application are reporting very strange errors. Very quickly, the application allows managers to enter performance evaluation data for their employees. Managers using the application are reporting that when they are entering data for an employee, they save and continue to the next page and a different employees data is being displayed! Hence, data overlapping.
what do you guys think?
thank you.
SAF
[This message has been edited by SAFROLE YUTANI (edited November 09, 2001).]
[This message has been edited by SAFROLE YUTANI (edited November 09, 2001).]
[This message has been edited by SAFROLE YUTANI (edited November 09, 2001).]
 
author
Posts: 3892
5
Redhat Quarkus Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you managing sessions? Sounds more like session confusion then Connection problems to me...
Kyle
 
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
In the doPost() method, the first call made is...
HttpSession session = req.getSession(true);
But do you think concurrent access to doPost() will cause jdbc problems? I dont see why since the Connection object is an automatic variable, not an instance variable of the servlet itself.
thanks,
SAF
 
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
Actually, why didn't I think of this before. It's not possible for 2 separate threads to access variables of a method since each thread gets its own copy of the methods variables. The problem I am having is being caused by something else.
SAF
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm interested in understanding this problem. I have been doing servlets for a while now, but don't know a lot of the behind-the-scenes stuff. It just works! Anyway, I've always been worried about something like this happening - multiple users hitting a servlet and seeing data that isn't their own. Can anyone explain why something like this would happen, and what the best strategies are to combat it? As the applications I'm working on get larger and larger, it seems like there's more potential for a problem like Safrole's happening. My servlets work with files, database connections, common data store objects, etc. I'm hoping I haven't built a house of cards!
Thanks for any revelations or wisdom!
 
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
Well Gerry, understanding what is happening behind the scenes is very important when dealing with production-level applications using components such as servlets that might be servicing multiple client requests. I was able to solve my problem, and thank God because it's 6PM Friday and I'm new at this job (hasn't even been a week) and my boss was breathing over my shoulder the entire time!
I was able to solve the problem I was having by going back to servlet basics. When a servlet is not specified as being "thread safe", in other words, the servlet does not implement SingleThreadModel, there is exactly 1 instance of the servlet running on your servlet engine. Therefore, this single instance is servicing requests for all client threads that are requesting the servlet. This is where you can run into problems. If your servlet does not contain any instance variables (variables that are defined in the class definition as non-static, then you dont have to worry. The reason for this is if two threads access the doPost() method, or any method, at the same time, they each get a copy of the variables in the method, so you dont have to worry about the threads over-writing each others data. As soon as you access an instance variable from any method of the servlet, you run the risk of concurrent access from multiple threads to that same variable. Remember, the servlet engine allocates only 1 instance of a servlet, therefore there is only one set of instance variables for that servlet in memory, and access to them must be synchronized, otherwise, you get strange results in your application.
One way to synchronize access to a servlet is to define the servlet to implement SingleThreadModel. There are no methods in this interface. By implementing this interface, you are informing the servlet engine to create a pool of servlet instances. When a request for the servlet is made, the servlet engine will check the pool for a free servlet instance and assign it to the client. The Servlet API indicates that the servlet engine will gaurantee synchronized access to the servlets service() method if it implements SingleThreadModel. Therefore, for a servlet that implements this interface, you would want to be doing your work from the service() method of the servlet. The service() method is always called when a servlet is invoked just before either doPost() or doGet() is called.
Using SingleThreadModel can impact performance on your application. The best way to avoid this is to minimize the use of instance variables in your servlets. If you remove the possiblity of synchronization issues in your servlets, then you wont have to resort to using SingleThreadModel. The JRun manual does not recommend against using this interface, but they dont encourage it very much either!
I hope that clears things up.
SAF
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic