Question

I am starting with a JPA optimistic locking. I need to achieve the following scenario.

User place some Request for consideration. This request has a status property. While being just put it has a status 'new'. Then several services can observe this request and serve it by changing its status. (The status could be, for example, 'handling', *'put_back'*, 'finished'). So, I use optimistic locking to ensure that only one service is currently working with the request. So when some other service try to put the status of the request from 'new' to 'handling' it will give me OptimisticLockingException and I'll inform the service with something like this "Current status of the request has changed, please refresh your request list". That's sounds normal. So this is the situation when two services try to handle the same request at the same time.

But my special requirement is that in any period of time user can cancel the request by setting its status to 'canceled'. So I can get OptimisticLockingException exception in this case too. But I do not need it. Thus the request would not be canceled. Here I want to put status to 'canceled' no matter if it was changed by someone else. I think I should not ask the user to cancel his request again.

My question is: How can I handle this situation with EclipseLink 2.4.1 I am using for persistence.

Note: I am new to Optimistic Locking in JPA so I can miss something, please, correct if so.

Was it helpful?

Solution

I would handle this in your application. If you get a lock exception in your cancelRequest() operation, just catch the Exception and retry the transaction until it commits.

You could also use a JPQL update query, as it does not check the lock version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top