Using bytea with ResultSet.getBytes("..."). Problem. Help!!

From: "roman" <xroot(at)mail(dot)ru>
To: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Using bytea with ResultSet.getBytes("..."). Problem. Help!!
Date: 2007-07-19 04:20:18
Message-ID: 003001c7c9bc$1da75a50$5405a8c0@nnp.nsk.su
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hello!
(I use Netbeans 5.5, Jdk 1.6, postgreSql 8.1.5 on linux host).

I have a problem with reading big binary data from BYTEA field.
I write to it about 16MB pdf file (as byte[] array) sucessfully, but when I try to read it back I have an exception:

Exception occurred during event dispatching:
java.lang.OutOfMemoryError: Java heap space
at org.postgresql.util.PGbytea.toBytes(PGbytea.java:53)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBytes(AbstractJdbc2ResultSet.java:2152)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBinaryStream(AbstractJdbc2ResultSet.java:2272)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBinaryStream(AbstractJdbc2ResultSet.java:2367)

I have no problem with files about 1MB.

My code for writing object:
...
java.sql.PreparedStatement insAttachment=connection.prepareStatement("INSERT INTO \"T_Table\" (\"f_InitialDocument\") VALUES (?)"); //f_InitialDocument is a BYTEA field
...
public void insertAttachment(String FILENAME) {
byte[] buf=null;
try {
//first read file to buf
java.io.File f=new java.io.File(FILENAME);
if(f.exists() && f.canRead()) {
java.io.RandomAccessFile raf=new java.io.RandomAccessFile(f, "r");
buf=new byte[(int)raf.length()];
raf.readFully(buf);
raf.close();
//Write it to database
insAttachment.setBytes(1, buf);
insAttachment.execute();
} else { ret=false; }
} catch(Exception e) {
ret=false;
}
}

My code for reading object:
...
String query="SELECT \"f_InitialDocument\" FROM \"T_Attachments\"";
java.sql.ResultSet rs=database.executeFastQuery(query); //My function for executing query which gives java.sql.ResultSet
if(rs!=null) {
try {
if(rs.next()) {
byte[] buf=rs.getBytes("f_InitialDocument"); //HERE I GOT THAT EXCEPTION ABOUT OutOfMemory!!!
//Create a template file
String filename=database.getTempFilename(); //Function for generating temporary filenames basing on GUID
java.io.File f=new java.io.File(filename);
if(f.createNewFile()) {
java.io.FileOutputStream fos=new java.io.FileOutputStream(f);
fos.write(buf);
fos.close();
database.executeCommand(filename); //function for opening file in appropiate application
}
}
} catch(Exception e){}
}
...

I tried to launch my program with options: -Xmx800M -Xms1500M, but it didn't help...

I tried to use: java.io.InputStream is=rs.getBinaryStream("f_InitialDocument"); but got the same error in this string in debugger.

How can I read such big objects???
Please help me.

Roman.

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Badawy, Mohamed 2007-07-19 09:46:00 sessionVariables=FOREIGN_KEY_CHECKS=0
Previous Message Kris Jurka 2007-07-18 21:09:35 Re: IN clause with PreparedStatement