1.

Can you tell the difference between getCurrentSession and openSession methods?

Answer»

Both the METHODS are provided by the Session Factory. The main differences are given below:

getCurrentSession()openSession()
This method returns the session bound to the context.This method always opens a new session.
This session object scope belongs to the hibernate context and to make this work hibernate configuration file has to be modified by adding <property name = "hibernate.current_session_context_class"> thread </property>. If not added, then using the method WOULD throw an HibernateException.A new session object has to be created for each request in a multi-threaded environment. Hence, you NEED not configure any property to call this method.
This session object gets closed once the session factory is closed.It's the developer’s responsibility to close this object once all the database OPERATIONS are done.
In a SINGLE-threaded environment, this method is faster than openSession().In single threaded environment, it is slower than getCurrentSession()single-threadeda 

Apart from these two methods, there is another method openStatelessSession() and this method returns a stateless session object.



Discussion

No Comment Found