Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Hibernate's session thread safe?

Tags:

I need to know, whether the Hibernate's session is thread safe or not. But obvious a new session is attached to every thread for execution. But my question is if in one thread I have updated some value of an entity, so will that be reflected in other thread during same time execution?

My problem is when I fire update from two threads sequentially, the value is updated properly but when I fire the update almost altogether then it fails.

for eg. current stage of table.

  ID      NAME      MARKS
------- --------- --------
  1       John       54

I am trying to do follwing :

Student student = session.load(Student.class, 1);
student.setMarks(student.getMarks() + 1);
session.update(student);
session.close();

When I try to run the above code in loop say 10, then value of "marks" in table "student" is properly updated i.e. the value gets updated to 64 which is proper.

But when I try to run the same code in threaded environment, it gives bad results.

like image 213
M.J. Avatar asked Sep 23 '10 11:09

M.J.


People also ask

Why is Session not thread safe?

No, Session is not a thread-safe object, many threads can't access it simultaneously. In other words, you cannot share it between threads.

Why hibernate SessionFactory is thread safe?

Yes, the Internal state of SessionFactory is immutable, so it's thread-safe. Multiple threads can access it simultaneously to get Session instances. SessionFactory is the factory class used to get the Session objects.

Which one is a thread safe object in hibernate?

Session can be created in two ways in hibernate. Yes. It offers thread safety as it'll ensure that it'll create a session for each thread if session not exist. transaction and automatic session closing is attached to this.

Can I reuse the Session 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.


2 Answers

It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.

Even with this in mind, your behaviour might still not be what you expect, because transactions come into play. You will have to set a proper transaction isolation level. See the configuration guide, hibernate.connection.isolation property.

like image 85
Bozho Avatar answered Oct 07 '22 00:10

Bozho


Hibernate session and threads do not mix.

You should not use a session from multiple threads at once, and I recommend you only use a session from a single thread. DB session implementations are not even required to be theadsafe.

You also must consider what happens to the transactions when you start doing things in multiple threads. Transactions are tied to the current thread. This becomes quickly mindblowing and you enter areas where the implementers have not tested their products.

In the end life is too short to get lost in that swamp.

like image 40
Peter Tillemans Avatar answered Oct 07 '22 00:10

Peter Tillemans