setBlob loop performance?

From: "David Wall" <dwall(at)Yozons(dot)com>
To: "pgsql-jdbc" <pgsql-jdbc(at)postgresql(dot)org>
Subject: setBlob loop performance?
Date: 2002-08-26 22:16:30
Message-ID: 000f01c24d4e$3ac3e620$3201a8c0@expertrade.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

In the 7.2.2 codeset, PreparedStatement.setBlob() shows a loop as it reads a
byte from the input stream (the blob) and writes it to the output stream
(PG's LO routines).

This seems highly inefficient since most large objects are, well, large...
So if I want to insert a 1MB image, this will loop a million times. Is
there a reason it's not read in chunks (even a 4096 sized array would reduce
such a loop down to 250 iterations)?

This is much worse than the 7.1 code which simply took my byte array and
wrote it all to the LargeObject stream in one call.

+++
public void setBlob(int i, Blob x) throws SQLException
{
InputStream l_inStream = x.getBinaryStream();
int l_length = (int) x.length();
LargeObjectManager lom = connection.getLargeObjectAPI();
int oid = lom.create();
LargeObject lob = lom.open(oid);
OutputStream los = lob.getOutputStream();
try
{
// could be buffered, but then the OutputStream
returned by LargeObject
// is buffered internally anyhow, so there would be
no performance
// boost gained, if anything it would be worse!
int c = l_inStream.read();
int p = 0;
while (c > -1 && p < l_length)
{
los.write(c);
c = l_inStream.read();
p++;
}
los.close();
}
catch (IOException se)
{
throw new PSQLException("postgresql.unusual", se);
}
// lob is closed by the stream so don't call lob.close()
setInt(i, oid);
}

+++

Since the getBinaryStream() returns an InputStream, should this routine
close that inputstream once it's done, or does the Blob itself have to
somehow know that a stream it creates can be closed and discarded (and if
so, how?)?

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Warren Massengill 2002-08-27 01:39:58 JDBC driver download
Previous Message David Wall 2002-08-26 21:43:32 Converting setBytes to setBlob