I wish to know the relation between transactions and locks.
To be more specific, how is Spring\'s @Transactional
related to Hibernate\'s LockMode. https://d
There is no direct relationship between @Transactional
and @LockMode
annotations.
@Transactional is used to mark the explicit boundaries of a RESOURCE_LOCAL or JTA transaction. The reason why you need it is that every database statement executes in a transactional context, and, if you don't set the transaction boundaries, you'll get one transaction per statement or auto-commit.
On the other hand, @LockModeType
is for setting explicit locking options. If you don't set it, the implicit locking mechanisms will be used:
So, @LockModeType
is for setting locking options explicitly, and you can have the following options:
The PESSIMISTIC
lock modes will always acquire a database lock on the table row that is associated with the locked entity.
There are also explicit optimistic lock strategies:
The OPTIMISTIC
lock modes are meant to give you a way of bumping up an entity version even if the entity hasn't changed in the currently running Persistence Context. This is a very useful mechanism when you need to coordinate multiple child entities using their parent entity version.
There are lots of examples in the links that I provided in this answer, so take your time, read them all, and you'll understand all these concepts in greater detail.