pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Craig Ringer <ringerc@ringerc.id.au>
To: PostgreSQL JDBC <pgsql-jdbc@postgresql.org>
Subject: Re: org.postgresql.util.PSQLException: An I/O error occured while sending to the backend
Date: Fri, 29 Jun 2012 11:09:09 +0800
Message-ID: <4FED1C55.4050507@ringerc.id.au> (raw)
In-Reply-To: <1340835352.64379.YahooMailNeo@web161405.mail.bf1.yahoo.com>
References: <1340835352.64379.YahooMailNeo@web161405.mail.bf1.yahoo.com>

(I accidentally sent this direct a couple of days ago; re-posting on-list)

On 06/28/2012 06:15 AM, Kiran Kulkarni wrote:
> I am getting this error couple of time in a day. How do I avoid it?
>
> Before making the call, I am checking these below things.
> session.isDirty: false
> session.isOpen: true
> session.isConnected: true
> session.getStatistics: SessionStatistics[entity count=0collection 
> count=0]
>
> connManager.isCurrentlyConnected(): true
> connManager.hasBorrowedConnection(): false
>
> But even after this check i am getting this error. Pls help.

Your connections are timing out. Without knowing how you connect to your 
database (host, via what conection, routers and nat in the way, etc) 
it's had to say why. You also haven't shown the PostgreSQL logs, which 
may contain additional information.

Setting a tcp keepalive may help.

More importantly though, your app must be robust in the face of errors 
and failed connections. You can test that a connection is OK, then go to 
us it and find that it broke in the tiny interval since you tested it - 
maybe the DB server got restated, maybe the network had a hiccup, 
whatever. You need to be able to open a new connection and retry your 
work when the connection fails, same way you need to be able to reissue 
a unit of work when there's a serialization failure in a serializable 
transaction.

Most JDBC work should look roughly like:

boolean work_done = false;
do {
   try {
      .. open a transaction ...
      .. do the work ...
      .. commit ...
   } catch ( ... various jdbc and ORM exceptions ... ) {
     .. Terminate the transaction
     try {
       ... issue a dummy query to see if the connection is usable
     } catch ( ... exception types ... ) {
       .. connection unusable, get a new connection
     }
   }
} while (!work_done);


The same logic applies if you're using an ORM like Hibernate, but you 
*also* have to cope with the fact that Hibernate and other JPA 
implementations leave your object graphs in an invalid state if a 
database operation fails. To cope with this, use Hibernate's 
SerializatiionHelper ( 
http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/util/SerializationHelper.html) 
to clone your object graph before merging/persisting. If there's an 
error in the database operation, discard the objects used in the 
operation, clone a new set from your backup copy, and try to commit them 
instead.

You can avoid cloning backups if it's easier for you to discard the 
object graph, fetch another from the database (usually actually from 
Hibernate's L1 cache), modify them again, and re-merge them.

Either way, your database access logic then looks like this:

.. (maybe) clone object graph ...
boolean work_done = false;
do {
   try {
      .. open a transaction ...
      .. do the work ...
      .. commit ...
   } catch ( ... various jdbc and ORM exceptions ... ) {
     .. Terminate the transaction
     try {
       ... issue a dummy query to see if the connection is usable
     } catch ( ... exception types ... ) {
       .. connection unusable, get a new connection
     }
     .. replace the object being merged/committed with a fresh one
     .. either by repeating the work or by restoring a cloned copy.
   }
} while (!work_done);


Annoying? Welcome to robustly using databases from application code. The 
alternative is accepting that your users will see database access errors 
you could prevent and possibly have to re-do work when a transient 
database issue occurs. I don't think that's good enough.

You shouldn't be repeating this stuff everywhere, though; when I was 
using non-container-managed transactions I had helper class that does 
most of the work. I just extended it with anonymous inner classes that 
overrode prepareGraphForPersist() and persistGraph() methods in my 
helper. I might still be able to find it somewhere; let me know if it'd 
be useful.

--
Craig Ringer

view thread (19+ messages)  latest in thread

Message-ID: <4FED1C55.4050507@ringerc.id.au>
Permalink:  ../4FED1C55.4050507@ringerc.id.au/
Also on:    postgresql.org/message-id/4FED1C55.4050507@ringerc.id.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: ringerc@ringerc.id.au
  Subject: Re: org.postgresql.util.PSQLException: An I/O error occured while sending to the backend
  In-Reply-To: <4FED1C55.4050507@ringerc.id.au>
  References: <1340835352.64379.YahooMailNeo@web161405.mail.bf1.yahoo.com> <4FED1C55.4050507@ringerc.id.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