Under highly concurrent circumstances, the application trigger unique constraints while saving new data. So we have retry mechanism to fetch the data from DB again and if the data already present we will not save again.
But in the retry logic while trying to fetch data from postgresDB we are getting
ERROR :HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null id in io.cred.rj.demo.entity.Merchant entry (don’t flush the Session after an exception occurs)
Retry was working initially but now always getting assertion failure error
Example code:
public void cleanseMerchant(Merchant description, String mcc) {
try {
updateOrAddMerchant(description,mcc );
}catch(Exception e) {
//Retrying again
updateOrAddMerchant(description,mcc );
}
}
public void updateOrAddMerchant(Merchant description, String mcc) {
Merchant merchant=merchantDao.findByDescAndMcc(description,mcc);
if(merchant == null) {
Merchant merchant = new Merchant();
merchant.setMerchantDesc(description);
//set other fields
merchantDao.save(merchant);
}else {
updateMerchantInfo();
}
}