我有一些关于在JSP Web应用程序中使用Hibernate的问题.
应该是什么价值hibernate.current_session_context_class
?
那么,应该使用以下哪个陈述?为什么?
Session s = HibernateUtil.getSessionFactory().openSession();
Session s = HibernateUtil.getSessionFactory().getCurrentSession()
Run Code Online (Sandbox Code Playgroud)最后,哪一个更好"每个网络应用一个会话"或"每个请求一个会话"?
gka*_*mal 137
如本论坛帖子所述,1和2是相关的.如果设置hibernate.current_session_context_class
为线程然后实现类似于打开会话的servlet过滤器 - 那么您可以使用其他任何地方访问该会话SessionFactory.getCurrentSession()
.
SessionFactory.openSession()
总是会打开一个新的会话,一旦完成操作,您必须关闭该会话.SessionFactory.getCurrentSession()
返回绑定到上下文的会话 - 您不需要关闭它.
如果您使用Spring或EJB来管理事务,则可以将它们配置为打开/关闭会话以及事务.
你永远one session per web app
不应该使用- session不是一个线程安全的对象 - 不能由多个线程共享.您应该始终使用"每个请求一个会话"或"每个事务一个会话"
Ram*_*wal 29
如果我们谈论SessionFactory.openSession()
如果我们谈论SessionFactory.getCurrentSession()
+----------------------+----------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| Parameter | openSession | getCurrentSession |
+----------------------+----------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| Session creation | Always open new session | It opens a new Session if not exists , else use same session which is in current hibernate context. |
+----------------------+----------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| Session close | Need to close the session object once all the database operations are done | No need to close the session. Once the session factory is closed, this session object is closed. |
+----------------------+----------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| Flush and close | Need to explicity flush and close session objects | No need to flush and close sessions , since it is automatically taken by hibernate internally. |
+----------------------+----------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| Performance | In single threaded environment , it is slower than getCurrentSession | In single threaded environment , it is faster than openSession |
+----------------------+----------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| Configuration | No need to configure any property to call this method | Need to configure additional property: |
| | | <property name=""hibernate.current_session_context_class"">thread</property> |
+----------------------+----------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
小智 6
openSession
:调用时SessionFactory.openSession
,它总是创建一个新Session
对象并将其提供给您。
您需要显式刷新并关闭这些会话对象。
由于会话对象不是线程安全的,因此您需要在多线程环境中为每个请求创建一个会话对象,在Web应用程序中也为每个请求创建一个会话。
getCurrentSession
:调用时SessionFactory.getCurrentSession
,它将为您提供处于休眠上下文中并由内部休眠管理的会话对象。它绑定到事务范围。
调用时SessionFactory.getCurrentSession
,它将创建一个新的(Session
如果不存在的话),否则使用当前休眠上下文中的相同会话。事务结束时,它将自动刷新并关闭会话,因此您无需在外部进行操作。
如果在单线程环境中使用休眠模式,则可以使用getCurrentSession
,因为与每次创建新会话相比,它的性能更快。
您需要在hibernate.cfg.xml中添加以下属性以使用getCurrentSession
方法:
<session-factory>
<!-- Put other elements here -->
<property name="hibernate.current_session_context_class">
thread
</property>
</session-factory>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
167500 次 |
最近记录: |