Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 66D8063262E for ; Sun, 20 Dec 2009 22:24:53 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 93057-07 for ; Mon, 21 Dec 2009 02:24:35 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mta1.zcs.ext.wpsrv.net (mta1.zcs.ext.wpsrv.net [78.47.55.244]) by mail.postgresql.org (Postfix) with ESMTP id 5964F6325A7 for ; Sun, 20 Dec 2009 22:24:42 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by mta1.zcs.ext.wpsrv.net (Postfix) with ESMTP id 250A0180AE; Mon, 21 Dec 2009 03:24:41 +0100 (CET) X-Virus-Scanned: amavisd-new at mta1.zcs.ext.wpsrv.net Received: from mta1.zcs.ext.wpsrv.net ([127.0.0.1]) by localhost (mta1.zcs.ext.wpsrv.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id eLD5V4aNYpFJ; Mon, 21 Dec 2009 03:24:36 +0100 (CET) Received: from store1.zcs.ext.wpsrv.net (store1.zcs.ext.wpsrv.net [78.47.55.243]) by mta1.zcs.ext.wpsrv.net (Postfix) with ESMTP id 01E2018046; Mon, 21 Dec 2009 03:24:35 +0100 (CET) Date: Mon, 21 Dec 2009 03:24:35 +0100 (CET) From: Andreas Brandl To: Craig Ringer Cc: pgsql-jdbc@postgresql.org Message-ID: <14912910.37.1261362275792.JavaMail.root@store1.zcs.ext.wpsrv.net> In-Reply-To: <26064609.35.1261362151726.JavaMail.root@store1.zcs.ext.wpsrv.net> Subject: Re: keeping Connection alive MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Mailer: Zimbra 6.0.2_GA_1912.DEBIAN4.0 (ZimbraWebClient - FF3.0 (Linux)/6.0.2_GA_1912.DEBIAN4.0) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=AWL=0.000, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/66 X-Sequence-Number: 20147 Hi Craig, folks, I'm coming back to this topic, because I now do have an implementation which is more robust in respect to connection failures and the like and which follows Craig's idea. > > As far as I know there is no native mechanism for checking the > state. > > Trying something and catching the SQLException if it fails. That > reflects the design of the JDBC interface and of transactional > databases > in general that you should try to do something and cope if it fails, > rather than ensuring it cannot fail. > > > So one approach may be to implement a DataSource which does check > (i.e. "SELECT 1") validity before returning the Connection (and > reconnect if its not valid, i.e. throwing a SQLException?). > > Nononono.... > > Have the DataSource _consumer_ deal with it. > > [...] > > ... where "invalidateConnection(Connection, Throwable)" tells the > provider/pool that the connection is broken. > > [...] > > In practice, you'll also generally test e.getSQLSTate() to see if the > exception might be a transient once tha doesn't imply the connection > is > broken, and re-try with the same connection if it is. There's no point > dropping and re-creating the connection if it's a deadlock, > serialization failure, or the like after all. > > Here's some code I use to check for SQLStates that may mean an > operation > is worth retrying: > > private static final List transientStates = > Arrays.asList( > "08", // Connection exceptions - refused, broken, etc > "53", // Insufficient resources - disk full, etc > "57P0", // Db server shutdown/restart > "40001",// Serialization failure > "40P01"// Deadlock detected > ); > In the course of implementing I discovered, that in JDBC 4 there are subtypes of SQLException designed for distinguishing transient and non-transient failures. These are SQLTransientException, SQLNonTransientException and SQLNonTransientConnectionException. Quite an old source is [1]. My understanding is that in general distinguishing by the type of SQLException is sufficient for this purpose. Though I wonder if the postgresql-jdbc does implement this? Another question arising with implementing a more robust connection handling is the following. Suppose there is a 'WorkUnit' containing the concrete jdbc-code which gets executed in a single transaction which may get rolled back and retried some more times. The execution of the 'WorkUnit' generates some Statement- and ResultSet-instances which have to be closed after execution. My question is if it is advisable to do this closing of resources on a background worker thread. This would have the advantage of returning faster after executing the relevant JDBC code because resources are closed on the separate background thread. I.e. in pseudo code: void execute(WorkUnit unit) { do { unit.execute(); } while (failed and retry-count is not reached}; backgroundThread.close(unit); // a background thread will close the units ressources } Thanks a lot, I really appreciate this (and the other pgsql-*) mailinglist(s)! Andreas [1] http://www.artima.com/lejava/articles/jdbc_four3.html