Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring + Hibernate session management across multiple threads

I am building a system, where each request from a client side spawns multiple threads on server side. Each thread then is using one or more DAOs (some DAOs can be used by more than one thread at the time). All DAOs are injected (@Autowired) to my thread classes by Spring. Each DAO receives SessionFactory injected as well.

What would be proper way of managing Hibernate sessions across these multiple DAOs so I would not run into problems because of multithreaded environment (e.g. few DAOs from different threads are trying to use the same session at the same time)?

Would be enough that I specify hibernate.current_session_context_class=thread in Hibernate configuration and then everytime in DAO simply use SessionFactory.getCurrentSession() to do the work? Would it properly detect and create sessions per thread as needed?

like image 713
Laimoncijus Avatar asked Jan 04 '12 07:01

Laimoncijus


People also ask

Can Hibernate Session be shared between threads?

Sessions in Hibernate are not thread safe. This mean a Hibernate session can not be passed to a new thread, it also means that because objects from a session can be communicated from their owning session only and Hibernate-managed session objects can not be shared between threads.

Can multiple threads run simultaneously?

Within a process or program, we can run multiple threads concurrently to improve the performance. Threads, unlike heavyweight process, are lightweight and run inside a single process – they share the same address space, the resources allocated and the environment of that process.

Does spring boot use multiple threads?

Multithreading in spring boot is similar to multitasking, except that it allows numerous threads to run concurrently rather than processes. A thread in Java is a sequence of programmed instructions that are managed by the operating system's scheduler.

Can Session be reused in hibernate?

Spring opens a new Hibernate Session at the beginning of the request. These Sessions are not necessarily connected to the database. Every time the application needs a Session, it will reuse the already existing one.


1 Answers

Yes. It is enough.

When setting hibernate.current_session_context_class to thread , the session returned from SessionFactory.getCurrentSession() is from the ThreadLocal instance.

Every thread will have their own, independently ThreadLocal instance, so different threads will not access to the same hibernate session.

The behaviour of SessionFactory.getCurrentSession() is that: if it is called for the first time in the current thread, a new Session is opened and returned. If it is called again in the same thread, the same session will be returned.

As a result , you can get the same session to use in different DAO methods in the same transaction code by simply calling SessionFactory.getCurrentSession(). It prevents you from passing the Hibernate session through the DAO method 's input parameters in the case that you have to call many different DAO methods in the same transaction code.

like image 167
Ken Chan Avatar answered Sep 21 '22 19:09

Ken Chan