关于并发访问相同servlet的问题

原文地址:
http://grokbase.com/t/tomcat/users/084pyx1bd2/question-on-concurrent-request-to-the-same-servlet

[Tomcat-users] Question on concurrent request to the same servlet

Dikan Xing Dikan Xing
Apr 22, 2008 at 11:45 am
Hi,

Recently, I realize such a problem with my Tomcat 6.0:

When two (almost) concurrent requests to the SAME servlet occurs (for example, I type the same url in two individual browser window), I find that Tomcat seems not to work on the second request until the first is done.

This is not a problem if the servlet service method is quick-responded or slow but heavy in computation, (in the latter case, parallelism of handling requests saves little time, since CPU resource is limited and becomes a main bottleneck).

However, in my situtaion, my servlet should wait for some other resources to respond. Those resources are quick-responded, but transimssion of requests to & responses from those resources cost much time. Therefore, if the servlet could be run (service method be called) immediately instead of the previous is done, the total response time could be reduced.

My servlet usually responds in about 15 seconds. My user accepts this amount of time, but not 60 seconds. When 5 requests almost concurrently occur, currently, the last response will not be available until 15*5=75 seconds elapses. This is terrible….

Is there any option in Tomcat so that I can configurate it to call the same servlet service method (doGet, doPost) simultanously instead of one by one?

Best Regards,

Dikan Xing

PS. I recond and print the time each doGet starts and ends, which reveal to me that the second request deson’t start until the first completes.
reply

Search Discussions

Search for…
Search All Groups users
2 responses
Oldest
Nested
Christopher Schultz Christopher Schultz at Apr 22, 2008 at 11:29 pm ⇧
Dikan

Dikan Xing wrote:
When two (almost) concurrent requests to the SAME servlet occurs (for
example, I type the same url in two individual browser window), I
find that Tomcat seems not to work on the second request until the
first is done.
This indicates a resource under contention. Are you using a database
connection pool with only one connection in the pool? Or perhaps another
pooled resource with only a single resource to share? Also check for
synchronized blocks of code that may be interfering with each other.
However, in my situtaion, my servlet should wait for some other
resources to respond. Those resources are quick-responded, but
transimssion of requests to & responses from those resources cost
much time. Therefore, if the servlet could be run (service method be
called) immediately instead of the previous is done, the total
response time could be reduced.
Could you be a little more specific? What resources do you need to wait
for, and why it is that sometimes you don’t necessarily have to wait?
Tomcat does not impose any prerequisites before calling the service
methods of your servlet that should be waiting around for anything.
My servlet usually responds in about 15 seconds. My user accepts this
amount of time, but not 60 seconds. When 5 requests almost
concurrently occur, currently, the last response will not be
available until 15*5=75 seconds elapses. This is terrible….
I think you have some resource contention that needs to be fixed before
you resort to any asynchronous solutions.
Is there any option in Tomcat so that I can configurate it to call
the same servlet service method (doGet, doPost) simultanously instead
of one by one?
The only Servlet API-related thing here would be using the
SingleThreadModel interface for your servlet. Are you using that?

- -chris

To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
reply | permalink
Juha Laiho Juha Laiho at Apr 23, 2008 at 3:20 am ⇧
- Show quoted text -
Are the two browser windows truly individual, or could they be part of the same
browser process? It seems that browsers may constrain themselves in several ways
when accessing a single resource (server, URL).
However, in my situtaion, my servlet should wait for some other resources to respond. Those resources are quick-responded, but transimssion of requests to & responses from those resources cost much time. Therefore, if the servlet could be run (service method be called) immediately instead of the previous is done, the total response time could be reduced.
Then you should, as Christopher mentioned, check that the contention is not caused
by those other resources (either via some pooling/synchronization mechanism within
your app only letting one request at a time to touch the external resource, or
somehow the external resource itself being able to only service a single request
at a given time).
Is there any option in Tomcat so that I can configurate it to call the same servlet service method (doGet, doPost) simultanously instead of one by one?
No options needed – not even if you’re using SingleThreadModel in your servlet.

I just wrote a small test and ran it in Tomcat 6.0.14 (not current, but that’s
what I happened to have at hand);

I wrote two servlets, with request body essentially “Thread.sleep(5000)”, followed
by a printout identifying the servlet (class and instance). One of the servlets
was a regular servlet, another was a servlet implementing SingleThreadModel.

Then I wrote a test program; a Java command-line application to spawn five threads
for making requests to the regular servlet (MTServlet, as in multi-threaded), and
another five threads for making requests to the servlet implementing SingleThreadModel
(STServlet).

Below is output from a run of the test program against a server hosting these two
servlets:

Start: Tue Apr 22 21:56:28 EEST 2008
Hello from STServlet instance 3
Hello from MTServlet instance 1
Hello from MTServlet instance 1
Hello from STServlet instance 2
Hello from MTServlet instance 1
Hello from MTServlet instance 1
Hello from MTServlet instance 1
Hello from STServlet instance 5
Hello from STServlet instance 4
Hello from STServlet instance 6
Stop: Tue Apr 22 21:56:34 EEST 2008

So, all servlets finished within six seconds of starting the first one – even those
implementing the SingleThreadModel. It should also be noted that all MTServlet calls
are handled by a single MTServlet instance (in parallel), but with STServlet the
parallelism comes from Tomcat creating several instances of the servlet (as allowed
by the Servlet Specification for servlets implementing the SingleThreadModel).

I’ve seen something like you describe, and it was when someone had either declared
the doGet() method (or another method called within doGet()) as synchronized, or
had written a synchronized code block referring to some single object for all requests.
- Show quoted text -
reply | permalink

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值