Hello,
I'm having problems storing files into a database. The main problem is that I have to upload the entire file to memory to pass it to the hibernate object.
I,m using a "bytea" colum in PostegreSQL to store the files.
So, I mapped that column using Binary.
When I generate the hibernate object, it expect a byte[] parameter.
When I convert the file to a byte[], all the file resides in memory and if there isn't enogut memory for it, Tomcat throws the "out of memory" error.
How can I do to store the file using hibernate without upload all the file in memory?
I'd appreciate some help.
Here is some information about my project:
Tomcat version: 5
Hibernate version: 2
Mapping documents:
<hibernate-mapping package="xxx.File">
<class name="File" table="file">
<id name="fileId" type="java.lang.Long" column="file_id">
<generator class="sequence">
<param name="sequence">sec_file</param>
</generator>
</id>
<property name="imagen" column="imagen" type="binary" not-null="true" />
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
...
File imagen = new File(item.getImage());
FileInputStream is = new FileInputStream(imagen);
BufferedInputStream bstr = new BufferedInputStream(is);
int size = (int) imagen.length();
byte[] data = new byte[size];
bstr.read( data, 0, size );
bstr.close();
FileDAO fdao = new FileDAO(HibernateSession.CFG_BD); //MyFile Object Factory
MyFile mf= new MyFile(data);//Hinerit database logic from general DB Handler
long id = fdao.save(mf);
.....
Full stack trace of any exception that occurs:
java.lang.RuntimeException: java.lang.OutOfMemoryError
at mysite.model.MyFileModel.mnt(TemaModel.java:317)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at mysite.model.MyFileModel.processPath(TemaModel.java:178)
at mysite.servlets.Controller.doGet(Controller.java:110)
at mysite.servlets.Controller.doPost(Controller.java:129)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at cr.go.ice.uentp.internetpublico.kioskadmin.filters.DBFilter.doFilter(DBFilter.java:60)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:587)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:688)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.OutOfMemoryError
Name and version of the database you are using:
PostgreSQL 7.4
_________________ Thank you!
|