Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Tomcat Crashing

 
Ranch Hand
Posts: 56
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All;

I am facing tomcat crashes with the following errors:

SEVERE: Socket accept failed
java.net.SocketException: Too many open files (Accept failed)
Out of Memory



I have really some questions that can help me to solve these errors please:

When I login to the tomcat management and I go to the Server Status, I see the following which are related to the threads, is it normal? Or there is something I have to change?

1) Threads monitoring:

Max threads: 400 Current thread count: 50 Current thread busy: 2
Max processing time: 73776 ms Processing time: 1231.185 s Request count: 2819 Error count: 1198 Bytes received: 0.18 MB Bytes sent: 148.64 MB

Stage Time B Sent B Recv Client (Forwarded) Client (Actual) VHost Request
P ? ? ? ? ? ?
R ? ? ? ? ? ?
R ? ? ? ? ? ?
R ? ? ? ? ? ?
R ? ? ? ? ? ?
R ? ? ? ? ? ?
R ? ? ? ? ? ?
R ? ? ? ? ? ?
S 13 ms 0 KB 0 KB 10.30.250.26 10.30.250.26 10.22.28.38 GET /manager/status HTTP/1.1
R ? ? ? ? ? ?
R ? ? ? ? ? ?
R ? ? ? ? ? ?
R ? ? ? ? ? ?
R ? ? ? ? ? ?
P: Parse and prepare request S: Service F: Finishing R: Ready K: Keepalive

Why the stages of the threads are still appearing even if they are Ready? Should not be disappeared? If I restarted the tomcat, I do not see them, it means that it started clean, so why they are appearing even after long time of not using the application (maybe after 6 hours of not using).

2) Opened files and lsof command:

When I type the command lsof -p process_id, I see a lot of the following lines (IP: 10.22.22.20 is the database server) although I close every database session that I open it in the code:

java    9874 tomcat   61u  IPv6          100650058       0t0        TCP localhost.localdomain:38190->10.22.22.20:cichild-lm (ESTABLISHED)

By the way, I do not use IP version 6.
Of course when tomcat is restated, I do not see this line. But after start using it, they start appearing and they do not disappear even if we stopped working. I am worry that I missed something that is causing them to stayed opened which will cause the above mentioned errors which are causing the tomcat to crash.

I know that I have to close every database session that I created in the code, and I know that I have to close every HttpurlConnection and every socket and every stream in the code. What else should I do?

Your kindly help is highly appreciated.
Regards
Bilal
 
Saloon Keeper
Posts: 27263
193
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tomcat itself listens on the channels defined by its Connection elements defined in the TOMCAT_HOME/conf/server.xml file. The Connectors affilitate with a pool so that Tomcat can handle multiple connections at a time efficiency. A connector is pulled from this pool are to receive an incoming client request and to provide the reply channel for the Response. HTTP is not a continuous-connection protocol, so the connection lasts only from the time of the request coming in to the end of the response going out. At that time, the network connection to/from the client is closed by Tomcat until/if the client makes another request. More in a moment.

A JDBC Database Connection done using a Tomcat Connection Pool is somewhat similar except that it maintains a collection of open network connections to the database, and supplies them to the application on demand. The web application uses the database Connection, then MUST close it to return it to the Connection Pool for re-use. And note that in any event, a Connection MUST NOT be saved between HTTP (servlet) requests! It must always be closed before the Response is complete.

OK. Now for the limits and constraints. As is the case for all standard ICP/IP applications, Tomcat has a central connection point attached to its incoming ports. This is where the connection request is bound to a connection handler. The handler attempts to obtain a request processor Thread from its request pool. If it cannot obtain one, the request is held until a Thread becomes available. If the request queue itself fills up, then the connection point stops accepting requests and they simply bounce (the client will probably display a "server busy" or "unable to connect" message). So that should not be inherently fatal.

Similarly, when a JDBC Connection Pool runs out of Connections, the Pool first looks to see if it's allowed to make more Connections. If it can't, then the request by the application for a Connection will fail. The application should be coded to allow for that and display a "Database busy" page or something to the user.

Again, this should not crash Tomcat.

What CAN cause problems is when a mis-coded application doesn't manage resources effectively. If a servlet/JSP request takes to long to process, it interferes with Tomcat's ability to pass out processing threads. Likewise, if an application holds a JDBC Connection too long, it interferes with effective operation of the JDBC Connection Pool.

The #1 rule then, is that a Servlet or JSP should spend as little time as possible getting from the request to the end of the Response. HTTP does NOT like it when you expect a servlet to run a request for 30 minutes. Or even 5. In fact, 1 minute is pushing it. If you need a long-running process to execute from a web request there are tricks to that, but a servlet shouldn't attempt to run such processes directly.

Now let's say that the apps are all well-behaved. Well, you can still have issues if, say, 40,000 users all descend on the server at once because they're all making legitimate requests there. At that point, you may need to tweak the Connector and/or Connection Pool settings to handle the load. And possibly consider an elastic solution with multiple Tomcat instances to spread the load around.

Finally, there's one last limit to consider. The OS itself has only a finite number of connections available for ALL apps (Java and otherwise) on the system. Some systems, such as the Oracle DBMS server will adjust the default limit, since they know that they're going to want more.  However there is a fixed numerical limit.

Now, as far as your particular error, it's a little hard to tell. I think that the "Ready" connections are connections maintained by HTTP keep-alive to make it faster for repeat connections from clients, but I could be wrong. The most fearsome message is Out Of Memory. That means that you don't have you JVM memory parameters set high enough to allow for the work that needs to be done. You can set the environment variable CATALINA_OPTS to override these JVM options.

Beyond that, about all I can say is that if Tomcat dies, copy the stack trace and paste it (as TEXT, please, no screenshots!) into this message thread.

 
Bilal Ghayad
Ranch Hand
Posts: 56
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you a lot Tim for your kindly information and reply.

What about:

SEVERE: Socket accept failed
java.net.SocketException: Too many open files (Accept failed)



Do you have any advise on how to fix it?
Regards
Bilal
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start reading here on what that means, and how to fix it: https://www.baeldung.com/linux/error-too-many-open-files
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic