Im presently experimentation on storing large files on the MySQL 5.5 database using java. My primary class is known as FileDatabaseTest. It's the next method:

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

...

public class FileDatabaseTest {

...

private void uploadToDatabase(File file, String description) {
        try {
            PreparedStatement stmt = connection.prepareStatement(
                "INSERT INTO FILES (FILENAME, FILESIZE, FILEDESCRIPTION, FILEDATA) " +
                    "VALUES (?, ?, ?, ?)");
            stmt.setString(1, file.getName());
            stmt.setLong(2, file.length());
            stmt.setString(3, description);
            stmt.setBinaryStream(4, new FileInputStream(file));
            stmt.executeUpdate();
            updateFileList();
            stmt.close();
        } catch(SQLException e) {
            e.printStackTrace();
        } catch(FileNotFoundException e) {//thrown by FileInputStream constructor
            e.printStackTrace();
        } catch(SecurityException e) { //thrown by FileInputStream constructor
            e.printStackTrace();
        }
    }

...

}

The database only has one Table - the "FILES" table, and contains the next posts.

ID - AUTOINCREMENT, PRIMARY KEY

FILENAME - VARCHAR(100)

FILESIZE - BIGINT

FILEDESCRIPTION - VARCHAR(500)

FILEDATA - LONGBLOB

This program is working fine when uploading small documents, however when I upload files like 20MB, the upload process is extremely slow. And So I attempted putting the FileInputStream in the BufferedInputStream within the following code:

stmt.setBinaryStream(4, new BufferedInputStream(new FileInputStream(file));

The upload process grew to become extremely fast. Its like just copying the file to a different directory. However when I attempted to upload files a lot more than 400mb, I acquired the next error:

Exception in thread "Thread-5" java.lang.OutOfMemoryError: Java heap space
    at com.mysql.jdbc.Buffer.ensureCapacity(Buffer.java:156)
    at com.mysql.jdbc.Buffer.writeBytesNoNull(Buffer.java:514)
    at com.mysql.jdbc.PreparedStatement.escapeblockFast(PreparedStatement.java:1169)
    at com.mysql.jdbc.PreparedStatement.streamToBytes(PreparedStatement.java:5064)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2560)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2401)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
    at FileDatabaseTest$2.run(FileDatabaseTest.java:312)
    at java.lang.Thread.run(Thread.java:662)

And So I attempted utilizing an embedded Apache-Derby database rather than MySQL, and that i did not obtain the error. I could upload 500MB to at least one.5G files within the Derby database while using BufferedInputStream. I additionally observed that while using the BufferedInputStream using the MySQL server in uploading large files, the JVM is eating lots of memory, while after i tried on the extender within the Derby database, the JVM's memory usage is maintaned around 85MB TO 100MB.

I'm relatively recent to MySQL and i'm simply using its default designs. The only real factor I transformed in the configuration may be the "max_permitted_packet" size in order to upload as much as 2GB file towards the database. And So I question in which the error originated from. Could it be a bug of MySQL or even the MySQL connector/J? or perhaps is there a problem with my code?

Things I am attempting to achieve here's to have the ability to upload large files (as much as 2GB) towards the MySQL server using java, without growing the java heap space.

Only for the heck from it, try boosting your JVM heap size.

increase the java heap size permanently? http://javahowto.blogspot.com/2006/06/6-common-errors-in-setting-java-heap.html