pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BLOB support problem !!!!
3+ messages / 2 participants
[nested] [flat]

* BLOB support problem !!!!
@ 2004-06-23 11:30 GP <pagomen@csd.uoc.gr>
  2004-06-23 11:55 ` Re: BLOB support problem !!!! Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 3+ messages in thread

From: GP @ 2004-06-23 11:30 UTC (permalink / raw)
  To: pgsql-jdbc; GP <pagomen@csd.uoc.gr>

Hi all,

I have some java code to r/w a binary file in the database.
According to the tutorial 
(http://doc.postgresintl.com/jdbc/ch08.html#jdbc-binary-data-example)
there are 2 ways:

1) To use a 'bytea' type in the database

CREATE TABLE images (imgname text, img bytea);

File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setBinaryStream(2, fis, file.length());
ps.executeUpdate();
ps.close();
fis.close();


2) Use the LargeObject API and the 'oid' type in the databse


CREATE TABLE imageslo (imgname text, imgoid oid);

 // All LargeObject API calls must be within a transaction block
conn.setAutoCommit(false);

// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();

// Create a new large object
int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE);

// Open the large object for writing
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);

// Now open the file
File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);

// Copy the data from the file to the large object
byte buf[] = new byte[2048];
int s, tl = 0;
while ((s = fis.read(buf, 0, 2048)) > 0) {
    obj.write(buf, 0, s);
    tl += s;
}

// Close the large object
obj.close();

// Now insert the row into imageslo
PreparedStatement ps = conn.prepareStatement("INSERT INTO imageslo VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setInt(2, oid);
ps.executeUpdate();
ps.close();
fis.close();


I tried both of them BUT
The first one throw an OutofMemoryException for files > 1.5 Mbyte (is 
this some kind of bug ??)
And the second one doesn't throw any Exception or Error but it does NOT 
write anything in the database !!

I did something wrong ?
Has anyone experience in r/w blobs in postgres through jdbc ??

Thanks in advance
George

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

* Re: BLOB support problem !!!!
  2004-06-23 11:30 BLOB support problem !!!! GP <pagomen@csd.uoc.gr>
@ 2004-06-23 11:55 ` Oliver Jowett <oliver@opencloud.com>
  2004-06-23 13:36   ` Re: BLOB support problem !!!! GP <pagomen@csd.uoc.gr>
  0 siblings, 1 reply; 3+ messages in thread

From: Oliver Jowett @ 2004-06-23 11:55 UTC (permalink / raw)
  To: pagomen@csd.uoc.gr; +Cc: pgsql-jdbc

GP wrote:

> I tried both of them BUT
> The first one throw an OutofMemoryException for files > 1.5 Mbyte (is 
> this some kind of bug ??)

It's a known issue. There are patches pending to fix it that will 
hopefully be applied soon. A workaround is to increase your JVM's heap size.

> And the second one doesn't throw any Exception or Error but it does NOT 
> write anything in the database !!

It looks like it's missing a conn.commit() call at the end, so none of 
the changes will actually be committed..

-O



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

* Re: BLOB support problem !!!!
  2004-06-23 11:30 BLOB support problem !!!! GP <pagomen@csd.uoc.gr>
  2004-06-23 11:55 ` Re: BLOB support problem !!!! Oliver Jowett <oliver@opencloud.com>
@ 2004-06-23 13:36   ` GP <pagomen@csd.uoc.gr>
  0 siblings, 0 replies; 3+ messages in thread

From: GP @ 2004-06-23 13:36 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

Thanks Oliver

The conn.commit() sloved my problems !


Oliver Jowett wrote:

> GP wrote:
>
>> I tried both of them BUT
>> The first one throw an OutofMemoryException for files > 1.5 Mbyte (is 
>> this some kind of bug ??)
>
>
> It's a known issue. There are patches pending to fix it that will 
> hopefully be applied soon. A workaround is to increase your JVM's heap 
> size.
>
>> And the second one doesn't throw any Exception or Error but it does 
>> NOT write anything in the database !!
>
>
> It looks like it's missing a conn.commit() call at the end, so none of 
> the changes will actually be committed..
>
> -O






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


end of thread, other threads:[~2004-06-23 13:36 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-06-23 11:30 BLOB support problem !!!! GP <pagomen@csd.uoc.gr>
2004-06-23 11:55 ` Oliver Jowett <oliver@opencloud.com>
2004-06-23 13:36   ` GP <pagomen@csd.uoc.gr>

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