2

In a computation involving an @Entity instance, one thread A creates a new thread B. Thread A holds a reference to the entity and passes this reference over to thread B.

Thread A takes care of serving a single HTTP request, finding the entity inside a repository, and sending an HTTP response.
Thread B takes care of some long-running computation involving that entity.
Q: Why did I choose two threads? A: I don't want a long-running HTTP request-response cycle; instead I want to have a fast HTTP request-response cycle that quickly responses "Computation started" to my user.

Accessing the entity from thread A works flawlessy.
But accessing the entity from thread B doesn't work: the moment I try to access an entity relationship (which should lazily fetch data from Hibernate), I get the exception org.hibernate.LazyInitializationException: could not initialize proxy - no Session.

Is a JPA/Hibernate session scoped to a single thread - namely the thread creating that session (thread A in my case, as this is the thread querying my repository for the entity)? If this is the case, how can I make several threads share a Hibernate session so that I can work with the same entity from several threads?

Changing my entity's relashionship behavior to "eager loading" is not a viable solution. I'm also looking for a solution that is non-intrusive to my domain classes / entities (e.g. I don't want to use a PersistenceContext or EntityManager inside my domain classes to re-attach detached entities.

Abdull
  • 26,371
  • 26
  • 130
  • 172
  • 1
    From the hibernate docs "It [session] is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own". You need to detach the object and reattch it to the other thread's session. – Boris the Spider Feb 16 '13 at 16:14
  • @bmorris591 So does this mean it is basically not possible/allowed to work on a JPA entity concurrently from two or more threads? – Abdull Feb 16 '13 at 16:17
  • Not an attached one. On a detached one all the caveats associated with threading/locking apply. – Boris the Spider Feb 16 '13 at 16:18
  • Now the following question arises: how can I tell "Spring"/"Spring Data JPA" to reattach my entity? – Abdull Feb 16 '13 at 16:23
  • Take a look at [this](http://stackoverflow.com/questions/912659/what-is-the-proper-way-to-re-attach-detached-objects-in-hibernate). – Boris the Spider Feb 16 '13 at 16:28

1 Answers1

1

According to the Javadocs for the session a session is not intended to be used by multiple threads.
You need to detach the object from the session in thread A using evict before you work on the detached enitity concurrently. Bear in mind that that will throw org.hibernate.LazyInitializationException if there are and lazy properties that you need that haven't yet been read from the database.
When you're done you then need to call merge in order to reattach the entity to a session and then save it or whatever.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166