Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying hibernate entities from multiple threads

I've got a problem with understanding the details of thread safety in Hibernate. I know that Hibernate Sessions are not by themselves thread safe, so I'm not going to access them from more than one thread. However, I can't find any information on the thread safety of Hibernate entities. Can I modify them in multiple threads, while they remain attached to the Session which was used to load them?

I won't be using lazy loading (I know it would lead to concurrency problems). Entities will be properly synchronized and hibernate will access them via synchronized getters.

The scenario that I've envisioned:

  • Use a Hibernate Session to load entity A from database,
  • Subsequently, modify entity A from multiple threads, other than the thread in which the entity was loaded,
  • All the time entity A remains attached to the Session and is in persistent state,
  • Flush the Session so that modifications are synchronized with the database.
  • Entity A remains attached to the Session, so the cycle can repeat, with further modification and flushing.
like image 405
Rafal Avatar asked Sep 13 '15 11:09

Rafal


2 Answers

That depends on the nature of the modifications. If you modify an entity by creating, persisting and associating another entity with it in another thread, then it will not work, because the other entity instance will be considered detached in the first thread.

Taken aside the use cases like the one above, this should work in theory and only if you don't use bytecode instrumentation for the dirty check. Hibernate will just check whether the objects are dirty when they need to be flushed; basically, it does not care how you modified the objects.

However, this is not recommended.

Firstly, it may not be compatible with future versions of Hibernate/JPA (there may be more restrictions preventing concurrent access to the entities).

Secondly, the workaround is fairly simple: Just make the DTOs for the data that you want to modify concurrently, submit it for processing, and update the entities when the processing is done. This way the code is more clear, there are no unexpected Hibernate thread-related complaints and you keep the flexibility to use other useful features like lazy-loading.

like image 195
Dragan Bozanovic Avatar answered Oct 05 '22 23:10

Dragan Bozanovic


Hibernate entities are tightly integrated with the session and I'm pretty confident, that if the session isn't thread safe, the entities aren't either.

Even without considering the session, entities are just java beans which aren't thread safe. If you for example

Set a reference from anA to anB in one thread and change a property of anA in a second thread (or persist the entity) there is no guarantee, that the second thread will ever see the changes from the first.

So NO: Hibernate Entities are not thread safe.

like image 22
Jens Schauder Avatar answered Oct 06 '22 00:10

Jens Schauder