TCP Socket connection in CLOSE_WAIT status and not getting closed
I am facing an issue with the TCP socket connections not getting closed and they are in CLOSE_WAIT status for ever.
As a part of batch process in our application, emails are sent with 4 embedded images. These images are downloaded from 3rd party site with IP say "UUU.XXX.YYY.ZZZ"
The images are embedded to email as follows
1. An URL object is created with the site url.
URL urlPhoto = new
URL("http://UUU.XXX.YYY.ZZZ/email/photos.jpg");
2. The image cid is created with the URL object and the image name
HtmlEmail htmlEmail = new HtmlEmail();
String cid1 = htmlEmail.embed(urlPhoto,
"photo.jpg");
3. The image cid is added to the email template by replacing the ${cid1} and the email is sent.
<td valign="top">
<img src="cid:${cid1}" width="279" height="274">
</td>
When a mail is sent, 4 new TCP connections are opened and are put in CLOSE_WAIT status for ever. For every mail sent 4 new connections are opened. In UNIX there is an upper limit on the number of open file handles (defaults to 1024) at any point of time. The open TCP connection has the underlying socket in CLOSE_WAIT status and is not getting closed at all. When the upper limit (1024) is reached the batch process is throwing the following exception and terminates.
Caused by: com.inet.tds.ap: java.net.SocketExceptionjava.net.SocketException: Too many open files
at com.inet.tds.am.a(Unknown Source)
at com.inet.tds.TdsDriver.a(Unknown Source)
at com.inet.tds.TdsDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at com.hcomemea.batchprocess.dataaccess.database.BaseJdbcDao.openConnection(BaseJdbcDao.java:106)
... 12 more
When I run the command lsof in UNIX which list the open file handles in the system
$ /usr/sbin/lsof -p 22933 -i | grep CLOSE_WAIT
java 22933 build_master 297u IPv6 129841943 TCP integration.com:47929->UUU.XXX.YYY.ZZZ:http (CLOSE_WAIT)
java 22933 build_master 298u IPv6 129841947 TCP integration.com:47933->UUU.XXX.YYY.ZZZ:http (CLOSE_WAIT)
java 22933 build_master 299u IPv6 129841950 TCP integration.com:47936->UUU.XXX.YYY.ZZZ:http (CLOSE_WAIT)
java 22933 build_master 300u IPv6 129841970 TCP integration.com:47952->UUU.XXX.YYY.ZZZ:http (CLOSE_WAIT)
���list of 935 connections similarly�
I tried 2 solutions
1. Got the HttpURLConnection from the URL object and invoked disconnect method on the same. But it doesn�t work.
2. Ran the batch process java program with the parameter �Dhttp.keepAlive=false to close the underlying connection but didn�t help.
I need the underlying sockets to be closed and not put in CLOSE_WAIT status after sending the mail.
Is it the problem with the embed method of HtmlEmail object not closing the underlying socket connection.
If anyone has faced this issue before, kindly let me know the possible solutions for the same ASAP.
Thank you,
Ramesh G