使用Hibernate高GC负载 [英] High GC load using Hibernate
问题描述
我使用Spring MVC + Hibernate开发了一个Web应用程序,并且在运行负载测试时看起来垃圾收集器过于频繁地被调用。恐怕这可能与我管理冬眠会议的方式有关。
I have developed a web application using Spring MVC + Hibernate and when running load tests it seems the Garbage Collector is being called too often. I am afraid it might have to do with the way I manage Hibernates session.
我有一个AbstractDao,它是我所有DAO对象的扩展:
I have an AbstractDao which all of my DAO objects extends:
public class AbstractDaoHibernateImpl {
protected GenericDataBaseExceptionHandler exceptionHandler;
private SessionFactory sessionFactory;
public AbstractDaoHibernateImpl() {
}
public void setExceptionHandler(GenericDataBaseExceptionHandler exceptionHandler) {
this.exceptionHandler = exceptionHandler;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
}
在我定义的spring entity bean configuration我的每个DAO都是这样的:
In spring entity bean configuration I define each of my DAOs like this:
<bean name="userDao" class="com.kelmer.dao.UserDaoImpl">
<property name="sessionFactory" ref="SessionFactory" />
<property name="exceptionHandler" ref="defaultSpringExceptionHandler" />
</bean>
然后这是我的一个DAO的示例方法:
And then this is a sample method from one of my DAOs:
@Override
public EstacionVO findById(Long id) throws InstanceNotFoundException {
User e = (User ) getSession().createQuery(SELECT_USER_BY_ID).setParameter("userId", id).uniqueResult();
if (e == null) {
throw new InstanceNotFoundException ("No user with provided Id", User.class);
}
return e;
}
我真的可以看到任何可能导致内存泄漏的事情,这里没有真正的专家。我在做会话管理吗?我知道我从一些遗留代码中抽取了AbstractDao的代码,我担心这是内存问题的原因,因为没有明确的会话关闭或终结。
I honestly can see anything that might be causing a memory leak but then again I'm no real expert here. Am I doing session management right? I know I took the code from AbstractDao from some legacy code and I fear that's where the memory problem could be, since there is no explicit session closing or finalizing.
PS。对于事务管理,我在Spring上下文中使用< tx:annotation-driven />
,并使用 @Transactional $ c注释每个方法$ c>。
PS. For transaction management, I am using <tx:annotation-driven />
in spring context and annotating each method with @Transactional
.
推荐答案
乍一看,代码中似乎没有任何异常。
At first glance, there doesn't seem to be anything unusual in your code.
运行性能监视器以查看创建了哪些对象。
Run a performance monitor to see what objects are created.
除非问题不重要,否则很难看出性能通过查看代码和90%的时间而丢失,任何声明它一定是这个结果是错误的。
Unless the problem is trivial, it's hard to see where performance is lost by just looking at the code and 90% of the time, any statement "it must be this" turns out to be wrong.
这篇关于使用Hibernate高GC负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!