0
@Transactional
    public void save(String myIds) 
    {
            synchronized (this) 
            {
                List<mydata> data = getDataToSaveOrUpdate(myIds);//Returns the new dataList and updates old data
                repository.saveAll(data);
                logger.info("request processed");
            }
        logger.debug("exiting the method");
    }

When this method is called in two separate request from postman what happens synchronized block gets completed but inserts are going and second request start reading from the data base .So second thread founds same data to insert in both the request and give unique key violation error how should we resolve this problem in springboot data jpa hibernate

Sunil Kumar
  • 1,349
  • 3
  • 14
  • 25
  • 1
    Adding that synchronize block would come short with processes running on multiple JVMs. Seems like the method `getDataToSaveOrUpdate` should do something like https://stackoverflow.com/questions/5022812/find-or-insert-based-on-unique-key-with-hibernate – stackguy May 29 '20 at 19:09
  • ti is springboot application is running on single jvm only two requests are coming from client that is postman – Sunil Kumar May 30 '20 at 10:24

0 Answers0