2

Hey, I have a question regarding multithreading. First off, how many instances of DispatcherServlet / DispatcherPorlet is there ? Is it always the only one ? Even when there are let say 10 requests per second ? What about the services that are singleton by default. If I have a validationService bean that is injected into handler to provide request validation, as a singleton (by default), can I rely on the fact that it is a singleton and that it won't be reinstantiated in some cases ?

lisak
  • 21,611
  • 40
  • 152
  • 243

2 Answers2

0

Depending on the load, servlet container creates number of servlet instances, developer does not have any control over that. But in most of the cases, the container maintains a single instance of each servlet (as servlets are supposed to be thread-safe anyway).

For as for Spring singleton beans, these are singletons per web application - the Spring application context is stored in servlet context (you can get access to it with WebApplicationContextUtils.getWebApplicationContext(ServletContext)).

As for reliability: yes, you can rely on the fact that in the scope on one Spring application context, there is only one instance of each singleton bean.

Neeme Praks
  • 8,956
  • 5
  • 47
  • 47
  • But here is the clash, if servlet container creates a number of instances in high load, they either have a reference to the singleton DispactherServlet (which is not likely) or an instance of DispatcherServlet is created for each Container servlet and that wouldn't be possible unless there were a handle per DispatcherServlet afaik (can't imagine otherwise), and if there is a handler per dispatcherServlet, there must be as many spring app contexts as handlers – lisak Nov 22 '10 at 11:50
  • Seems that you did not read my response carefully. I did not say that there is one Spring ApplicationContext PER SERVLET INSTANCE. I said that there is one context PER WEB APPLICATION, no matter how many servlet instances there are or how many different DispatcherServlets you have defined (you can define more than one). – Neeme Praks Nov 22 '10 at 13:26
  • Actually there is one Spring ApplicationContext PER SERVLET INSTANCE (within a web application of course). Container delegates request to a singleton Servlet. Sure, you can define more than one Dispatcher, but we are talking about instances of one dispatcher, not defining 3 dispatchers ...And there is always only one instance – lisak Nov 22 '10 at 15:12
  • 1
    This claim is contrary to everything that I know about how Spring WebApplicationContext operates - can you provide a source, based on what you claim such a thing? As I said earlier, WebApplicationContext is not related to specific servlet (or instance), it is related to ServletContext and there is only one servlet context per web application (WAR) - this is guaranteed by servlet container. – Neeme Praks Nov 22 '10 at 15:51
0

This is an interesting question.

As mentioned in this previous question, the container is only permitted to instantiate one servlet instance. In this case, you're guaranteed to have one spring context, and one singleton.

The question is what happens for previous versions of the Servlet spec, which I'm not sure specify this behaviour explicitly.

In practice, though, containers only ever instantiate one servlet instance - I've never seen one do otherwise. So it's safe to assume that you'll only get one app context.

Community
  • 1
  • 1
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Thanks, I knew that tomcat instantiates singleton servlet instance, but I've never used anything else except for Jetty, and I need the app to be portable. – lisak Nov 22 '10 at 12:25