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 ?
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.
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.
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.
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 ;-)
I think I found the answer in:
Session is Closed
I am yet to implement it
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