pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Craig Ringer <craig@postnewspapers.com.au>
To: Thomas Finneid <tfinneid@fcon.no>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Connection exceptions
Date: Sat, 20 Jun 2009 11:39:15 +0800
Message-ID: <1245469155.1624.6.camel@ayaki> (raw)
In-Reply-To: <54338.134.32.140.234.1245407835.squirrel@fcon.no>
References: <54338.134.32.140.234.1245407835.squirrel@fcon.no>

On Fri, 2009-06-19 at 12:37 +0200, Thomas Finneid wrote:

> Anybody got any best practices for how do I deal with connection problems
> in JDBC?

Most of your question really deals with the connection pooler you're
using, rather than JDBC its self. I'd start with the documentation for
DBCP.

That said, there are a few areas where you do need to handle connection
issues. The big one is that when you perform an operation, or a series
of operations wrapped into a transaction, they shouldn't change any
state in the wider program until they're complete, so you can retry them
if something goes wrong. You should be prepared to catch an SQLException
thrown at any point in the operation or series of operations. You
shouldn't care at what point in the process the exception was thrown.
When you catch it, you can test the SQLState to see if the error is
possibly transient and be prepared to retry the operation. (deadlocked
transaction? OK, retry. Connection broken? Obtain a new connection and
retry. etc) 

That way, you can just go ahead and use the connection you got from the
pooler. If it doesn't work for any reason, your code will deal with it.
After all, just because the connection was fine when the pooler tested
it doesn't mean it's still going to be fine by the time you start using
it; that's a race you'll win most of the time but are far from
guaranteed to win. Much better to make the code actually doing the work
handle failures cleanly rather than try to prevent them.

Think, for example, of a user with a laptop wandering around - they
might leave wifi range half way through an operation.

--
Craig Ringer




view thread (4+ messages)  latest in thread

Message-ID: <1245469155.1624.6.camel@ayaki>
Permalink:  ../1245469155.1624.6.camel@ayaki/
Also on:    postgresql.org/message-id/1245469155.1624.6.camel@ayaki

 · 

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, tfinneid@fcon.no
  Subject: Re: Connection exceptions
  In-Reply-To: <1245469155.1624.6.camel@ayaki>
  References: <54338.134.32.140.234.1245407835.squirrel@fcon.no> <1245469155.1624.6.camel@ayaki>

* 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