简体   繁体   中英

When to close hibernate session?

I am using a dynamic web project. I open the session when the server starts. I use a single ton static class for this purpose and use the session everywhere around.

But I am not sure when to close the session. Do you know where could it be handled? What is the best practice?

There are two Object in Hibernate :

Short Answer : 
SessionFactory : Heavy Weight.
Session : light weight.

Long Answer :

SessionFactory is responsible for creating connection with database and managing other stuff, so creating sessionFactory is time consuming task. So I will say only one sessionFactory object should be created per Application, or in more better way one SeesionFactory per Database.

Now coming to your Session, it is light weight object, for each transaction you can create session object, when you are done with Transaction you can close session.

You should maintain your session as long as you want to be connected to your database.

But sessions are not a thread safe objects and cannot be shared by multiple threads. So you should use one session per request.

So bottomline is, if you're using a single threaded application, then it's better to use one session for your entire application. You can use SessionFactory.getCurrentSession() for this.

But if your application is shared across multiple threads, then you should always open new sessions using SessionFactory.openSession() . Although this is slower than the former, but it's thread safe.

Do you know where could it be handled

The best practice is to close them in finally block. However if you're using Java SE 7 and later, then you can handle them in try-with-resources as well.

Hibernate session should be closed when transaction is closed. One session per request is the best approach. Session is not thread safe

you can config hibernate to open and close session automatically and it is not required to close manually. if you want to open and close manually, open your session, execute your query and close session:

    Transaction tx = session.beginTransaction();
    try {
        for (int i = 0; i < mesages.size(); i++) {
            Message message = messages.get(i);
            session.save(message);
            if (i % 75 == 0) { 
                // flush a batch of inserts and release memory:
                session.flush();
                session.clear();
            }
        }
        tx.commit();
    }catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        tx.rollBack();
    }finally{
        session.close();
    }
}

above code question link : How to properly close and open a Hibernate session?

also you can search about Hibernate session view filter to handle open and close session automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM