Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id A09629FB68A for ; Thu, 19 Jul 2007 01:20:05 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 83859-04 for ; Thu, 19 Jul 2007 01:20:00 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from mx7.mail.ru (mx7.mail.ru [194.67.23.27]) by postgresql.org (Postfix) with ESMTP id 5867F9FB687 for ; Thu, 19 Jul 2007 01:20:00 -0300 (ADT) Received: from [217.70.107.253] (port=63134 helo=ws024) by mx7.mail.ru with smtp id 1IBNUc-000G07-00 for pgsql-jdbc@postgresql.org; Thu, 19 Jul 2007 08:19:58 +0400 Message-ID: <003001c7c9bc$1da75a50$5405a8c0@nnp.nsk.su> From: "roman" To: Subject: Using bytea with ResultSet.getBytes("..."). Problem. Help!! Date: Thu, 19 Jul 2007 11:20:18 +0700 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_002D_01C7C9F6.C96D9BD0" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.3028 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/86 X-Sequence-Number: 17260 This is a multi-part message in MIME format. ------=_NextPart_000_002D_01C7C9F6.C96D9BD0 Content-Type: text/plain; charset="koi8-r" Content-Transfer-Encoding: quoted-printable 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.=20 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(AbstractJdbc2ResultS= et.java:2152) at = org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBinaryStream(AbstractJdbc2= ResultSet.java:2272) at = org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBinaryStream(AbstractJdbc2= ResultSet.java:2367) I have no problem with files about 1MB. My code for writing object: ... java.sql.PreparedStatement = insAttachment=3Dconnection.prepareStatement("INSERT INTO \"T_Table\" = (\"f_InitialDocument\") VALUES (?)"); //f_InitialDocument is a BYTEA = field ... public void insertAttachment(String FILENAME) { byte[] buf=3Dnull; try { //first read file to buf java.io.File f=3Dnew java.io.File(FILENAME); if(f.exists() && f.canRead()) { java.io.RandomAccessFile raf=3Dnew = java.io.RandomAccessFile(f, "r"); buf=3Dnew byte[(int)raf.length()]; raf.readFully(buf); raf.close(); //Write it to database insAttachment.setBytes(1, buf); insAttachment.execute(); } else { ret=3Dfalse; } } catch(Exception e) { ret=3Dfalse; } } My code for reading object: ... String query=3D"SELECT \"f_InitialDocument\" FROM = \"T_Attachments\""; java.sql.ResultSet rs=3Ddatabase.executeFastQuery(query); //My = function for executing query which gives java.sql.ResultSet if(rs!=3Dnull) { try { if(rs.next()) { byte[] buf=3Drs.getBytes("f_InitialDocument"); //HERE I = GOT THAT EXCEPTION ABOUT OutOfMemory!!! //Create a template file String filename=3Ddatabase.getTempFilename(); = //Function for generating temporary filenames basing on GUID java.io.File f=3Dnew java.io.File(filename); if(f.createNewFile()) { java.io.FileOutputStream fos=3Dnew = 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=3Drs.getBinaryStream("f_InitialDocument"); but got the same error in = this string in debugger. How can I read such big objects??? Please help me. Roman. ------=_NextPart_000_002D_01C7C9F6.C96D9BD0 Content-Type: text/html; charset="koi8-r" Content-Transfer-Encoding: quoted-printable
Hello!
(I use Netbeans 5.5, Jdk 1.6, = postgreSql 8.1.5=20 on linux host).
 
 
I have a problem with reading big = binary data from=20 BYTEA field.
I write to it about 16MB pdf file (as = byte[] array)=20 sucessfully, but when I try to read = it back I=20 have an exception:
 
Exception occurred during event=20 dispatching:
java.lang.OutOfMemoryError: Java heap=20 space
        at=20 org.postgresql.util.PGbytea.toBytes(PGbytea.java:53)
   = ;    =20 at=20 org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBytes(AbstractJdbc2ResultS= et.java:2152)
       =20 at=20 org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBinaryStream(AbstractJdbc2= ResultSet.java:2272)
       =20 at=20 org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBinaryStream(AbstractJdbc2= ResultSet.java:2367)
I have no problem with files about=20 1MB.
 
My = code for writing=20 object:
...
    = java.sql.PreparedStatement=20 insAttachment=3Dconnection.prepareStatement("INSERT INTO=20 \"T_Table\" (\"f_InitialDocument\") VALUES (?)");=20 //f_InitialDocument is a BYTEA field
...
    public void=20 insertAttachment(String FILENAME)=20 {
        byte[]=20 buf=3Dnull;
        try=20 {
            = //first=20 read file to=20 buf
           = =20 java.io.File f=3Dnew=20 java.io.File(FILENAME);
       &nbs= p;   =20 if(f.exists() && f.canRead())=20 {
           &n= bsp;   =20 java.io.RandomAccessFile raf=3Dnew java.io.RandomAccessFile(f,=20 "r");
          &nbs= p;    =20 buf=3Dnew=20 byte[(int)raf.length()];
       &nb= sp;       =20 raf.readFully(buf);
        &n= bsp;      =20 raf.close();
         &nb= sp;     =20 //Write it to=20 database
          &= nbsp;    =20 insAttachment.setBytes(1,=20 buf);
          &nbs= p;    =20 insAttachment.execute();
       &nb= sp;   =20 } else { ret=3Dfalse; }
        }=20 catch(Exception e)=20 {
           =20 ret=3Dfalse;
        = }
   =20 }
My code for reading = object:
...
     String = query=3D"SELECT=20 \"f_InitialDocument\" FROM = \"T_Attachments\"";
    =20 java.sql.ResultSet rs=3Ddatabase.executeFastQuery(query); //My function = for=20 executing query which gives = java.sql.ResultSet
    =20 if(rs!=3Dnull) {
         try=20 {
             = if(rs.next()) {
    =20             = byte[]=20 buf=3Drs.getBytes("f_InitialDocument"); //HERE I GOT THAT EXCEPTION = ABOUT=20 OutOfMemory!!!
         &= nbsp;      =20 //Create a template file
  =20             &= nbsp;=20 String filename=3Ddatabase.getTempFilename(); //Function for generating = temporary=20 filenames basing on GUID
   =20             &= nbsp;=20 java.io.File f=3Dnew=20 java.io.File(filename);
       &nbs= p;         =20 if(f.createNewFile()) {
       =20             &= nbsp;=20 java.io.FileOutputStream fos=3Dnew=20 java.io.FileOutputStream(f);
       = ;            =   =20 fos.write(buf);
         =              = fos.close();
         &nb= sp;           =20 database.executeCommand(filename); //function for opening file in = appropiate=20 application
         &nbs= p;       =20 }
           &n= bsp;=20 }
         } = catch(Exception=20 e){}
     }
...
 
I tried to launch my program with = options: -Xmx800M=20 -Xms1500M, but it didn't help...
 
I tried to use: java.io.InputStream=20 is=3Drs.getBinaryStream("f_InitialDocument"); but got the same error in = this=20 string in debugger.
 
How can I read such big = objects???
Please help me.
 
Roman.
 
 
 
------=_NextPart_000_002D_01C7C9F6.C96D9BD0--