pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Craig Ringer <craig@postnewspapers.com.au>
To: Andreas <ml@3.141592654.de>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: keeping Connection alive
Date: Sun, 13 Dec 2009 11:05:08 +0800
Message-ID: <4B2459E4.40009@postnewspapers.com.au> (raw)
In-Reply-To: <4B2456D3.8000406@postnewspapers.com.au>
References: <21846658.35.1260668264808.JavaMail.root@store1.zcs.ext.wpsrv.net>
	<4B2456D3.8000406@postnewspapers.com.au>

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



view thread (12+ messages)  latest in thread

Message-ID: <4B2459E4.40009@postnewspapers.com.au>
Permalink:  ../4B2459E4.40009@postnewspapers.com.au/
Also on:    postgresql.org/message-id/4B2459E4.40009@postnewspapers.com.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: craig@postnewspapers.com.au, ml@3.141592654.de
  Subject: Re: keeping Connection alive
  In-Reply-To: <4B2459E4.40009@postnewspapers.com.au>
  References: <21846658.35.1260668264808.JavaMail.root@store1.zcs.ext.wpsrv.net> <4B2456D3.8000406@postnewspapers.com.au> <4B2459E4.40009@postnewspapers.com.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