Hibernate openSession()vs getCurrentSession()

wan*_*nik 126 java hibernate

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

  1. 应该是什么价值hibernate.current_session_context_class

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

     Session s = HibernateUtil.getSessionFactory().openSession();
     Session s = HibernateUtil.getSessionFactory().getCurrentSession()
    
    Run Code Online (Sandbox Code Playgroud)
  3. 最后,哪一个更好"每个网络应用一个会话"或"每个请求一个会话"?

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不是一个线程安全的对象 - 不能由多个线程共享.您应该始终使用"每个请求一个会话"或"每个事务一个会话"

  • 如果您不希望会话绑定到任何上下文,则将使用OpenSession.有些时候你需要一个不同的会话情况 - 不是一个绑定到上下文中的其他(Hibernate的拦截器有,你不能使用原来的会话限制) - 在这些情况下,您会使用的openSession而不是currentSession.OpenSession创建一个必须显式关闭的新会话.例如,在DAO方法中,您将调用OpenSession - 使用会话并关闭它. (4认同)

Ram*_*wal 29

如果我们谈论SessionFactory.openSession()

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

如果我们谈论SessionFactory.getCurrentSession()

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


Job*_*ews 7

+----------------------+----------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| 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)