Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 5E3E45D026C for ; Fri, 29 Jun 2012 00:09:33 -0300 (ADT) Received: from mail-gg0-f174.google.com ([209.85.161.174]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SkRaL-0005do-Oi for pgsql-jdbc@postgresql.org; Fri, 29 Jun 2012 03:09:32 +0000 Received: by gglu4 with SMTP id u4so2391679ggl.19 for ; Thu, 28 Jun 2012 20:09:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:x-gm-message-state; bh=ArR4fr89+caGWToHONIboGGncEBteq6u9NIIygjXrjw=; b=MLnHYHEobqLFqOFhfEuodBe8e/d7XJc0UNqpJ4KlgI3r22bQnU9VyPoPP3qA7wfLAV pdGADIbp90hGwWvgg3O846cG6dj94G1m7fLvqyI9Pr2TU1e8PA0K4I5MSbMDU79iwhuF 5nOlHRxPMWH0iHwQ0UpdHlS+4BCFtBLUnU9M0O5TF88mTcd4LqutdFkXnIXv3OhSI5/D 9lEVZ31OWzFpkFtAI8bLPy/ySweZgcRO8IcI5Q7ruHCvrb8Ts4u8AhVvtlbCaUE2fs2M 0WB7hmkSjM+oNapfhk1yGyNiC8tQX7QWBkdS7Z+Z3WY61qVSyMlLkwGyQky6RYvmxl91 Z3nw== Received: by 10.50.222.134 with SMTP id qm6mr309409igc.49.1340939355611; Thu, 28 Jun 2012 20:09:15 -0700 (PDT) Received: from ayaki.localdomain (124-169-169-118.dyn.iinet.net.au. [124.169.169.118]) by mx.google.com with ESMTPS id wp10sm1865376igb.6.2012.06.28.20.09.12 (version=SSLv3 cipher=OTHER); Thu, 28 Jun 2012 20:09:14 -0700 (PDT) Message-ID: <4FED1C55.4050507@ringerc.id.au> Date: Fri, 29 Jun 2012 11:09:09 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120605 Thunderbird/13.0 MIME-Version: 1.0 To: PostgreSQL JDBC Subject: Re: org.postgresql.util.PSQLException: An I/O error occured while sending to the backend References: <1340835352.64379.YahooMailNeo@web161405.mail.bf1.yahoo.com> In-Reply-To: <1340835352.64379.YahooMailNeo@web161405.mail.bf1.yahoo.com> Content-Type: multipart/alternative; boundary="------------020500010303050406070304" X-Gm-Message-State: ALoCoQl6iKKIn3fKx6eTErE1eBTZzw9ExZ53zb52LSYH6KEWyxTzBDyhWcuvRjgb5fMxW9ItgbNw X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201206/102 X-Sequence-Number: 23096 This is a multi-part message in MIME format. --------------020500010303050406070304 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit (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 --------------020500010303050406070304 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
(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

--------------020500010303050406070304--