I'm trying to figure out the 'correct', failsafe way to save/retrieve files in a mysql database using Hibernate. The solution has to work with files that are quite large, so minimizing the memory footprint would be important.
My solution, so far:
A model class containing a java.sql.Blob field, the getter annotated with "@Lob".
public static void main(String[] args) {
DAOFactory factory = DAOFactory.getFactory();
IResultExtraDAO resultExtraDAO = factory.getResultExtraDAO();
factory.getResultExtraDAO().beginTransaction();
ResultExtra resultExtra = new ResultExtra();
LobHelper lh = HibernateUtil.getSession().getLobHelper();
try {
File file = new File("/3030.jpg");
FileInputStream fis = new FileInputStream(file);
Blob b = lh.createBlob(fis, fis.available());
resultExtra.setFile(b);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
resultExtraDAO.save(resultExtra);
factory.getResultExtraDAO().commitTransaction();
}
Is this the correct way to do it? Whould there be a risk of running out of memmory if there are manny simultanious uploads and/or large files? Is there another solution that would be better?
Also, I'm using the 'Generic Data Access Object' pattern encapsule hibernate, so I don't like accessing the HibernateUtil directly as I do here, but so far I haven't figured out a good generic way to access it. Suggestions are welcome.