hibernate中query.list()内存不释放

xuguiyi100 2016-09-17 10:24:28
求解决hibernate中query.list()内存不释放?

public List<T> getList(String hql,String... values)throws Exception {
Session session = HibernateSessionFactory.currentSession();
List<T> list = null;
try {

Query query = session.createQuery(hql);
for(int i = 0;i < values.length;i++){
query.setParameter(i, values[i]);
}
list = (List<T>)query.list();
session.flush();

return list;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
session.close();
}
}


HibernateSessionFactory.java


package com.rjht.dzqb.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory {

/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

/** Holds a single instance of Session */
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();

/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;

/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

/**
* Default constructor.
*/
private HibernateSessionFactory() {
}

}

...全文
615 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuguiyi100 2016-10-18
  • 打赏
  • 举报
回复
引用 4 楼 xuguiyi100 的回复:
源头: 有人遇到这种情况吗?
其它没有发现问题
xuguiyi100 2016-10-18
  • 打赏
  • 举报
回复
引用 6 楼 shijing266 的回复:
我之前也遇到过,就是这个list查询线程用完之后都是休眠状态,而不是真正的Close, 时间一长,容易内存溢出 不过不同的是,我手动加了close,后面这个问题就没出现过
我代码都加了,closed了还是一样的。
  • 打赏
  • 举报
回复
我之前也遇到过,就是这个list查询线程用完之后都是休眠状态,而不是真正的Close, 时间一长,容易内存溢出 不过不同的是,我手动加了close,后面这个问题就没出现过
wangzhuoyan 2016-09-22
  • 打赏
  • 举报
回复
你确定是这个query.list()接口不释放?还是你有其他的逻辑在里面,导致的
xuguiyi100 2016-09-21
  • 打赏
  • 举报
回复

源头:

有人遇到这种情况吗?
xuguiyi100 2016-09-18
  • 打赏
  • 举报
回复
有没遇到类似的问题,求解
xuguiyi100 2016-09-18
  • 打赏
  • 举报
回复
jprofiler找到内存没释放在query.list, 用这个 Session session = HibernateSessionFactory.currentSession();每次创建seesion如何理解? 主要是session.closed了内存就是不释放。
  • 打赏
  • 举报
回复
如何看出来不释放的?还有session对象已经提供了getcurrentsession方法,你这个方法是为每个请求新建了一个session对象。

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧