0 votes
in JAVA by
What is the difference Between openSession and getCurrentSession?

1 Answer

0 votes
by

Hibernate SessionFactory getCurrentSession() method returns the session bound to the context. But for this to work, we need to configure it in hibernate configuration file. 

Since this session object belongs to the hibernate context, we don’t need to close it. Once the session factory is closed, this session object gets closed.

<property name="hibernate.current_session_context_class">thread</property>

Hibernate SessionFactory openSession() method always opens a new session. We should close this session object once we are done with all the database operations. We should open a new session for each request in a multi-threaded environment.

There is another method openStatelessSession() that returns a stateless session, for more details with examples please read Hibernate openSession vs getCurrentSession.

Related questions

+1 vote
asked May 7, 2021 in JAVA by sharadyadav1986
0 votes
asked Sep 10, 2022 in Scala Constructs by Robin
...