Topic
4 replies Latest Post - ‏2014-04-02T14:22:25Z by KevinRahe
KevinRahe
KevinRahe
22 Posts
ACCEPTED ANSWER

Pinned topic BLOBs retrieved incorrectly using JNDI/pooled Connections in 64-bit Windows

‏2011-11-09T03:53:28Z |
I am experiencing problems retrieving BLOB values from DB2 Express-C 9.7 (DB2/NT64 9.7.2) under 64-bit Windows 7 when my database Connection is obtained from an application server using JNDI (in my case Glassfish 3.1.1).

The value I'm trying to retrieve is a JPEG image that is 36621 bytes long. When I query the length of the object in CLP using

select length(companylogo) from esp.contractors
it reports the length correctly. However, when I retrieve it in Java (using either Blob.getBytes() or Blob.getBinaryStream()), I get 36622 bytes, where the first byte is an extra 0 and the remaining bytes appear to be the expected JPEG contents. If I add an extra byte to the end of file that was the source of my BLOB value so that it's now actually 36622 bytes long, and then update the BLOB column with it, I again get 36622 bytes returned to my application, but the first byte is still an extra 0 and the extra byte I added is truncated. If I add one more byte to the file, then I get 36624 bytes returned to my application. So the pattern is that Blobs always return a value that is an even number of bytes in size, and always with an extra 0 prepended to the expected contents.

I am running under a 64-bit JDK (Oracle 1.6.0_27) using the latest DB2 JDBC drivers I could find (v9.7fp4_jdbc_sqlj, with driver files dated 3/31/2011). I have tried both the JDBC3 and JDBC4 drivers and get the same results with both. I strongly suspect based on the nature of the error and the seeming rarity of reports of the problem that this is a 64-bit issue. I will re-create my development environment on my 32-bit Windows XP laptop tomorrow and see if the problem doesn't manifest itself there, which will prove whether or not it's a 64-bit-only problem. If it works there, I'll try changing to a 32-bit JDK on my Windows 7 machine and see if that solves the problem there, too. If it is a 64-bit issue, I can't really pinpoint where the problem lies, yet. It could be in the JDBC driver, Glassfish or even the JDK itself.

The one peculiarity in all this is that I first noticed a strange problem running a BIRT report that retrieves the same BLOB values from the database that are being retrieved incorrectly by my application. The report fails with an OutOfMemory error when I try to execute it using a JDBC Connection obtained from Glassfish/JNDI, but when I let it open its own DB2 connection using the same drivers it works fine. That would seem to rule out a problem caused by the 64-bit JDK itself, since the report is still executing under that JDK when it succeeds. Yet why a JDBC connection opened by Glassfish/JNDI would behave so differently than one opened directly by a Java application is a mystery. I will post more after I test everything in a 32-bit environment.
Updated on 2011-11-21T13:59:16Z at 2011-11-21T13:59:16Z by KevinRahe
  • KevinRahe
    KevinRahe
    22 Posts
    ACCEPTED ANSWER

    Re: BLOBs retrieved incorrectly using JNDI/pooled Connections in 64-bit Windows

    ‏2011-11-09T05:33:25Z  in response to KevinRahe
    I have tested the application under 32-bit Windows XP and still have the same problem there. So it appears that instead of a 64-bit issue there is simply something peculiar about DB2 Connections obtained via Glassfish/JNDI that makes them incompatible with BLOBs. Why that would be I have no idea, but this is a major problem for me right now - I'm developing an application for a national account and they're expecting to see a demo Friday, and if this isn't working I'm going to be missing some significant functionality. The only workaround I can fathom right now is to use DriverManager rather than JNDI to obtain DB2 Connections in the JSF beans where I need to retrieve BLOB values, which are few and are used only rarely so it may work for the time being. Interestingly I retrieve values from CLOB columns elsewhere using JNDI-supplied Connections and I haven't noticed a problem with those, though I'm going to check them over more carefully now that I see the issue with BLOBs.
  • KevinRahe
    KevinRahe
    22 Posts
    ACCEPTED ANSWER

    Re: BLOBs retrieved incorrectly using JNDI/pooled Connections in 64-bit Windows

    ‏2011-11-09T15:01:30Z  in response to KevinRahe
    Scratch all that. The BLOB values are being returned correctly. I was looking at the actual InputStream that was being returned and noticed the mismatched length of its internal buffer, but when I invoke InputStream.available() I get the correct length and InputStream.read() returns the correct first byte rather than the 0 byte at the beginning of the array. (It appears I was wrong about the buffer always being an even length as well - must have fooled myself.)

    This still doesn't explain why my BIRT report doesn't work when executed using a Connection obtained from Glassfish/JNDI nor does it explain why PrimeFaces' graphicImage tag doesn't display my JPEG image properly once my backing bean obtains it from the database, but it does at least seem to rule out an issue with BLOB values obtained through a JDBC Connection.
  • KevinRahe
    KevinRahe
    22 Posts
    ACCEPTED ANSWER

    Re: BLOBs retrieved incorrectly using JNDI/pooled Connections in 64-bit Windows

    ‏2011-11-21T13:59:16Z  in response to KevinRahe
    Just in case someone happens across this topic who has the same problem I do, it appears that there is a problem with BLOB values in BIRT reports when they're retrieved using a JDBC Connection supplied by an application server using JNDI, which is specific to neither the database nor the application server being used (I'm using DB2 + Glassfish, others with the same issue are using MySQL + JBoss). A BIRT bug has been logged: https://bugs.eclipse.org/bugs/show_bug.cgi?id=323563
  • KevinRahe
    KevinRahe
    22 Posts
    ACCEPTED ANSWER

    Re: BLOBs retrieved incorrectly using JNDI/pooled Connections in 64-bit Windows

    ‏2014-04-02T14:22:25Z  in response to KevinRahe

    I've found additional references to this issue, but none of the suggested workarounds work in my case.  I will defer trying to address this issue again until I upgrade to DB2 10.x.

    https://issues.jboss.org/browse/SOA-3350

    https://bugzilla.redhat.com/show_bug.cgi?id=780889

    https://issues.jboss.org/browse/JBPAPP-2613