Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Spring MVC app be multithreaded even if its servlets are not?

When you talk about a Spring app being multithreaded, are you necessarily referring to whether the servlets that are defined in that app are multithreaded?

Or can a Spring app be configured to be multithreaded even if the servlets in the app are not multithreaded?

like image 674
Oleksandr Avatar asked May 12 '11 15:05

Oleksandr


2 Answers

Single-threaded servlets are no longer supported. They have been deprecated for a long time, so all servlets are multithreaded.

Then, spring does not use servlets (apart from one - the dispatcher). It uses beans, which can be controllers, services and repositories (daos).

These beans are thread-safe (what I suppose you mean by "multithreaded") if they don't hold any data in their fields (apart from their dependencies)

In short - don't store any data in your spring beans. Pass all required data as parameters.

like image 124
Bozho Avatar answered Nov 09 '22 05:11

Bozho


typical java web applications are multi-threaded in that every request is handled on its own thread. In such applications, you have to be careful when you have objects that maintain state (via modifying a static property, for example), as they can overwrite each other.

When you are talking about servlets, if two requests come in at the same time to the same servlet, the relevant servlet code is being executed twice concurrently. In frameworks like Struts or Spring, which delegate requests to objects, either the same bean instance can be reused, or a new bean instance could be created for each request, depending on how you have your framework configured (i.e. to use prototypes or singletons in the case of Spring)

like image 26
hvgotcodes Avatar answered Nov 09 '22 05:11

hvgotcodes