论坛首页 Java企业应用论坛

hibernate 读取Blob字段 OutOfMemoryError

浏览 9142 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-06-06  
数据库:mysql server 6.0
driver:mysql-connect-java-5.0.4-bin.jar
Hibernate:3.2

读mysql user表中的一个BLOB字段,出现这种错误:java.lang.OutOfMemoryError: Java heap space

下面是我的代码:

User类:

package org.redleaf;

import java.sql.Blob;

public class User {

	private int id ;
	private String name ;
	private Blob details ;
	
	public Blob getDetails() {
		return details;
	}
	public void setDetails(Blob details) {
		this.details = details;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}



user.hbm.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="org.redleaf.User" table="user"> 
       <id name="id" column="id">
          <generator class="native" />
       </id>
       <property name="name" column="name" />
       <property name="details" column="details" type="java.sql.Blob" lazy="true"/>
    </class>
</hibernate-mapping>



下载的servlet程序:

package org.redleaf.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class download extends HttpServlet {

	public download() {
		super();
	}

	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		org.hibernate.cfg.Configuration conf = new org.hibernate.cfg.Configuration().configure("/workspace.cfg.xml");
		org.hibernate.SessionFactory sessionFactory = conf.buildSessionFactory();
		org.hibernate.Session se = sessionFactory.openSession();
		org.redleaf.User user = (org.redleaf.User)se.get(org.redleaf.User.class, new Integer(4));//运行到这里就出错了,好像details字段并没有延迟加载
		
		String display_name = "test文件.chm";  //该文件存在数据库的details字段,有33MB
		
		response.setHeader("Content-disposition","attachment;filename=" + new String(display_name.getBytes("gbk"),"iso8859-1"));
		
		BufferedInputStream input = null;
		BufferedOutputStream output = null;
		try{
		
				java.sql.Blob details = user.getDetails();

			
				java.io.InputStream in = details.getBinaryStream(); 
			
			    input = new BufferedInputStream(in);
			    output = new BufferedOutputStream(response.getOutputStream());
			    int len = 0;
			    byte [] b = new byte[10240];
			    while((len = input.read(b)) != -1){
		    	    	output.write(b,0,len);
			    }


		}catch(Exception e){ 
		    e.printStackTrace();
		}finally{
			if(output!=null) output.close();
		    if(input!=null) input.close();
		}
		
		
		/*
		OutputStream out = response.getOutputStream();
		try{
			
			out.write(user.getDetails().getBytes(1,(int) user.getDetails().length()));
        
      
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			  out.flush();
		      out.close();
		}
		*/
	}

	public void init() throws ServletException {

	}

}


大家帮我看看,有什么办法让这个details字段从数据库里读出来不出java.lang.OutOfMemoryError: Java heap space这个错误
   发表时间:2007-06-06  
我试过下载4MB左右的文件并没有出错,但好像大一点的文件就读不出来了,会有java.lang.OutOfMemoryError: Java heap space这个错误
0 请登录后投票
   发表时间:2007-06-06  
为什么details字段不会延迟加载啊?
0 请登录后投票
   发表时间:2007-06-06  
字段的延迟加载要进行字节码增强
0 请登录后投票
   发表时间:2007-06-06  
dennis_zane 写道
字段的延迟加载要进行字节码增强

什么意思啊?
0 请登录后投票
   发表时间:2007-06-06  
redleaf 写道
我试过下载4MB左右的文件并没有出错,但好像大一点的文件就读不出来了,会有java.lang.OutOfMemoryError: Java heap space这个错误

在oracle里,如果超过4m就不能使用thin那个驱动了,要换一个驱动,那个以o什么来着的驱动,不知道mysql6是不是也会有这种问题
0 请登录后投票
   发表时间:2007-06-06  
你看看“http://www.iteye.com/topic/80620”是否有帮助?
0 请登录后投票
   发表时间:2007-06-06  
我认为是hibernate在读这条数据时并没有把blob字段延迟加载,而是直接读到内存来造成的
0 请登录后投票
   发表时间:2007-06-06  
有些资料说mysql没有延迟加载,晕死
0 请登录后投票
   发表时间:2007-06-07  
hgq0011 写道
你看看“http://www.iteye.com/topic/80620”是否有帮助?

我看了一下,好像是说虚拟机的问题,不知道最新的java6有没有改变和提高
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics