Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How tomcat handle multiple concurrent request at the same time?

How tomcat handle multiple concurrent request at the same time ?

Does it queues up the requests or processes some of the requests in parallel ?

If it processes requests in parallel , how does it returns the asynchronous response ? Does it keeps the connection open with client until response comes ?


If the tomcat uses a multi threaded environment ? Does the code needs to completely thread safe ? Or tomcat handles it on its own.

like image 839
sourabh1024 Avatar asked Aug 11 '17 05:08

sourabh1024


1 Answers

Tomcat uses thread connection pool and each incoming request will be assigned to a thread from the pool and once thread finishes the job, it return to the pool.

You can configure the tomcat connection pool according to your application.

Apart from connection pool, tomcat internally uses the JDBC connection pool as well, read more here https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html.

Edit :- to answer your second question, yes tomcat uses multi-threaded env. and each request is tomcat is assigned to a new thread for example each servlet request will be wrapped to a new request object(thread-safe) but after that it depends on your business logic, whether you are passing it to some thread safe code or not.

Will add code sample later on but http://jcip.net/ is a very good starting point if you want more code samples and deep understanding of it.

like image 199
Amit Avatar answered Nov 02 '22 23:11

Amit