1

We have a Struts2 web application running in Tomcat7 and MySQL5 database. After navigating on the site for a bit, we are getting the exception:

java.net.SocketException: Too many open files

If we restart Tomcat, it works again for some time, and then the problem respawn.

Can some one help me to get out of this issue ?

STACKTRACE:

java.net.SocketException: Too many open files
 at java.net.Socket.createImpl(Socket.java:387)
 at java.net.Socket.<init>(Socket.java:361)
 at java.net.Socket.<init>(Socket.java:208)
 at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
 at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
 at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
 at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
 at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
 at org.apache.tomcat.dbcp.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
 at org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
 at org.apache.tomcat.dbcp.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1185)
 at org.apache.tomcat.dbcp.dbcp.AbandonedObjectPool.borrowObject(AbandonedObjectPool.java:79)
 at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
 at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
Roman C
  • 49,761
  • 33
  • 66
  • 176

1 Answers1

1

Socket connections are treated like files, and they use File Descriptor (FD), which is a limited resource. When a program (not necessarily your program, maybe is just Tomcat serving too much connections) open and close too many connections in a short span of time, you will get:

java.net.SocketException: Too many open files

This is due to the fact that a connection, after is closed, can be in a TIME_WAIT state for a bit (60 seconds on Linux, 4 minutes on Windows), and hence keeping the FD locked. When you ran out of FDs, you get the exception, that basically means

Too many socket connections open in a short span of time

The limit, different on each Operating System, can be examined with a command, eg. on Unix / Linux:

ulimit -n

Then simply raise the limit, eg. on Unix / Linux edit the file

/etc/security/limits.conf

adding at the end of the file the following line:

USR hard nofile 13370 
USR soft nofile 13370 

replacing USR with the user name used by Tomcat, and 13370 with the new limit you want to set.

You might need to do other operations, follow this Q&A if the above change isn't enough.

Also remember to reboot.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243