Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Session is closed

When I call the method session.begin transaction as follows:

//session factory is instantiated via a bean
Session session = this.getSessionFactory().getCurrentSession();
session.beginTransaction();

Then I get the following exception message

6:13:52,217 ERROR [STDERR] org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1319)

What could be the cause of this error ?

like image 942
Anand Sunderraman Avatar asked Mar 04 '10 10:03

Anand Sunderraman


People also ask

What does session close do in Hibernate?

A persistent instance has a representation in the database, an identifier value and is associated with a Session. detached − Once we close the Hibernate Session, the persistent instance will become a detached instance.

Do I need to close session in Hibernate?

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 multi-threaded environment.

What happens if Hibernate session is not closed?

When you don't close your Hibernate sessions and therefore do not release JDBC connections, you have what is typically called Connection leak. So, after a number of requests (depending on the size of your connection pool) the server will not be able to acquire a connection to respond your request.


2 Answers

Update: I guess that calling getCurrentSession() does not guarantee that the session is actually open. For the very first time, you should use

Session session = this.getSessionFactory().openSession();
session.beginTransaction();

instead. This suggestion is actually consistent with the page you found.

Earlier:

Based on the information available so far, we can conclude that the cause of the error is the session not being open ;-)

like image 111
Péter Török Avatar answered Oct 12 '22 17:10

Péter Török


I think I found the answer in:

Session is Closed

I am yet to implement it

like image 45
Anand Sunderraman Avatar answered Oct 12 '22 16:10

Anand Sunderraman