What are the advantages/disadvantages with regards to the fact that Tomcat only creates one instance of a Servlet class to handle all requests for a JSP/servlet?
views:
147answers:
1
+2
A:
This is not Tomcat-specific, it's just conform the Sun Java Servlet API specification. You can however go around this by letting the servlet implement the deprecated SingleThreadModel
interface.
Using a single instance applicationwide has the huge benfit that there's no means of overhead of creating a new instance on every request in busy environments. And the disadvantages? No one comes to mind. It makes perfectly sense.
BalusC
2010-02-18 01:10:25
What about an "extremely heavy" load on the server? Would it be better to have several instances if one is getting bogged down with too many requests?
Lirik
2010-02-18 03:18:51
That's no problem. Java is multithreaded. The same instance can be used simultaneously by multiple threads.
BalusC
2010-02-18 10:37:00
...as long as you write threadsafe code yourself ;) I.e. do not assign request/session scoped data as instance variables. Also see this answer: http://stackoverflow.com/questions/2183974/difference-each-instance-of-servlet-and-each-thread-of-servlet-in-servlets/2184147#2184147
BalusC
2010-02-18 22:29:44