简体   繁体   English

Tomcat的行为就像没有在处理线程请求一样,导致响应时间变慢

[英]Tomcat behaves as if it's not threading requests, causes slow response time

I'm seeing strange behavior and I don't know how to gain any further insight into and am hoping someone can help. 我看到了奇怪的行为,而且我不知道如何获得进一步的了解,并希望有人可以提供帮助。

Background: I have a query that takes a long time to return results so instead of making the user wait for the data directly upon request I execute this query via a Timer object at regular intervals and store the results in a static variable. 背景:我有一个查询,查询返回结果要花费很长时间,因此我没有使用户根据请求直接等待数据,而是定期通过Timer对象执行此查询并将结果存储在静态变量中。 Therefore, when the user requests the data I always just pull from the static variable, therefore making the response virtually instant. 因此,当用户请求数据时,我总是总是从静态变量中拉出数据,因此实际上使响应迅速。 So far so good. 到现在为止还挺好。

Issue: The behavior I'm seeing, however, is that if I make a request for the data just as the background (Timer) request has begun to query the data, my user's request waits for the data to come back before responding -- forcing the user to wait. 问题:不过,我所看到的行为是,如果我在后台(Timer)请求开始查询数据的同时对数据进行请求,则我的用户请求将等待数据返回,然后进行响应-迫使用户等待。 It's as if tomcat is behaving synchronously with the threads (I know it's not -- it just looks that way). 好像tomcat与线程同步运行一样(我知道不是-只是那样)。

This is in a Production environment and, for the most part, everything works great but for users there are times when the site just hangs for them and they feel it's unreliable (well, in a sense it is). 这是在生产环境中进行的,大部分情况下一切正常,但是对于用户而言,有时网站只是为他们而挂起,他们觉得它不可靠(从某种意义上来说是这样)。

What I've done: Being that the requests for the data were in a static method I thought "A ha! The threads are syncronized which is causing the delay!" 我所做的:由于对数据的请求是在静态方法中,所以我认为“啊!线程被同步化,这导致了延迟!” so i pulled all of my static methods out, removed the syncronization and forced each call to instantiate it's own object to retrieve the data (to keep it thread safe). 因此我退出了所有静态方法,删除了同步,并强制每个调用实例化其自己的对象以检索数据(以确保其线程安全)。 There isn't any syncronization on a semaphore to the static variable either. 信号量与静态变量之间也没有任何同步。

I've also installed javamelody to try and gain some insight into what's going on but nothing new thus far. 我还安装了javamelody,以尝试了解正在发生的事情,但到目前为止没有新内容。 I have noticed a lot (majority) of threads are in "WAITING" state but they also have 0ms for User and CPU time so don't think that is pointing to anything(?). 我注意到很多(大多数)线程处于“等待”状态,但是它们还有0ms的用户和CPU时间,所以不要认为这指向任何东西(?)。

Running Tomcat 5.5 (no apache layer), struts 2, Java 1.5 运行Tomcat 5.5(无Apache层),struts 2,Java 1.5

If anyone has any idea why a simple request to a static variable hangs for longer background processes I would really appreciate it! 如果有人知道为什么对静态变量的简单请求会挂起较长的后台进程,我将不胜感激! Or if you know how I can gain insight that would be great too. 或者,如果您知道我如何获得洞察力,那也将很棒。

Thanks! 谢谢!

One possible explanation is that the threads are actually blocking at the database level due to database locking (or something) caused by the long-running query. 一种可能的解释是,由于长时间运行的查询引起的数据库锁定(或其他原因),线程实际上在数据库级别处于阻塞状态。

The way to figure out what is going on is to find out exactly where the blocked threads are blocking. 弄清楚正在发生什么的方法是确切地找出被阻塞的线程在哪里阻塞。 A thread dump can be produced by sending a SIGQUIT (or equivalent) to the JVM, and included stack traces for all Java thread stacks. 可以通过将SIGQUIT(或等效方法)发送到JVM以及所有Java线程堆栈的堆栈跟踪来生成线程转储。 Alternatively, you can get the same information (and more) by attaching a debugger, etcetera. 另外,您可以通过附加调试器等获得相同的信息(甚至更多)。 Either way, the class name and line number of the top frame of each stack should allow you to look at the source code and figure out (at least) what kind of locking or blocking is going on. 无论哪种方式,每个堆栈顶部框架的类名和行号都应允许您查看源代码并找出(至少)发生了哪种锁定或阻塞。

For those who would like to know I eventually found VisualVM ( http://visualvm.java.net/download.html ). 对于那些想知道的人,我最终找到了VisualVM( http://visualvm.java.net/download.html )。 It's perfect. 这是完美的。 I run Tomcat from eclipse like I normally do and it appears within the VisualVM client. 我像往常一样从eclipse运行Tomcat,它出现在VisualVM客户端中。 Right-mouse click the tomcat icon, choose Thread Dump and, boom, I've got it all. 右键单击tomcat图标,选择“ Thread Dump”,繁荣,我已经掌握了一切。

Thanks, all, for the help and pointers towards the right direction! 谢谢大家的帮助和正确方向的指点!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM