Two Spring boot (spring data jpa) Application A1 and Aplication A2 both read/write to a table named T1.
T1 -> ID, data1, isDeleted columns
My requirement is A1 will be updating column isDeleted to true and committing. In the same transaction/method then subsequently A1 will delete all the records which as the isDeleted column updated to true. It is important that the updation of isDeleted column e reflected in DB, because A2 is writing data continuously in the same table for the records which have isDeleted false.
Things tried so far:
- Application A1 (didnt work. Causes PessimisticLockFailureException at times_:
class ServiceLayer{
@Transactional
public void updateAndDeleteRecords(
helperServiceLayer.updateIsDeletedColumn(deletedRows);
repo.deleteAll(deletedRows);
}
}
class HelperServiceLayer{
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateIsDeletedColumn(List<TableRows> deletedRows) {
try{
repo.saveAll(deletedRows);
}
catch (Exception e){
e.printStackTrace();
}
}
}
Use entity manager to manually commit the update delete column.Didnt workwithin @transactional boundaries
Used TrasactionalTemplate but ran into issues
4. What baffles me is that a simple code below worked
class ServiceLayer{
repo.saveAll(deletedRows);
repo.deleteAll(deletedRows);
}
My understanding was unless the transactional method in A1 ends and the changes are committed, the changes will not be visible to A2. But what i found out that above code in section 4 works as A2 doesnt pick up data while A1 method is in progress. I need to understand what I am missing. Or is there a clean way of doing this.
I cannot change the design. All i can do is update the column isDeleted to true, persist it, subsequently delete the same rows in the same method. All this while A2 will be trying to read data whose isDeleted value is false
EDIT: the system was behaving wierdly because of concurret issues. The premise of this question was wrong. Because of less number of records solution 4 gave the illusion that it worked. Took a completely different approach of marking a row as deleted instead of actually deleting it and filter based on that. Thanks everone