Hibernate:OutOfMemoryError:PermGen空间 [英] Hibernate :OutOfMemoryError: PermGen space

查看:196
本文介绍了Hibernate:OutOfMemoryError:PermGen空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人会说我的应用程序有什么问题吗?

  public class HibernateUtil {
private static final SessionFactory sessionFactory;

static {
try {
sessionFactory = new AnnotationConfiguration()。configure()。buildSessionFactory();
} catch(Throwable ex){
System.err.println(Initial SessionFactory creation failed。+ ex);
抛出新的ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}

public boolean insertUser(User user){
Session session = HibernateUtil.getSessionFactory()。openSession();
try {
session.beginTransaction();
session.save(user);
session.getTransaction()。commit();

} catch(HibernateException他){
session.getTransaction()。rollback();
返回false;
} finally {
if(session!= null){
session.close();
}
}

返回true;
}

这是我调用将数据库中插入用户的唯一方法,那么如果我尝试插入另外一个例外:

 创建初始SessionFactory failed.java.lang.OutOfMemoryError:PermGen空间






更新:
我不知道hibernate.cfg中有没有什么东西,但是在这里:

 <?xml version =1.0encoding =UTF -8\" >?; 




org.hibernate.dialect.MySQLDialect
com.mysql.jdbc.Driver
jdbc:mysql:// localhost:3306 / XXXXX
XXXX
XXXXc
线程

 <! - 连接池C3P0  - > 
< property name =hibernate.c3p0.acquire_increment> 2< / property>
< property name =hibernate.c3p0.min_size> 5< / property>
< property name =hibernate.c3p0.max_size> 100< / property>
< property name =hibernate.c3p0.timeout> 20< / property>
< property name =hibernate.c3p0.max_statements> 50< / property>
< property name =hibernate.c3p0.idle_test_period> 60< / property>

<! - 在屏幕上显示sql输出 - >
< property name =hibernate.show_sql> true< / property>
< property name =hibernate.format_sql> true< / property>

<! - 映射我的模式中的表 - >
...

< / session-factory>



为什么会发生这种情况?修改它?



最好的问候,
Valter Henrique。

解决方案

Hibernate和大型项目非常常见。 PermGen是存储类的地方(这不是堆!)。每个类都进入PermGen,如果项目足够大,那么64MB的默认大小还不够。



Hibernate为您的实体生成代理类。这可能是为什么在Hibernate的上下文中得到这个错误的原因。生成的类也可以填充PermGen。



一般建议:如果它不是一个玩具项目,只需增加您的PermGen与 -XX:MaxPermSize = 96M 而且您可以安全地使用一段时间。



另请注意,Web应用程序容器在卸载应用程序时可能有问题,原因有几个(服务器端log4j配置是常见问题)并且所有Hibernate生成的类将会每个webapp一次又一次地创建。在这种情况下,请更好地重新启动服务器。


could anyone say what's wrong with my application?

public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

public boolean insertUser(User user) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();

    } catch (HibernateException he) {
        session.getTransaction().rollback();
        return false;
    }finally{
        if (session != null){
            session.close();
        }
    }

    return true;
}

This is the only method i call to insert an user in the database, and then if i try to insert another one, launch this exception:

Initial SessionFactory creation failed.java.lang.OutOfMemoryError: PermGen space


UPDATE: I don't know if there's something in hibernate.cfg but here it go:

<?xml version="1.0" encoding="UTF-8"?>

org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/XXXXX XXXX XXXXc thread

    <!-- Connection pool C3P0 -->
    <property name="hibernate.c3p0.acquire_increment">2</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">100</property>
    <property name="hibernate.c3p0.timeout">20</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">60</property>

    <!-- To show sql output on the screen -->
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>

    <!-- Mapping the tables in my schema -->
    ...

</session-factory>

Why this happen and how to fix it ?

Best regards, Valter Henrique.

解决方案

Very common with Hibernate and large project. The PermGen is the place where the classes are stored (this is not the Heap!). Every now class comes into PermGen, and if the project is large enought, the default size of 64MB is not enought anymore.

Hibernate generated Proxy classes for your entities on the fly. This might be a reason why you get this error in the context of Hibernate. The generated classes also might fill the PermGen.

General advice: If it is not a toy project, just increase your PermGen with -XX:MaxPermSize=96M and you are safe for a while.

Please also note that Web application containers might have problems unloading applications for several reasons (a server wide log4j configuration is a common problem), and all of Hibernates generated classes will be created again and again per webapp. Better restart your server in this case.

这篇关于Hibernate:OutOfMemoryError:PermGen空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆