Breako Breako
Breako Breako

Reputation: 6451

Can a StaleStateObjectException ever be thrown from a single thread?

We are using Grails and Hibernate. We are getting an org.hibernate.StaleObjectStateException in our logs. I see this can happen because of optimistic lock failure. Fine. However, I want to ensure this can never happen from multiple updates from the same thread. I mean never ever. Is there any corner case where there is multiple updates to the same hibernate entity but all from the same thread that can cause this exception?

Thanks,

Upvotes: 0

Views: 159

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328556

I didn't try but I would expect this error from this code:

 Foo foo1 = session.get(Foo.class, 1);
 Foo foo2 = session.get(Foo.class, 1); // same ID

 foo1.setBar(1);
 foo2.setBar(2);

 session.saveOrUpdate(foo1);
 session.saveOrUpdate(foo2); // Should throw an error

This can happen, for example, when you have complex helper objects that work on the same entities.

Upvotes: 1

Related Questions