db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Grégoire Dubois <gregoire.dub...@online.fr>
Subject Re: Reading blobs larger than a certain size fails with memory error
Date Sun, 08 Jan 2006 01:15:04 GMT
Here is some sample code.
I write a file from disk into the database, and then read the file from
the database, and write it on disk.



import java.sql.*;
import java.io.*;

/**
 *
 * @author greg
 */
public class derby_filewrite_fileread {
    
    private static File file = new
File("/mnt/BigDisk/Clips/BabyMamaDrama-JShin.wmv");
    private static File destinationFile = new
File("/home/greg/DerbyDatabase/"+file.getName());
    
    /** Creates a new instance of derby_filewrite_fileread */
    public derby_filewrite_fileread() {        
        
    }
    
    public static void main(String args[]) {
        try {

Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
            Connection connection = DriverManager.getConnection
("jdbc:derby:/home/greg/DerbyDatabase/BigFileTestDB;create=true", "APP",
"");
            connection.setAutoCommit(false);
            
            Statement statement =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
            ResultSet result = statement.executeQuery("SELECT TABLENAME
FROM SYS.SYSTABLES");
            
            // Create table if it doesn't already exists.
            boolean exist=false;
            while ( result.next() ) {
                if ("db_file".equalsIgnoreCase(result.getString(1)))
                    exist=true;
            }
            if ( !exist ) {
                System.out.println("Create table db_file.");
                statement.execute("CREATE TABLE db_file ("+
                                           "     name
VARCHAR(40),"+
                                           "     file          BLOB(2G)
NOT NULL)");
                connection.commit();
            }
            
            // Read file from disk, write on DB.
            System.out.println("1 - Read file from disk, write on DB.");
            PreparedStatement
preparedStatement=connection.prepareStatement("INSERT INTO
db_file(name,file) VALUES (?,?)");
            FileInputStream fileInputStream = new FileInputStream(file);
            preparedStatement.setString(1, file.getName());
            preparedStatement.setBinaryStream(2, fileInputStream,
(int)file.length());            
            preparedStatement.execute();
            connection.commit();
            System.out.println("2 - END OF Read file from disk, write on
DB.");
            
            
            // Read file from DB, and write on disk.
            System.out.println("3 - Read file from DB, and write on
disk.");
            result = statement.executeQuery("SELECT file FROM db_file
WHERE name='"+file.getName()+"'");
            byte[] buffer = new byte [1024];
            result.next();
            BufferedInputStream     inputStream=new
BufferedInputStream(result.getBinaryStream(1),1024);
            FileOutputStream outputStream = new
FileOutputStream(destinationFile);
            int readBytes = 0;
            while (readBytes!=-1) {
                readBytes=inputStream.read(buffer,0,buffer.length);
                if ( readBytes != -1 )
                    outputStream.write(buffer, 0, readBytes);
            }      
            inputStream.close();
            outputStream.close();
            System.out.println("4 - END OF Read file from DB, and write
on disk.");
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}


It returns 
1 - Read file from disk, write on DB.
2 - END OF Read file from disk, write on DB.
3 - Read file from DB, and write on disk.
java.lang.OutOfMemoryError
if the file is ~10MB or more

Greg

Le vendredi 06 janvier 2006 à 20:43 -0800, Daniel John Debrunner a
écrit :

> Grégoire Dubois wrote:
> 
> > Hi all,
> > 
> > I am using the embedded driver.
> > 
> > I can write big blobs into the database , but reading blobs larger than
> > a certain size fails with memory error
> > 
> > (java.lang.OutOfMemoryError).
> > 
> > *How to repeat:*
> > Insert a blob about 10Mo or more in size and then try to read it back out.
> > 
> > 
> > Does someone else have the same problem ? Do I have to report a bug ?
> 
> Do you have sample code that shows the problem?
> 
> Dan.
> 
> 

Mime
View raw message