Session session = HibernateSessionFactory.getSession是否需要关闭呢

lylshr 2010-09-15 05:14:01
Session session = HibernateSessionFactory.getSession();

// Test 头字母要大写
Query query = session.createQuery("from Test");

Iterator iterate = query.iterate();
while (iterate.hasNext()) {
Test test = (Test) iterate.next();
test.print();
}
session.close();

谁能把原理给我解释一下呢....非常感谢,我就算会了但是原理也是没弄懂下次遇见也会忘记的.....
...全文
488 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
XYK13027451 2010-09-17
  • 打赏
  • 举报
回复
不一定要关闭,不过N久不关闭,服务器应该会死。
可以使用一个filter来关闭或者使用spring来管理
O溺水的鱼0 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sun19271 的回复:]
因为一个数据库的连接可以被很多线程使用,hibernate会维护一个连接池,里面有有限的与数据库的连接(可配置数量的)(这种连接是长期保持的,避免平凡的创立连接进程,提高效率),一个session如果不关闭,会占用一个连接,别的线程就没办法使用,如果关闭了,这个连接又可以被连接池分配给其它线程了。所以session如果不关闭,对于效率来说后果很严重。
[/Quote]
需要关闭的,对于hibernate,可以在filter中来打开和关闭session,可以google下 one session per request模式。
fable0115 2010-09-16
  • 打赏
  • 举报
回复
肯定需要关闭,这种机制就是为了避免系统负荷过大,否则容易导致系统瘫痪
a237428367 2010-09-16
  • 打赏
  • 举报
回复
集成spring就可以不用手动关闭啊
t150ckh 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wypbt1032 的回复:]
引用 8 楼 sun19271 的回复:
因为一个数据库的连接可以被很多线程使用,hibernate会维护一个连接池,里面有有限的与数据库的连接(可配置数量的)(这种连接是长期保持的,避免平凡的创立连接进程,提高效率),一个session如果不关闭,会占用一个连接,别的线程就没办法使用,如果关闭了,这个连接又可以被连接池分配给其它线程了。所以session如果不关闭,对于效率来说后果很严重。
……
[/Quote]
wypbt1032 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sun19271 的回复:]
因为一个数据库的连接可以被很多线程使用,hibernate会维护一个连接池,里面有有限的与数据库的连接(可配置数量的)(这种连接是长期保持的,避免平凡的创立连接进程,提高效率),一个session如果不关闭,会占用一个连接,别的线程就没办法使用,如果关闭了,这个连接又可以被连接池分配给其它线程了。所以session如果不关闭,对于效率来说后果很严重。
[/Quote]

正解,基本就是这样
sun19271 2010-09-16
  • 打赏
  • 举报
回复
因为一个数据库的连接可以被很多线程使用,hibernate会维护一个连接池,里面有有限的与数据库的连接(可配置数量的)(这种连接是长期保持的,避免平凡的创立连接进程,提高效率),一个session如果不关闭,会占用一个连接,别的线程就没办法使用,如果关闭了,这个连接又可以被连接池分配给其它线程了。所以session如果不关闭,对于效率来说后果很严重。
hackerster 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lylshr 的回复:]

session如果不关闭延迟加载还得可以查询,是这个好处吗
[/Quote]
session是要要关闭的,如果想保持延迟加载应该采用openSession in view
Zerolym 2010-09-16
  • 打赏
  • 举报
回复
肯定要关闭,几万、几十万的用户在操作,服务器受得了吗!
lylshr 2010-09-16
  • 打赏
  • 举报
回复
session如果不关闭延迟加载还得可以查询,是这个好处吗
lylshr 2010-09-16
  • 打赏
  • 举报
回复
能不能说详细点呢
WoodLikeWater 2010-09-15
  • 打赏
  • 举报
回复
当然需要关闭的,楼主去了解下数据库连接池的原理吧。
对你这个问题会有帮助的!
huangminyanghe 2010-09-15
  • 打赏
  • 举报
回复
这样会浪费资源的,打开一个session就会占有一些资源,不关闭不释放,你都没有什么别的用处了,为什么还要浪费呢。
tracyXiaoAi 2010-09-15
  • 打赏
  • 举报
回复
不关闭的话,它就一直保持连接状态呀,就会影响性能啊,而且session会过时
package hib; 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; static { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err .println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } private HibernateSessionFactory() { } /** * Returns the ThreadLocal Session instance. Lazy initialize * the SessionFactory 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) { 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; } }

67,512

社区成员

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

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