views:

216

answers:

2

I am getting this weird case when querying Postgres 8.4 for some records with Blobs (of type OIDs) with Hibernate. The query does return all right but when my code wants to read the content of the BLOB with the simple code below, it gets 0 bytes back

public static byte[] readBlob(Blob blob) throws Exception {
    InputStream is = null;
    try {
        is = blob.getBinaryStream();
        return org.apache.commons.io.IOUtils.toByteArray(is);
    }
    finally {
        if (is != null)
            try {
                is.close();
            }
            catch(Exception e) {}
    }
}

Funny think is that I am getting this behavior only since I've started adding more then one such records to the table. The underlying JDBC library is type 3 (postgresq 8.4-701). Can someone give me a hint as to how to solve this issue? Thanks

Peter

A: 

It was a while since I have run into similar issue, and since I've refreshed my memories about this topic I am thinking of sharing the results. The problem is that Postgres (and a few versions back Oracle too) will not handle the Blob content at the record creation time in the same transaction. Funny think is that one needs to pass the content after the external file (where the content gets stored eventually) has been well created and reserved for the operation. Yes, the record gets created but the Blob is blank. To have the Blob filled out with whatever you need to put in, you need that operation in a second transaction (sort of an update record). That's a funny business (maybe a major bug), ehe

peter
PostgreSQL definitely supports handling of blobs inside the main transaction. That's kind of the whole reason to use blobs in the database. I don't know about Hibernate though...
Magnus Hagander
I have debugged to the JDBC client level what calls are made. It seems to me that in some cases when the content to be stored is limited the transaction with the Blob goes through in on shot no problem. But pass a certain threshold the transaction won't go. It may have to do with a Postgres configuration setting that I am not yet aware of. But thanks for your comments though
peter
A: 

Looks like you may have found this bug: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4876

Justin