Forums Register Login

Servlet Thread Model

+Pie Number of slices to send: Send
All,
I have a question that folks here at JavaRanch may be familiar with.

Could someone please explain servlet instantiations (probably in Tomcat) using a Single Threaded Model versus Multi-Threaded Model?
My confusion basically refers to this:
If I have servletA requested at the same time by two different requests, what happens? Does a new instance of the servlet get initiated for each request? If not, how is it synchronized?
Thank you.
cyberCipher
+Pie Number of slices to send: Send
"cyberCipher"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
here.
Thanks! and welcome to the JavaRanch!
Mark
+Pie Number of slices to send: Send
servletA will not be instantiated for each request. Basically each request is treated as a thread accessing the same instance of the servlet. And the Servlet is NOT synchronised. u have to IMPLEMENT Synchronization for the data u want to be threadsafe.
But the scenario is different when servletA implements the SingleThreadModel interface(i.e. a new instance is created for each request)
thats it ....

..GR..
+Pie Number of slices to send: Send
Thanks for your reply.
since servletA will not be instantiated for each request, is it possible for two requests to get each other's information.
e.g. servletA accesses a bank account balance through a stateless ejb. Is it possible for userA to get userB's account info and vice versa, if they access the servlet at the same time? If this is the case, is the doPost method not synchronized?
Thank you,
Maneesh
+Pie Number of slices to send: Send
You have to have a major shift in your programming viewpoint and habits to use servlets.
1. HTTP is inherently state-free - it is up to your program to track the state of each user and remember it between requests. Typically this is accomplished with the HttpSession class. The servlet engine provides methods to associate a "session" with a particular user.
2. You can't use instance variables in the servlet for ANYTHING that is unique to a given user - you must use "local" variables. Any variable state that must be saved between requests can go in the session.
You should find a good servlet tutorial to get a clear picture of whats going on in a servlet/JSP web server.
Bill
+Pie Number of slices to send: Send
I agree with you that the variables that are used must be local. The bankaccount is a local variable in my example described above. I assumed that everyone would understand that but I was slightly unclear.
Now, considering this is the scenario, how will the example given react in both cases?
e.g. servletA accesses a bank account balance (local variable) through a stateless ejb. Is it possible for userA to get userB's account info and vice versa, if they access the servlet at the same time? If this is the case, is the doPost method not synchronized?
Since the doPost() method is called with the request, response sent as parameters, is it impossible that the user's accounts will be interchanged?
Thank you,
Maneesh
+Pie Number of slices to send: Send
If I understand your problem correctly, if there is only one instance of the EJB being used by all requests it is up to the EJB to distinquish between the two users in a Thread-safe fashion.
It makes more sense to synchronize on the EJB instance than to synchronize the entire doPost metohd. The reason being that generating the text of the response after the (local variable) balance has been located may be time consuming but will not interfere with other requests.
Each request and response are objects unique to the Thread handling the request - they are created and managed by the servlet engine.
Bill
I was her plaything! And so was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2075 times.
Similar Threads
Multithreading in the new Beta
Servlet Container
servlet concepts
when to use SingleThreadModle
Servlet Thread Model
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:11:17.