如何hibernate sessionfactory管理会话? [英] how does hibernate sessionfactory manage session?

查看:145
本文介绍了如何hibernate sessionfactory管理会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部!我刚刚得到了Hibernate Session和Connection之间的关系。
但是现在,我得到另一个问题:hibernate sessionfactory如何管理会话?
在以下代码段中:DAO类的save()方法:

all! I have just got the relationship between Hibernate Session and Connection. But now, I get another question: how does hibernate sessionfactory manage session? In the following code segment: save() method of a DAO class:

Session session = sessionFactory.openSession();
   Transaction tx=null;
   tx=session.beginTransaction(); 
   session.save(transientInstance);
   session.flush();
   tx.commit();



< ,它会创建一个新的会话附加到当前线程(通过ThreadLocal),这个会话也附加到一个JDBC连接,
但是,正如你所看到的,我们不需要关闭会话(session .close()),既不是连接。
那么,什么是Hibernate会话的生命周期,在什么情况下它会被关闭?自动?

感谢高级。

When we call sessionFactory.openSession() , it will create a new session attached to the current thread (through the ThreadLocal), this session is also attached to a JDBC connection, But, as you can see, we don't need to close the session (session.close()), neither the connection. So, what is the lifecycle of a Hibernate session, in what circumstances it will be closed? automatically?
Thanks in advanced.

推荐答案

我推荐 getCurrentSession 方法,因为只有这种方法确保会话从hibernate中关闭的可能性

I recommend the getCurrentSession method because only with this method you have the possibility to be sure that the session will be closed from hibernate

配置J2EE当前会话

如果使用 openSession 方法,您必须自己关闭会话。
在我开始使用hibernate之后,我认为这并不重要我使用哪个方法,因为所有会话将自动关闭...我错了。
我从hibernate中发现了SessionStatistics
SessionStatistics
,打开的会话已经打开并且从未关闭。


在我改变所有调用 getCurrentSession impl 。每次请求模式打开的会话将在工作后关闭。

If you use the openSession method, you must close the sessions by yourself. After i begin to work with hibernate i thought it does'n matter which method I use because all session will be closed automatically... i was wrong. I had discovered with the SessionStatistics from hibernate SessionStatistics that the open session was already opened and never closed.

After i changed all calls to getCurrentSession and impl. the Session-per-request pattern opened session will be closed after work.

交易基础

这篇关于如何hibernate sessionfactory管理会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆