Reputation: 83
I'm receiving a StaleObjectStateException while I'm trying to lock a domain object inside of a transactional service (grails 2.3.8):
@Transactional
class AnalyticsService {
boolean newStreamView(Long streamId) {
Stream stream = Stream.lock(streamId) // The exception is launched here
Of course this only occurs if there are many concurrent calls to this service. As I can see, hibernate is trying to lock with ID and version parameters:
select id from stream where id =442 and version =305 for update
and this fails. If I disable the optimistic locking (version: false) inside of that domain class, everything works fine (hibernate just use the id for locking the row).
As posted in Marc Palmer's Blog seems that:
The only foolproof way to avoid StaleObjectException(s) while keeping optimistic locking ON, is to do all GORM work in transactions, and always load objects with Domain.lock(id). When using dynamic finders or criteria you will need to specify the “lock” option to have results pre-locked
He says that we should keep optimistic locking ON.
Is there any safe approach of avoiding StaleObjectStateException with lock and optimistic locking ON ?
If I disable optimistic locking (version: false) what other problems can I expect. I'm concerned about this because this domain class is updated from other services ?
Thanks in advance.
Upvotes: 5
Views: 944
Reputation: 61
We fixed 100% of our StaleStateException/OptimisticLocking issues by:
Upvotes: 3