Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 7ABE9633662 for ; Sun, 20 Dec 2009 23:06:14 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 16592-03 for ; Mon, 21 Dec 2009 03:06:08 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id 41F456335F9 for ; Sun, 20 Dec 2009 23:06:06 -0400 (AST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id ADED41E0450; Mon, 21 Dec 2009 11:06:04 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id N7qPAXOM4CLc; Mon, 21 Dec 2009 11:06:02 +0800 (WST) Received: from [10.1.1.6] (124-169-133-89.dyn.iinet.net.au [124.169.133.89]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id 911B61E044E; Mon, 21 Dec 2009 11:06:02 +0800 (WST) Message-ID: <4B2EE611.7020605@postnewspapers.com.au> Date: Mon, 21 Dec 2009 11:05:53 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.1.5) Gecko/20091204 Thunderbird/3.0 MIME-Version: 1.0 To: Andreas Brandl CC: pgsql-jdbc@postgresql.org Subject: Re: keeping Connection alive References: <14912910.37.1261362275792.JavaMail.root@store1.zcs.ext.wpsrv.net> In-Reply-To: <14912910.37.1261362275792.JavaMail.root@store1.zcs.ext.wpsrv.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/67 X-Sequence-Number: 20148 On 21/12/2009 10:24 AM, Andreas Brandl wrote: > 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]. Hmm, I'd never noticed the subtypes of SQLException. > 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? It does not appear to - at least, those exception names don't appear anywhere in the sources. It only seems to throw PSQLException, which is a subtype of SQLException. The full heirarchy is here, by the way: http://java.sun.com/javase/5/docs/api/java/sql/SQLException.html Unfortunately, the exception descriptions don't seem to be as clear as they could be regarding what exactly "transient" means. SQLRecoverableException describes something the app can recover from by at minimum re-connecting and re-trying. That's clear enough. SQLTransactionRollbackException would be a good one to be able to use for deadlock-rollback. It's similarly clear. SQLTransientException is for when "a previoulsy failed operation might be able to succeed when the operation is retried without any intervention by application-level functionality." This isn't very clear. Does that mean "retrying *this* *statement* may work, you don't have to re-try the whole transaction" ? That's how I read it. If it refers to just the one JDBC operation as it seems to, it doesn't really apply to Pg. In that case, is there even a suitable class for the most important case in Pg - "if you re-try the whole transaction this will probably succeed, but re-trying this particular statement is pointless. You don't have to reconnect." ? Anyway: To implement the use of SQLTransientException etc might be interesting given that the Pg driver supports JDBC2, JDBC3 _and_ JDBC4 drivers in one source package. It'd need an exception factory that was part of the JDBC-version-specific code, which took an exception type param as well as the usual msg, sqlstate and cause. The factory for JDBC2 / JDBC3 would return a normal PSQLException, and the JDBC4 factory would override it to return a PSQLException subclass derived from one of the JDBC SQLException subtypes (for JDBC4). The JDBC4 factory wouldn't even get built when building the JDBC2/3 drivers, so the exceptions being missing from older JDKs wouldn't matter. > 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. Closing the statement and associated result set shouldn't take long. I'd be reluctant to add the additional complexity, myself. Also, while the PgJDBC driver _is_ thread safe, this still seems like it's asking for threading issues. I'm no JDBC expert, though. -- Craig Ringer