pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to retieve binary data (bytea) without problem ?
5+ messages / 3 participants
[nested] [flat]

* How to retieve binary data (bytea) without problem ?
@ 2005-09-28 20:27 Alfredo Rico <alfredorico@gmail.com>
  2005-09-29 12:20 ` Re: How to retieve binary data (bytea) without problem ? Russell Francis <rfrancis@ev.net>
  0 siblings, 1 reply; 5+ messages in thread

From: Alfredo Rico @ 2005-09-28 20:27 UTC (permalink / raw)
  To: pgsql-jdbc; +Cc: Kris Jurka <books@ejurka.com>

Hi friends, greetings :-)

I'm using PostgreSQL v7.4.7 with JDBC driver version 8.0 Build 312 in a Java
Web Deveploment.

I have a table named 'attachedfiles' in which there is a column used to
store binary data (bytea type) (pdf's files, doc, png, jpg, what ever...).

I have stored, without troubles, binary data reaching 30 MB.

To retrieve the binary data I'm using the following Java code:

PreparedStatement ps = this.con.prepareStatement("SELECT contentfile from
attachedfiles where filename = ? ", ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
ps.setString(1,"UsingJDBC.pdf");
bytes filebinary[] = null;
ResultSet rs = ps.executeQuery();
if (rs != null)
{
while (rs.next()) //Only one row
{
filebinary = rs.getBytes(1); //Obtain the file...
}
rs.close();
}
ps.close();


Problem:
If the binary data that I want to retrieve, is up to 12 MB, I obtain a
java.lang.OutOfMemoryError: Java Heap Space.

I'm using NetBeans 4.1 and a I have increased the JVM heap size uing the
following comand at momento to start Netbeans IDE

$ ./netbeans -J-Xmx300m

but this don't work.

How could I overcome this error ?

Thanks in advance by your help and support.

Kind Regards.-
Alfredo Rico.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to retieve binary data (bytea) without problem ?
  2005-09-28 20:27 How to retieve binary data (bytea) without problem ? Alfredo Rico <alfredorico@gmail.com>
@ 2005-09-29 12:20 ` Russell Francis <rfrancis@ev.net>
  2005-09-29 13:52   ` Re: How to retieve binary data (bytea) without problem ? Alfredo Rico <alfredorico@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Russell Francis @ 2005-09-29 12:20 UTC (permalink / raw)
  To: Alfredo Rico <alfredorico@gmail.com>; +Cc: pgsql-jdbc

Alfredo Rico wrote:
> Hi friends, greetings :-)
> 
> I'm using PostgreSQL v7.4.7 with JDBC driver version 8.0 Build 312 in a Java
> Web Deveploment.
> 
> I have a table named 'attachedfiles' in which there is a column used to
> store binary data (bytea type) (pdf's files, doc, png, jpg, what ever...).
> 
> I have stored, without troubles, binary data reaching 30 MB.
> 
> To retrieve the binary data I'm using the following Java code:
> 
> PreparedStatement ps = this.con.prepareStatement("SELECT contentfile from
> attachedfiles where filename = ? ", ResultSet.TYPE_FORWARD_ONLY,
> ResultSet.CONCUR_READ_ONLY);
> ps.setString(1,"UsingJDBC.pdf");
> bytes filebinary[] = null;
> ResultSet rs = ps.executeQuery();
> if (rs != null)
> {
> while (rs.next()) //Only one row
> {
> filebinary = rs.getBytes(1); //Obtain the file...
> }
> rs.close();
> }
> ps.close();
> 
> 
> Problem:
> If the binary data that I want to retrieve, is up to 12 MB, I obtain a
> java.lang.OutOfMemoryError: Java Heap Space.

Hi Alfredo,

I am not an expert and am relatively new to this list but perhaps a 
different approach rather than using getBytes which loads the whole 12M 
into memory would work.  Have you tried something like this?

...
int bytes_read = 0;
byte[] buf = new byte[ 8192 ];
ServletOutputStream outs = servletRequest.getOutputStream();
InputStream ins = rs.getBinaryStream( 1 );

while( ( bytes_read = ins.read( buf ) ) != -1 )
{
	outs.write( buf, 0, bytes_read );
}

outs.close();
ins.close();
...

This may be less memory intensive than pulling the whole binary field 
into memory at once.

Cheers,
Russ

Attachments:

rfrancis.vcf (text/x-vcard, 260B) download begin:vcard fn:Russell Francis n:Francis;Russell org:Electronic Vision adr:;;5 Depot Street;Athens;OH;45701;USA email;internet:rfrancis@ev.net title:Software Engineer tel;work:(740)-592-2433 x 18 x-mozilla-html:FALSE url:http://www.ev.net version:2.1 end:vcard
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: How to retieve binary data (bytea) without problem ?
  2005-09-28 20:27 How to retieve binary data (bytea) without problem ? Alfredo Rico <alfredorico@gmail.com>
  2005-09-29 12:20 ` Re: How to retieve binary data (bytea) without problem ? Russell Francis <rfrancis@ev.net>
@ 2005-09-29 13:52   ` Alfredo Rico <alfredorico@gmail.com>
  2005-09-29 17:06     ` Re: How to retieve binary data (bytea) without problem ? Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Alfredo Rico @ 2005-09-29 13:52 UTC (permalink / raw)
  To: Russell Francis <rfrancis@ev.net>; +Cc: pgsql-jdbc

Hi, Russell.
Your suggest is very correct and in fact it works very well but only if the
file size is under 10 MB,
The problem happens just at moment to execute:
InputStream ins = rs.getBinaryStream( 1 );

I don't know how to overcome this :-(


On 9/29/05, Russell Francis <rfrancis@ev.net> wrote:
>
> Alfredo Rico wrote:
> > Hi friends, greetings :-)
> >
> > I'm using PostgreSQL v7.4.7 with JDBC driver version 8.0 Build 312 in a
> Java
> > Web Deveploment.
> >
> > I have a table named 'attachedfiles' in which there is a column used to
> > store binary data (bytea type) (pdf's files, doc, png, jpg, what
> ever...).
> >
> > I have stored, without troubles, binary data reaching 30 MB.
> >
> > To retrieve the binary data I'm using the following Java code:
> >
> > PreparedStatement ps = this.con.prepareStatement("SELECT contentfile
> from
> > attachedfiles where filename = ? ", ResultSet.TYPE_FORWARD_ONLY,
> > ResultSet.CONCUR_READ_ONLY);
> > ps.setString(1,"UsingJDBC.pdf");
> > bytes filebinary[] = null;
> > ResultSet rs = ps.executeQuery();
> > if (rs != null)
> > {
> > while (rs.next()) //Only one row
> > {
> > filebinary = rs.getBytes(1); //Obtain the file...
> > }
> > rs.close();
> > }
> > ps.close();
> >
> >
> > Problem:
> > If the binary data that I want to retrieve, is up to 12 MB, I obtain a
> > java.lang.OutOfMemoryError: Java Heap Space.
>
> Hi Alfredo,
>
> I am not an expert and am relatively new to this list but perhaps a
> different approach rather than using getBytes which loads the whole 12M
> into memory would work. Have you tried something like this?
>
> ...
> int bytes_read = 0;
> byte[] buf = new byte[ 8192 ];
> ServletOutputStream outs = servletRequest.getOutputStream();
> InputStream ins = rs.getBinaryStream( 1 );
>
> while( ( bytes_read = ins.read( buf ) ) != -1 )
> {
> outs.write( buf, 0, bytes_read );
> }
>
> outs.close();
> ins.close();
> ...
>
> This may be less memory intensive than pulling the whole binary field
> into memory at once.
>
> Cheers,
> Russ
>
>
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to retieve binary data (bytea) without problem ?
  2005-09-28 20:27 How to retieve binary data (bytea) without problem ? Alfredo Rico <alfredorico@gmail.com>
  2005-09-29 12:20 ` Re: How to retieve binary data (bytea) without problem ? Russell Francis <rfrancis@ev.net>
  2005-09-29 13:52   ` Re: How to retieve binary data (bytea) without problem ? Alfredo Rico <alfredorico@gmail.com>
@ 2005-09-29 17:06     ` Kris Jurka <books@ejurka.com>
  2005-09-29 18:37       ` Re: How to retieve binary data (bytea) without problem ? Alfredo Rico <alfredorico@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Kris Jurka @ 2005-09-29 17:06 UTC (permalink / raw)
  To: Alfredo Rico <alfredorico@gmail.com>; +Cc: Russell Francis <rfrancis@ev.net>; pgsql-jdbc



On Thu, 29 Sep 2005, Alfredo Rico wrote:

> Your suggest is very correct and in fact it works very well but only if the
> file size is under 10 MB,
> The problem happens just at moment to execute:
> InputStream ins = rs.getBinaryStream( 1 );
>

The JDBC driver can stream data going to the server (setBinaryStream), but 
cannot stream data returning from the server (getBinaryStream) because of 
protocol limitations.  You have two options:

Switch from using bytea to large objects (Blobs).  Large objects were 
designed to work with much larger data sizes and have a complete streaming 
interface.

Increase the JVM's heap size.  Since you are using netbeans and you 
haven't received many helpful suggestions here, you might try a netbeans 
specific list for suggestions on how to accomplish this.

Kris Jurka



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to retieve binary data (bytea) without problem ?
  2005-09-28 20:27 How to retieve binary data (bytea) without problem ? Alfredo Rico <alfredorico@gmail.com>
  2005-09-29 12:20 ` Re: How to retieve binary data (bytea) without problem ? Russell Francis <rfrancis@ev.net>
  2005-09-29 13:52   ` Re: How to retieve binary data (bytea) without problem ? Alfredo Rico <alfredorico@gmail.com>
  2005-09-29 17:06     ` Re: How to retieve binary data (bytea) without problem ? Kris Jurka <books@ejurka.com>
@ 2005-09-29 18:37       ` Alfredo Rico <alfredorico@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Alfredo Rico @ 2005-09-29 18:37 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: Russell Francis <rfrancis@ev.net>; pgsql-jdbc

Thank you Kris, I was absolutely confused about this situation.

I'll switch to Blob objects because increasing the JVM heap could harm
perfromance...

Kind Regards.
Alfredo Rico,

On 9/29/05, Kris Jurka <books@ejurka.com> wrote:
>
>
>
> On Thu, 29 Sep 2005, Alfredo Rico wrote:
>
> > Your suggest is very correct and in fact it works very well but only if
> the
> > file size is under 10 MB,
> > The problem happens just at moment to execute:
> > InputStream ins = rs.getBinaryStream( 1 );
> >
>
> The JDBC driver can stream data going to the server (setBinaryStream), but
> cannot stream data returning from the server (getBinaryStream) because of
> protocol limitations. You have two options:
>
> Switch from using bytea to large objects (Blobs). Large objects were
> designed to work with much larger data sizes and have a complete streaming
> interface.
>
> Increase the JVM's heap size. Since you are using netbeans and you
> haven't received many helpful suggestions here, you might try a netbeans
> specific list for suggestions on how to accomplish this.
>
> Kris Jurka
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2005-09-29 18:37 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-09-28 20:27 How to retieve binary data (bytea) without problem ? Alfredo Rico <alfredorico@gmail.com>
2005-09-29 12:20 ` Russell Francis <rfrancis@ev.net>
2005-09-29 13:52   ` Alfredo Rico <alfredorico@gmail.com>
2005-09-29 17:06     ` Kris Jurka <books@ejurka.com>
2005-09-29 18:37       ` Alfredo Rico <alfredorico@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox