Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id E715D13C7C39 for ; Sun, 10 Jun 2012 00:39:28 -0300 (ADT) Received: from mail-pb0-f46.google.com ([209.85.160.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SdYzr-0003LH-V3 for pgsql-jdbc@postgresql.org; Sun, 10 Jun 2012 03:39:28 +0000 Received: by pbbrp8 with SMTP id rp8so4144890pbb.19 for ; Sat, 09 Jun 2012 20:39:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=FBh31sKP8tE+erO5AEZ7u9b0M+/8BkOCml8DkZBn5Ls=; b=n6spG2yjvhZn7FTHwijNYJH1VJcasLIG8N83Y9hheAoPTQdJyk8yjbZTVAp3bN3icw KMqrrXJrYIiT52/P4u6MRqrumdweHwLlf7Z7IHdRByItk0zmcmpkwk5no8aNyi5ffcfg Wa9hNUJMsToBCkAJ7gCco+wECGvYCoPaBQ1hgwz5grzChNX1vRtByCbsvn30wm3j9xFP sIj81D8FsS7pgeilW0F5LiAj+fR3YXNi6oUcs/XQUfR3sSN2RKIn+fCRwA9Y9HTJh1ZQ DsyfK3j2gjAVkyA1+UXUHj/KoLG5+bU2ptjobAptV1tnldGidwRWLAzHkGsr+1xILW7o MlyA== Received: by 10.68.192.97 with SMTP id hf1mr9919635pbc.132.1339299551031; Sat, 09 Jun 2012 20:39:11 -0700 (PDT) Received: from ayaki.localdomain (203-59-174-91.dyn.iinet.net.au. [203.59.174.91]) by mx.google.com with ESMTPS id kt1sm13461459pbc.32.2012.06.09.20.39.08 (version=SSLv3 cipher=OTHER); Sat, 09 Jun 2012 20:39:10 -0700 (PDT) Message-ID: <4FD416D9.8020808@ringerc.id.au> Date: Sun, 10 Jun 2012 11:39:05 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1 MIME-Version: 1.0 To: Mike Charnoky CC: pgsql-jdbc@postgresql.org Subject: Re: Hung JDBC connections References: <4FD20A3F.70602@nextbus.com> In-Reply-To: <4FD20A3F.70602@nextbus.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQmaWzQqXfSEOvfyhj4Qyter50ZqRNfca0XzV95jJkMdL8xbbOZP01amrZ5iGvI7f8OmwULs X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201206/40 X-Sequence-Number: 23034 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