myeclise6.5 生成的 hibernate3的session是否需要手动关闭

houlc 2008-08-24 10:29:51
看了几篇关于session关闭的文章,有人说不用关闭,还是不能确定,初学hibernate3 不知道session是否需要手动关闭,没有用spring和其它的托管,web应用程序中
myeclipse生成的dao中的方法如下: 不知道是否需要在save方法里或查询的方法里加上
finally{
HibernateSessionFactory.closeSession();
}

如果不需要关闭的话,具体原因是什么呢?
public void save(Custinfo transientInstance) {
log.debug("saving WxGzjg instance");
try {
Transaction tran = null;
tran = (Transaction) getSession().beginTransaction();
getSession().save(transientInstance);
log.debug("save successful");
tran.commit();
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

我用myeclipse6.5,hibernate3.0生成的SessionFactory 类如下:
package com.soft.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.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;

private static final Log log = LogFactory.getLog(HibernateSessionFactory.class);

static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
log.info("Error Creating SessionFactory");
System.err.println("Error Creating SessionFactory");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}

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

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
log.info("Error Creating SessionFactory");
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
* 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();
}
}

/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}

}
...全文
231 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
如锋 2009-01-06
  • 打赏
  • 举报
回复

我也觉得应该关闭的,但奇怪MyEclipse生成的代码为什么没有?
tiantian7604 2008-09-28
  • 打赏
  • 举报
回复
我给你关闭了

public void save(Custinfo transientInstance) {
log.debug("saving WxGzjg instance");
try {
Transaction tran = null;
tran = (Transaction) getSession().beginTransaction();
getSession().save(transientInstance);
log.debug("save successful");
tran.commit();
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}finally
{
//在这里去关闭session 自己写一个关闭session的方法。在这里一调用就可以了
}
}
showgood119 2008-09-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hhay7758 的回复:]
不是吧
大家似乎对于这个hibernate的session的管理不是很熟悉啊
建议大家查看hibernate的sessionFactory类的源代码
谢谢
[/Quote]

偶很赞同
RyanDid 2008-09-26
  • 打赏
  • 举报
回复
关注
tz_dzg 2008-08-27
  • 打赏
  • 举报
回复
session的会话的作用是起到对象与数据库数据之间形成对应关系,关掉的话就是将对象与数据库之间脱离关心,不关的话关系依旧保留。
懒萝卜 2008-08-27
  • 打赏
  • 举报
回复
好像听说只要事务提交完,它会自动关闭的呢。
我也不知道是不是对的?
mellow_msb 2008-08-27
  • 打赏
  • 举报
回复
最好自己关闭,不过hibernate也好自动关闭的,我总感受还是主动些好
zgxzowen 2008-08-26
  • 打赏
  • 举报
回复
需要关闭
蛋蛋の忧伤 2008-08-25
  • 打赏
  • 举报
回复
不是吧
大家似乎对于这个hibernate的session的管理不是很熟悉啊
建议大家查看hibernate的sessionFactory类的源代码
谢谢
maodie007 2008-08-24
  • 打赏
  • 举报
回复
关闭只有好处 没有坏处
jiangpinhe 2008-08-24
  • 打赏
  • 举报
回复
最好关闭,记得老师做的没个程序都有session.close()来释放资源
digyso888 2008-08-24
  • 打赏
  • 举报
回复
关闭只有好处 没有坏处

67,513

社区成员

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

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