Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Heap Space Error while inserting large file.

843859Jul 23 2009 — edited Nov 20 2014
Hi All,
I have searched a lot on internet and could not find a real answer! I will appreciate any help regarding the following issue.
I have the following code where I am trying to insert a file.
String sql = "insert into pictures(data,name,length) values(?,?,?)"; // get the picture and insert it first
            prs = conn.prepareStatement(sql);
 prs.setBinaryStream(1, i.getInputStream(), i.getInputStream().available());
                prs.setString(2, i.getName());
                prs.setLong(3, i.getSize());
                prs.execute();
My code breaks if I try to insert a file bigger then couple of MBs (I would say more then 10 MBs)
The run time exception is as following
java.lang.OutOfMemoryError: Java heap space
	com.mysql.jdbc.Buffer.ensureCapacity(Buffer.java:157)
	com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2167)
	com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2103)
	com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:934)
	org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:169)
	com.cynosureweb.photosync.db.DAO.addPictures(DAO.java:61)
	com.cynosureweb.photosync.servlets.Pictures.doPost(Pictures.java:83)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	com.cynosureweb.photosync.filter.AuthFilter.doFilter(AuthFilter.java:41)
The same code works perfectly for smaller files.
I am using MySql 5.0 with JDBC MySql connector 5.1.6
Please help I have spent hours trying to fix this issues will really appreciate anyone's help on this.
Thanks

Edited by: Java_Wiz on Jul 23, 2009 8:55 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 24 2009
Added on Jul 23 2009
12 comments
1,071 views