java - hibernate java.lang.OutOfMemoryError : Java heap space

标签 java hibernate web-applications tomcat7

我有一个在 Tomcat 7 - jdk1.7 上运行的 Java Web 应用程序

此应用程序使用 Spring 4.1.5.RELEASE 和 Hibernate 4.2.2.Final

我的问题是在构建 section factory 时出现堆空间的 OutOfMemoryException

这是我打开 SessionFactory 的静态方法

public class GenericDAO {

    public static SessionFactory sessionFactory = null;
    public static ServiceRegistry serviceRegistry = null;

    Transaction tx = null;

    public static SessionFactory createSessionFactory() {
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(
            configuration.getProperties()). buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    }
}

这是 DAO 的一个例子

public class SpecificDAO extends GenericDAO {
    public int save(MyObject item) {
        Session session = createSessionFactory().openSession();
        try {
            tx = session.beginTransaction();
            session.save(item);
            tx.commit();
            return item.getId();
        } catch (HibernateException e) {
            if (tx != null) tx.rollback();
                 e.printStackTrace();
        } finally {
            session.close();
        }
        return -1;
    }
}

错误发生在包含

的行

sessionFactory = configuration.buildSessionFactory(serviceRegistry);

问题不会在部署时立即出现,而是在使用 2 到 3 天后出现

这是我的 Hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
<session-factory>
    <property name="connection.url">jdbc:sqlserver://192.168.XX.XXX:1433;databaseName=DatabaseName</property>
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="connection.username">username</property>
    <property name="connection.password">password</property>
    <mapping class="it.company.client.project.hibernate.MyObject"/>

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>

最佳答案

您只需创建 session 工厂一次,因为它是一个重量级对象,请参阅 hibernate documentation了解详情。

这是文档中关于如何创建它的示例代码:

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory(
                new StandardServiceRegistryBuilder().build() );
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

关于java - hibernate java.lang.OutOfMemoryError : Java heap space,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30234995/

相关文章:

java 在操作textField数据时重写textField上的getText方法

java - 从 xwork-core-2.2.1.jar 设置 Struts-Spring 集成时出现 fatal error

java - 使用jsp显示空结果

eclipse - 无法在 Eclipse 中加载 Maven 项目中的 Widgetsets

java - Websocket 一段时间后(20-30 分钟)停止接收数据

Java - LinkedList push() pop() 暗示它是一个堆栈,而不是一个队列?

java - 使用复合键的 Hibernate XML 子类化

java - 任何持久更改的审计日志,不使用数据库触发器,而是使用 spring/hibernate

java - JPA 保留具有外键值的实体,但不想显式查找外键实体

java - Struts 调用的是什么方法?