Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes Servlet synchronization question..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet synchronization question....." Watch "Servlet synchronization question....." New topic
Author

Servlet synchronization question.....

SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
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).]
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
How are you managing sessions? Sounds more like session confusion then Connection problems to me...
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
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

Joined: Jul 06, 2001
Posts: 257
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
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
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!


CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet synchronization question.....
 
Similar Threads
Concurrent access on servlet instance variables...
Just took the IBM J2EE (483) Exam....
Session problems with JSP on ServletExec, W2000
JDBC and use of SQL CONNECT BY and PRIOR for hierachy (son,father)
How to determine if a session has expired...