Home > Mobile >  Is it possible to acquire a lock while modifying a child & parent entity?
Is it possible to acquire a lock while modifying a child & parent entity?

Time:10-29

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:

  1. Get PARENT and CHILD Entity
  2. Convert Entity to Domain objects (Following DDD design)
  3. Update parent and child
  4. Convert the CHILD Domain to CHILD Entity
  5. 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.

CodePudding user response:

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(...);
  • Related