1

I'm using MySQL with Hibernate to save uploaded files in the database. The file is transformed into a byte array as a field of a simple entity:

@Entity
@Table(name = "TestBlob")
public class TestBlob {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Lob
    @Column(name = "file")
    private byte[] file;

    public byte[] getFile() {
        return file;
    }

    public void setFile(byte[] file) {
        this.file = file;
    }
}

Then when I try to run the following code:

    @Override
     public void addBlob(TestBlob t) {
      System.out.println("Trying to upload file with size:" + t.getFile().length);
      Session session = sessionFactory.getCurrentSession();
      session.save(t);
    }

    @Override
     public void updateBlob(TestBlob t) {
      System.out.println("Trying to upload file with size:" + t.getFile().length);
      Session session = sessionFactory.getCurrentSession();
      session.saveOrUpdate(t);
    }

I get this output:

Trying to upload file with size:8663040
Hibernate: 
    insert 
    into
        TestBlob
        (file) 
    values
        (?)
...
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (17326125 > 16777216).

The only relevant post I've found is Loading 26MB text data from database consume JVM heap of 258MB or posts related to Postgre. Is there anything I can fix about my code or is it a some weird quirk that I need to know?

EDIT: I'm required to save files in a blob.

Community
  • 1
  • 1
Entro
  • 23
  • 1
  • 5
  • 1
    I think it would be better to put files out of the database and just put links to files in the database – Bilal BBB May 23 '15 at 17:16

2 Answers2

0

You would need to configure your MySQL server to handle a larger packet size. See the following MySQL documentation for more information: http://dev.mysql.com/doc/refman/5.6/en/packet-too-large.html

codedabbler
  • 1,231
  • 7
  • 13
0

Is there any way to insert a large value in a mysql DB without changing max_allowed_packet?

If you use Preparedstatement and there setBytes, then the Problem with doublesize is there, because of the Mysql-Driver behavior which doubles the size. Use setBinaryStream instead.

Meik
  • 16