Hi,
Whilst trying to insert a streaming Blob into MySQL (I'm an optimist), I've got code similar to:
InputStream is = conn.getInputStream(); // From a URL Connection
setMyBlob(Hibernate.createBlob(is));
Trouble is, only a buffer full actually arrives in the database. The JDBC 4.0 spec is a little hazy on actual virgin Blob implementation, but I've tracked down why.
Hibernate does this:
>> src/org/hibernate/Hibernate.java:379
Code:
public static Blob createBlob(InputStream stream) throws IOException {
return new SerializableBlob( new BlobImpl( stream, stream.available() ) );
}
Shouldn't use stream.available(), as it's not the whole story.
Then MySQL does this:
src/com/mysql/jdbc/PreparedStatement.java:2800
Code:
public void setBlob(int i, java.sql.Blob x) throws SQLException {
...
bytesOut.write('\'');
escapeblockFast(x.getBytes(1, (int) x.length()), bytesOut, (int) x
.length());
I'm generally leaning towards this not being a MySQL fault, because Hibernate should not be reporting the length of the data if it hasn't been explicitly told it, or properly determined it. I'd like to start a discussion, before filing a bug, on the best approach.
I suggest if the source is a stream then Hibernate either has to fully materialize it to determine length, or throw an unsupportedSQL exception on that method. If this is done, then MySQL should trap the exception and stream the input itself.
Anyone?