I am trying to figure out the βcorrectβ, safe way to save / retrieve files in a mysql database using Hibernate. The solution should work with files that are large enough, so it is important to minimize the amount of memory.
My decision:
The model class containing the java.sql.Blob field, the getter is annotated using "@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) {
e.printStackTrace();
}
resultExtraDAO.save(resultExtra);
factory.getResultExtraDAO().commitTransaction();
}
Is this the right way to do this? If there is a risk of running out of memory, if are manly simultaneous downloads and / or large files? Is there any other solution that would be better?
, "" ", habernate, HibernateUtil , , . .