Let's assume we have a distributed system and Server A and B tries to update same data on the database at the same time. And we use Hibernate.
In this case, how will Hibernate behave? What kind of locking mechanism should we have?
The most common solution is to use optimistic locking. It comes down to adding a version
column to your entities, annotated with @Version
.
version
will be checked by Hibernate before each commit, and if the version in the database is newer than version on the entity being saved, exception will be thrown.