Issue
My scenario:
I am having a Parent and children entity. I want to read a child along with its parent, update it and save it back. I want the whole operation to happen with a lock.
Current flow of execution:
- Get PARENT and CHILD Entity
- Convert Entity to Domain objects (Following DDD design)
- Update parent and child
- Convert the CHILD Domain to CHILD Entity
- Save the CHILD Entity. (Parent gets auto-saved as child has a ManyToOne relationship with parent object)
To get parent entity a findbyId method in ParentRepositoy is called and to get child method a FindById method in ChildrenRepository is called.
To save child and parent, saveAll method in childrenRepository is called. (Parent gets auto-saved through ManyToOne relationship)
I want here a Persistent_write lock, which should cover from read to write. But I am not sure which object to lock or which repository method to lock.
Can anyone help me out here, or elaborate if I misunderstood anything here.
Solution
I declared the pessimistic_write lock in the parent's repo.findby method. As I only want the parent row to be locked and its working.
Code:
@Lock(LockModeType.PESSIMISTIC_WRITE)
List<...> findWithLockById(...);
Answered By - Nagulan S
Answer Checked By - Pedro (JavaFixing Volunteer)