Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 17700632E76 for ; Sat, 12 Dec 2009 23:05:32 -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 10616-06 for ; Sun, 13 Dec 2009 03:05:11 +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 29A486329DF for ; Sat, 12 Dec 2009 23:05:20 -0400 (AST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 31ADC1E044A; Sun, 13 Dec 2009 11:05:16 +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 fMg6boM+uOUs; Sun, 13 Dec 2009 11:05:14 +0800 (WST) Received: from [10.1.1.6] (124-169-88-117.dyn.iinet.net.au [124.169.88.117]) (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 4EFEA1E0440; Sun, 13 Dec 2009 11:05:14 +0800 (WST) Message-ID: <4B2459E4.40009@postnewspapers.com.au> Date: Sun, 13 Dec 2009 11:05:08 +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 CC: pgsql-jdbc@postgresql.org Subject: Re: keeping Connection alive References: <21846658.35.1260668264808.JavaMail.root@store1.zcs.ext.wpsrv.net> <4B2456D3.8000406@postnewspapers.com.au> In-Reply-To: <4B2456D3.8000406@postnewspapers.com.au> 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.34 tagged_above=-10 required=5 tests=AWL=0.259, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/36 X-Sequence-Number: 20117 On 13/12/2009 10:52 AM, Craig Ringer wrote: > int retries = MAX_RETRIES; > do { > try { > try { > Connection conn = myprovider.getConnection(); > Statement stmt = conn.createStatement(); > // do work > break; > } finally { > try { > stmt.close(); > } catch (SQLException e) { > // log to complain about statement close failure > } > } > } catch (SQLException e) { > myProvider.invalidateConnection(conn, e); > retries--; > } > } while (retries > 0); Oh, also: if that seems ghastly and rather undesirable to repeat all over the place - good. You should probably wrap your database work up in an implementation of an interface that you can pass to a database work executor. Your interface has a single method that does the actual work with the connection, plus hooks called for success, failure, etc. You pass a (probably anonymous inner class) instance of the interface to a database worker that's responsible for the ugly error handling and connection management, and let it take care of giving it a connection and running it. The database worker can take care of the nasty bits, leaving the rest of your code free to just handle real failures. You don't have to bother to catch SQLExceptions thrown within your work code, since it should all be running in a single transaction so it'll get automatically rolled back when the exception is thrown. Instead, you can let the exception bubble up for the database worker to handle. It'll decide whether to retry, to give up and call your failure hook, etc. As a bonus, if you specify that the on success and on failure methods must be run on the EDT but require the database work code to be safe to run on any thread, you can trivially move your database work off the EDT and into a background worker thread. -- Craig Ringer