Pregunta

As per How to do optimistic locking in hibernate, we need to enable the optimistic locking with version element or version annotation in hibernate. I am clear till here.

I am not sure what is the purpose of Lock Mode Optimistic ? In what kind of scenario, developer should use it ?

No hay solución correcta

Otros consejos

to understand why you would want optimistic locking, you first need to understand what no locking and pessimistic locking mean. I'm no hibernate expert, so I'll just tell it to you without a focus on hibernate.

When 2 process/users update the same object then the one who updates it last will win. So you need to find a way to prevent this. One way to do this is pessimistic locking. Here, you will put a lock on the object at the moment you load it from database "select for update". Until your transaction is commited or rolled back, nobody else can "select for update" this object. now the problem is: When you load an entity via hibernate, you nowhere specify if you want to load it for read-only purpose or if you want to modify this object.

So here comes optimistic locking. This concept assumes optimistically that everything will go ok in most cases. When 2 processes/users update the same object, the second one will not win, but get an exception on commit.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top