Hibernate SessionFactory openSession() method always opens a new session. We should close this session object once we are done with all the database operations.
Hibernate SessionFactory provides three methods through which we can get Session object – getCurrentSession() , openSession() and openStatelessSession() .
SessionFactory is a factory class for Session objects. It is available for the whole application while a Session is only available for particular transaction. Session is short-lived while SessionFactory objects are long-lived. SessionFactory provides a second level cache and Session provides a first level cache.
After each operation, closeSession() will close the session and set the threadLocal object to null. Finally call the closeSessionFactory() . Show activity on this post. A session is opened whenever getCurrentSession() is called for the first time and closed when the transaction ends.
As explained in this forum post, 1 and 2 are related. If you set hibernate.current_session_context_class
to thread and then implement something like a servlet filter that opens the session - then you can access that session anywhere else by using the SessionFactory.getCurrentSession()
.
SessionFactory.openSession()
always opens a new session that you have to close once you are done with the operations. SessionFactory.getCurrentSession()
returns a session bound to a context - you don't need to close this.
If you are using Spring or EJBs to manage transactions you can configure them to open / close sessions along with the transactions.
You should never use one session per web app
- session is not a thread safe object - cannot be shared by multiple threads. You should always use "one session per request" or "one session per transaction"
If we talk about SessionFactory.openSession()
And If we talk about SessionFactory.getCurrentSession()
openSession
: When you call SessionFactory.openSession
, it always creates a new Session
object and give it to you.
You need to explicitly flush and close these session objects.
As session objects are not thread safe, you need to create one session object per request in multi-threaded environment and one session per request in web applications too.
getCurrentSession
: When you call SessionFactory.getCurrentSession
, it will provide you session object which is in hibernate context and managed by hibernate internally. It is bound to transaction scope.
When you call SessionFactory.getCurrentSession
, it creates a new Session
if it does not exist, otherwise use same session which is in current hibernate context. It automatically flushes and closes session when transaction ends, so you do not need to do it externally.
If you are using hibernate in single-threaded environment , you can use getCurrentSession
, as it is faster in performance as compared to creating a new session each time.
You need to add following property to hibernate.cfg.xml to use getCurrentSession
method:
<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> |
+----------------------+----------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With