pessimistlock exception when saving data in loop

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am getting pesimistlockexception when trying to persist multiple object of same time through JPA. Here is my code for reference

 @Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public Boolean changeDplListMappingByCustomWatchList(List<Integer> dplIds, Integer customWatchListId,
    ServiceRequestor customServiceRequestor) {

            for(Integer dplId : dplIds) {
                if(dplId != null) {
                    CustomWatchListDplMapping customWatchListDplMapping = new CustomWatchListDplMapping();
                    customWatchListDplMapping.setDplId(dplId);
                    customWatchListDplMapping.setWatchListId(customWatchListId);

                    this.create(customWatchListDplMapping);
                }
            }
    }
    catch(Exception e) {
        LOG.error("Exception occured while changing dpl mapping by custom watchList id", e);
    }

    return true;
}
 public void create(Model entity) {

    manager.persist(entity);
    manager.joinTransaction();
}

After first entity when it iterate through second one it throws an exception. If it has only one entity to save then it works well, but for more than one entity model it throws this exception.

profilepic.png
manpreet 2 years ago

by default pessimistic lock is for 1 second so please do the changes in the properties file it will help you to unlock and you will be able to save into database


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.