pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Craig Ringer <ringerc@ringerc.id.au>
To: Mike Charnoky <noky@nextbus.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Hung JDBC connections
Date: Sun, 10 Jun 2012 11:39:05 +0800
Message-ID: <4FD416D9.8020808@ringerc.id.au> (raw)
In-Reply-To: <4FD20A3F.70602@nextbus.com>
References: <4FD20A3F.70602@nextbus.com>

On 06/08/2012 10:20 PM, Mike Charnoky wrote:
> Hi,
>
> Yesterday we experienced a problem whereby hundreds of JDBC client 
> connections remained in a hung state, despite the fact that the 
> PostgreSQL server was actually rebooted. 
 From the stack trace below, that would seem to be clients that were 
actively in a query and receiving data from the server at the time.
> The strange thing is that on the client side, according to "netstat" 
> the TCP connections between the JDBC clients and PostgreSQL were still 
> in the ESTABLISHED state (even though the server was rebooted).
Presumably your server wasn't rebooted cleanly, or at least PostgreSQL 
wasn't able to shut down cleanly, so no RST or FIN packets were sent for 
connections.

If a tcp client doesn't receive a FIN or RST from its peer, it won't 
know the peer is going away and will continue waiting indefinitely. The 
wait will only terminate if:

- The application using the tcp socket has set a timeout or alarm 
that'll interrupt the read(), allowing it to decide that the remote peer 
might've gone away and unilaterally close the connection; or

- The operating system has tcp keepalives configured, and concludes 
after several missed keepalives that the remote peer is dead. The 
operating system will close the socket and return control to the client 
with an error flag set, allowing the client to continue.


If you don't want the default behaviour of the TCP stack,  you need to 
set a tcp keepalive globally for the OS, get your program (in this case 
Java + PgJDBC) to set a keepalive flag on its sockets, or get the client 
program to set an alarm to interrupt blocked socket reads after a timeout.

PgJDBC didn't offer the latter option until 9.1, so you had to use 
keepalives. If you didn't use keepalives, connections would stay stuck 
until whatever supervising tool decided they weren't going to respond 
and terminated them. If nothing like a transaction manager was 
supervising ,they'd just stay open.

 > Is there a way this hang can be addressed by the JDBC driver? Again, 
this is very disconcerting that the clients did not detect the server 
failure. Does the connection use any sort of timeout mechanism? Thank 
you for your help.


TCP can't tell the difference between "peer dead" and "peer unreachable 
due to sustained connection issues or congestion", so it assumes the 
peer is alive and will respond ... eventually. The wisdom of this 
decision may be questionable, but at least keepalives provide a solution.

Of course, It's not very friendly for the JDBC driver to wait 
indefinitely without periodically checking and seeing if the connection 
was progressing, so I'm glad to see that's changed in  9.1.

--
Craig Ringer



view thread (9+ messages)  latest in thread

Message-ID: <4FD416D9.8020808@ringerc.id.au>
Permalink:  ../4FD416D9.8020808@ringerc.id.au/
Also on:    postgresql.org/message-id/4FD416D9.8020808@ringerc.id.au

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: ringerc@ringerc.id.au, noky@nextbus.com
  Subject: Re: Hung JDBC connections
  In-Reply-To: <4FD416D9.8020808@ringerc.id.au>
  References: <4FD20A3F.70602@nextbus.com> <4FD416D9.8020808@ringerc.id.au>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox