区别b/w Hibernate的Sessionfactory.getCurrentSession()和SessionFactory.openSession()

时间:2022-09-11 14:41:01

I'm a little confused between the two. As per I know both returns hibernate session, SessionFactory.getCurrentSession() returns a contextual session based on the property <property name="current_session_context_class"> which is set in hibernate.cfg.xml Shouldn't we always go with this approach?

我对这两者有点困惑。正如我所知道的,它们都返回hibernate会话,SessionFactory.getCurrentSession()基于属性 (在hibernate.cfg中设置)返回一个上下文会话。我们不应该总是采用这种方法吗?

What additional value is added by SessionFactory.openSession()?

SessionFactory.openSession()增加了什么附加价值?

1 个解决方案

#1


37  

A session is opened whenever sf.getCurrentSession() is called for the first time. This creates a brand new session if one does not exist or uses an existing one if one already exists.

当第一次调用sf.getCurrentSession()时,会打开一个会话。这将创建一个全新的会话,如果您不存在或使用现有的会话,如果您已经存在。

In Tomcat this associates a session with a thread which is created using the underlying ThreadLocal object. But since Tomcat uses thread pooling it is entirely possible that a request may receive a thread with a session already associated with it, thus introducing the possibility of not even creating a brand new session. Another thing is that The Session that you obtained with sf.getCurrentSession() is flushed and closed automatically.

在Tomcat中,它将一个会话与一个使用底层ThreadLocal对象创建的线程关联起来。但是,由于Tomcat使用线程池,所以请求可能会接收到与它关联的会话的线程,从而引入了不创建一个全新会话的可能性。另一件事是您通过sf.getCurrentSession()获得的会话被刷新并自动关闭。

The method sf.openSession() on the other hand creates a new session but does not attempt to associate it with a thread. But remember sf.openSession() introduces another hitch in that it expects users to handle the closing and flushing of sessions themselves, instead of letting Hibernate do it automatically for us.

另一方面,方法sf.openSession()创建了一个新会话,但不尝试将其与线程关联。但请记住,sf.openSession()引入了另一个障碍,它希望用户自己处理会话的关闭和刷新,而不是让Hibernate自动为我们执行。

sf.getCurrentSession() is usually sufficient. sf.openSession() provides and facilitates a greater level of management of where the session is stored and managed. It's certainly an advanced option.

sf.getCurrentSession()通常是足够的。sf.openSession()提供并促进了对会话存储和管理的更高级别的管理。这当然是一个先进的选择。

#1


37  

A session is opened whenever sf.getCurrentSession() is called for the first time. This creates a brand new session if one does not exist or uses an existing one if one already exists.

当第一次调用sf.getCurrentSession()时,会打开一个会话。这将创建一个全新的会话,如果您不存在或使用现有的会话,如果您已经存在。

In Tomcat this associates a session with a thread which is created using the underlying ThreadLocal object. But since Tomcat uses thread pooling it is entirely possible that a request may receive a thread with a session already associated with it, thus introducing the possibility of not even creating a brand new session. Another thing is that The Session that you obtained with sf.getCurrentSession() is flushed and closed automatically.

在Tomcat中,它将一个会话与一个使用底层ThreadLocal对象创建的线程关联起来。但是,由于Tomcat使用线程池,所以请求可能会接收到与它关联的会话的线程,从而引入了不创建一个全新会话的可能性。另一件事是您通过sf.getCurrentSession()获得的会话被刷新并自动关闭。

The method sf.openSession() on the other hand creates a new session but does not attempt to associate it with a thread. But remember sf.openSession() introduces another hitch in that it expects users to handle the closing and flushing of sessions themselves, instead of letting Hibernate do it automatically for us.

另一方面,方法sf.openSession()创建了一个新会话,但不尝试将其与线程关联。但请记住,sf.openSession()引入了另一个障碍,它希望用户自己处理会话的关闭和刷新,而不是让Hibernate自动为我们执行。

sf.getCurrentSession() is usually sufficient. sf.openSession() provides and facilitates a greater level of management of where the session is stored and managed. It's certainly an advanced option.

sf.getCurrentSession()通常是足够的。sf.openSession()提供并促进了对会话存储和管理的更高级别的管理。这当然是一个先进的选择。