Reputation: 8138
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 ?
Upvotes: 17
Views: 85174
Reputation: 653
The following solution worked for me:
@SuppressWarnings("unchecked")
public List<Enfermedad> obtenerEnfermedadesOrderByProcal() throws HibernateException {
if (session==null)
session = HibernateUtil.getSession();
if (!session.isOpen())
session = HibernateUtil.getSession();
return session.createQuery("FROM Enfermedad AS e ORDER BY e.ordenPROCAL, e.id").list();
}
Upvotes: 0
Reputation: 38
To handle session is closed exception:
1-)In your hibernate.cfg file change the default hibernate.current_session_context_class
to managed
instead of thread
2-)Create two methods to open and close the session properly and use the before and after your query as sown below:
public static void renewSession() {
try {
session = HibernateUtil.getSessionFactory().openSession();
session.setFlushMode(FlushMode.MANUAL);
ManagedSessionContext.bind(session);
session.beginTransaction();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void closeSession()
{
try {
ManagedSessionContext.unbind(HibernateUtil.getSessionFactory());
session.flush();
session.getTransaction().commit();
session.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
Usage:
try {
renewSession();
String query = "from Table tb";
data = (List<TypeObject>)session.createQuery(query).list();
closeSession();
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 10561
You can use this annotation on your controller method:
@Transactional(readOnly = true)
Upvotes: 7
Reputation: 2688
Instead of closing the session completely, you can use session.disconnect()
, do some other work, then session.reconnect()
before doing other transactions.
session.close()
would be called at the very end of the request / business process.
In Hibernate 2.1.8 we have been using the one session per request model and it works well, avoiding session leaks, etc.
See here for some elaboration
Upvotes: 1
Reputation: 692
This may be the reason
session.close();
tx.commit();
The correct way would be:
tx.commit();
session.close();
Upvotes: 0
Reputation: 9237
I had the same Hibernate Session Closed Problem a year ago . MY QUESTION
but it was for a different reason, I put this here in case someone faces this problem and wants to know more about it.
Upvotes: 0
Reputation: 116266
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 ;-)
Upvotes: 30