kingkong
kingkong

Reputation: 1547

How to reopen hibernate session after session was closed?

Is there is any session/hibernate function to reconnect/reopen

LOG: SEVERE: No operations allowed after connection closed.

Upvotes: 1

Views: 7863

Answers (3)

Lee Chee Kiam
Lee Chee Kiam

Reputation: 12058

Here is code example how to ensure the session is always valid. Line 2 must reassign back the session as openSession returns a new session. Just session.getSessionFactory().openSession() is not enough.

if (!session.isOpen()) {
        session = session.getSessionFactory().openSession();
        session.beginTransaction();
}
// operate your session 
Criteria criteria = session.createCriteria(clazz);

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

Hibernate Docs about Seesion says

The lifecycle of a Session is bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)

Once your transaction committed,Session wont available. You have to create a new one.

Upvotes: 0

sharakan
sharakan

Reputation: 6901

No. You should create/open a new Session.

Upvotes: 3

Related Questions