繁体   English   中英

休眠 openSession() 与 getCurrentSession()

[英]Hibernate openSession() vs getCurrentSession()

我有一些关于在 JSP Web 应用程序中使用 Hibernate 的问题。

  1. hibernate.current_session_context_class的值应该是多少?

  2. 那么,应该使用以下哪个语句? 为什么?

     Session s = HibernateUtil.getSessionFactory().openSession(); Session s = HibernateUtil.getSessionFactory().getCurrentSession()
  3. 最后,“每个 Web 应用程序一个会话”或“每个请求一个会话”哪个更好?

正如本论坛帖子中所解释,1 和 2 是相关的。 如果您将hibernate.current_session_context_class设置为线程,然后实现类似于打开会话的 servlet 过滤器的功能 - 那么您可以使用SessionFactory.getCurrentSession()在其他任何地方访问该会话。

SessionFactory.openSession()始终打开一个新会话,一旦您完成操作,您必须关闭该会话。 SessionFactory.getCurrentSession()返回绑定到上下文的会话 - 您不需要关闭它。

如果您使用 Spring 或 EJB 来管理事务,您可以将它们配置为与事务一起打开/关闭会话。

您永远不应该为one session per web app使用one session per web app - 会话不是线程安全对象 - 不能由多个线程共享。 您应该始终使用“每个请求一个会话”或“每个事务一个会话”

如果我们谈论 SessionFactory.openSession()

  • 它总是创建一个新的 Session 对象。
  • 您需要明确刷新和关闭会话对象。
  • 在单线程环境中,它比 getCurrentSession() 慢。
  • 您无需配置任何属性即可调用此方法。

如果我们谈论 SessionFactory.getCurrentSession()

  • 如果不存在,它会创建一个新会话,否则使用当前休眠上下文中的相同会话。
  • 您不需要刷新和关闭会话对象,它会在内部由 Hibernate 自动处理。
  • 在单线程环境中它比 openSession() 快。
  • 您需要配置额外的属性。 “hibernate.current_session_context_class”调用getCurrentSession()方法,否则会抛出异常。

openSession :当您调用SessionFactory.openSession ,它总是会创建一个新的Session对象并将其提供给您。

您需要显式刷新和关闭这些会话对象。

由于会话对象不是线程安全的,因此您需要在多线程环境中为每个请求创建一个会话对象,在 Web 应用程序中也需要为每个请求创建一个会话。

getCurrentSession :当您调用SessionFactory.getCurrentSession ,它将为您提供处于休眠上下文并由休眠内部管理的会话对象。 它绑定到事务范围。

当您调用SessionFactory.getCurrentSession ,如果它不存在,它会创建一个新Session ,否则使用当前休眠上下文中的相同会话。 它会在事务结束时自动刷新和关闭会话,因此您无需在外部执行此操作。

如果您在单线程环境中使用 hibernate,则可以使用getCurrentSession ,因为与每次创建新会话相比,它的性能更快。

您需要将以下属性添加到hibernate.cfg.xml以使用getCurrentSession方法:

<session-factory>
    <!--  Put other elements here -->
    <property name="hibernate.current_session_context_class">
          thread
    </property>
</session-factory>
+----------------------+----------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| 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>                       |
+----------------------+----------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+

SessionFactory:“每个应用程序每个数据库一个会话工厂”(例如,如果您在我们的应用程序中使用 3 个数据库,则需要为每个数据库创建 sessionFactory 对象,总共需要创建 3 个会话工厂。否则如果您只有一个数据库,则创建一个会话工厂足够 )。

会话:“一个请求-响应周期的一个会话”。 您可以在请求到来时打开会话,并在请求过程完成后关闭会话。 注意:-不要为 Web 应用程序使用一个会话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM