Ok, here are the big lines. I want to store files in a database. When I work with small files (ex, 5 meg), I got no problem at all. I can save/load them perfectly. However, when I attempt the same thing with a large file (ex, 50 meg, 130 meg), while I can save the file fine, and I know the file entry is good and well as I can validate that with a mysql client, I can't load it properly. I get outofmemory errors.
Now, the problem might arise from my faulty understanding of how blob objects work. I was hoping for the blob object returned by the query NOT to return the full byte data, but rather some form of stream to it, that I could then manipulate using getBinaryStream(). What I notice happens instead is the entire byte array is getting loaded into the blob object.
Now, here's the question: is there a way to manipulate streams with blob objects? Is there some more intelligent way to retrieve large files from a database? Am I wrong in some of the assumptions I make and the problem is elsewhere? Could it be something related to the mysql driver not giving me a stream?
Any help appreciated :-)
Hibernate version:
3.2 everywhere
Code between sessionFactory.openSession() and session.close():
Code:
public void raw(int iID){
Session s = HibernateUtilities.getSessionFactory().getCurrentSession();
Transaction t = null ;
boolean bSuccess = false;
try{
t = s.beginTransaction();
Query q = s.createQuery("select fd.ID, fd.blobData from FileData fd where fd.ID = "+iID);
List l = q.list();
t.commit();
}
catch(Throwable th) {
th.printStackTrace();
if(t != null)
t.rollback() ;
}
finally {
if(s.isOpen())
s.close();
}
}
Full stack trace of any exception that occurs:java.lang.OutOfMemoryError: Java heap space
Name and version of the database you are using:MySQL5, with v5 drivers
The generated SQL (show_sql=true):Hibernate: select filedata0_.ID as col_0_0_, filedata0_.blobData as col_1_0_ from FileData filedata0_ where filedata0_.ID=2
The mapping:The mapping is the following
Code:
private Long lID;
private Blob bBlobData;
@Id
@GeneratedValue
public Long getID() {
return lID;
}
public void setID(Long lID) {
this.lID = lID;
}
@Lob
@Column(columnDefinition="longblob")
public Blob getBlobData() {
return bBlobData;
}
public void setBlobData(Blob bBlobData) {
this.bBlobData = bBlobData;
}