17

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 ?

animuson
  • 53,861
  • 28
  • 137
  • 147
Anand Sunderraman
  • 7,900
  • 31
  • 90
  • 150

8 Answers8

30

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 ;-)

Péter Török
  • 114,404
  • 31
  • 268
  • 329
8

I think I found the answer in:

Session is Closed

I am yet to implement it

Anand Sunderraman
  • 7,900
  • 31
  • 90
  • 150
7

You can use this annotation on your controller method:

@Transactional(readOnly = true)
ACV
  • 9,964
  • 5
  • 76
  • 81
1

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

Bizmarck
  • 2,663
  • 2
  • 33
  • 48
0

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();
            }
EngTiyeb
  • 38
  • 8
0

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();
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nicolas400
  • 563
  • 1
  • 7
  • 29
0

This may be the reason

session.close();
tx.commit();

The correct way would be:

tx.commit();
session.close();
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Mohammed Afsul
  • 692
  • 7
  • 8
0

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.

Community
  • 1
  • 1
Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152