Hibernate java.lang.OutOfMemoryError:Java堆空间 [英] Hibernate java.lang.OutOfMemoryError: Java heap space
问题描述
我有一个运行在Tomcat 7上的Java Web应用程序 - jdk1.7
这个应用程序使用Spring 4.1.5.RELEASE和Hibernate 4.2.2.Final
p>我的问题是构建部分工厂的Heap空间的OutOfMemoryException
这是我的静态方法,它打开SessionFactory
public class GenericDAO {
public static SessionFactory sessionFactory = null;
public static ServiceRegistry serviceRegistry = null;
交易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;
$ b $这些是DAO的例子
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' ();
尝试{
tx = session.beginTransaction();
session.save(item);
tx.commit();
return item.getId();
} catch(HibernateException e){
if(tx!= null)tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
返回-1;
$ / code $ / pre
错误发生在包含
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
部署,但在使用2到3天后
这是我的Hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate Configuration DTD // EN
http://www.hibernate.org/dtd/冬眠配置-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>用户名< / property>
< property name =connection.password>密码< / property>
< mapping class =it.company.client.project.hibernate.MyObject/>
<! - 如果需要,数据库模式将被更新 - >
<! - < property name =hbm2ddl.auto>更新< / property> - >
< / session-factory>
< / hibernate-configuration>
解决方案您必须创建会话工厂一次是一个沉重的重量物体,请参阅
以下是文档中关于如何创建应用程序的示例代码:
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
导入org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory(){
try {
//从hibernate.cfg.xml创建SessionFactory
返回新的Configuration().configure()。 buildSessionFactory(
new StandardServiceRegistryBuilder()。build());
}
catch(Throwable ex){
//确保您记录异常,因为它可能被吞噬
System.err.println(初始化SessionFactory创建失败。 + ex);
抛出新的ExceptionInInitializerError(ex);
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
I have a Java web application running on Tomcat 7 - jdk1.7
This application uses Spring 4.1.5.RELEASE and Hibernate 4.2.2.Final
My problem is a OutOfMemoryException of the Heap space on building section factory
This is my static method that opens a 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;
}
}
And this is an example of 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;
}
}
The error occurs at the line containing
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
The problem doesn't occur immediately at the deploy, but after 2 o 3 days of usage
This is my 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>
解决方案 You have to create the session factory only once as it is a heavy weight object, refer to the hibernate documentation for its details.
Here is the sample code from the doc on how it should be created:
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;
}
}
这篇关于Hibernate java.lang.OutOfMemoryError:Java堆空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!