Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCP keepalive not working

The situation:

  • Postgres 9.1 on Debian Server
  • Scala(Java) application using the LISTEN/NOTIFY mechanism to get notified through JDBC

As there can be very long pauses (multipla days) between notifications I ran into the problem that the underlying TCP connection silently got terminated after some time and my application stopped to receive the notifications.

When googeling for a solution I found that there is a parameter tcpKeepAlive that you can set on the connection. So I set it to true and was happy. Until the next day I saw that again my connection was dead.

As I had been suspicious there was a wireshark capture running in parallel which now turns out to be very usefull. Just about exactly two hours after the last successfull communication on the connection of interest my application sends a keepalive packet to the database server. However the server responds with RST as it seems it has already closed the connection.

The net.ipv4.tcp_keepalive_time on the server is set to 7200 which is 2 hours.

Do I need to somehow enable keepalive on the server or increase the keepalive_time?

Is this the way to go about keeping my application connected?

TL;DR: My database connection gets terminated after long inactivity. Setting tcpKeepAlive didnt fix it as server responds with RST. What to do?

like image 417
mgttlinger Avatar asked Dec 15 '25 13:12

mgttlinger


1 Answers

As Craig suggested in the comments the problem was very likely related to some piece of network hardware in between the server and the application. The fix was to increase the frequency of the keepalive messages.

In my case the OS was Windows where you have to create a Registry key with the idle time in milliseconds after which the message should be sent. Info on that here

I have set it to 15 minutes which seems to have solved the issue.

UPDATE: It only seemed like it solved the issue. After about two days of program run time my connection was gone again. I switched to checking the validity my connection every time I use it. This does not seem like it is the solution but it is a solution nonetheless.

like image 78
mgttlinger Avatar answered Dec 17 '25 15:12

mgttlinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!