0

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:

  1. 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();
        }
    }
}
  1. Use entity manager to manually commit the update delete column.Didnt workwithin @transactional boundaries

  2. 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

  • how do you get your instance of `helperServiceLayer` into `ServiceLayer`? If it's not injected, the `@Transactional` there will not have an effect, as no proxying for the methods will occur. – cyberbrain Apr 21 '22 at 17:13
  • And while looking for possible problems, with your code, I found this interesting article about `@Transactional` and how it works under the hood: https://www.marcobehler.com/guides/spring-transaction-management-transactional-in-depth – cyberbrain Apr 21 '22 at 17:20
  • @cyberbrain The helperServiceLayer is autowired. For berevity i have added only bits and parts of the code. – Locke Lamora Apr 21 '22 at 17:24
  • 1
    Does this answer your question? [Spring @Transactional - isolation, propagation](https://stackoverflow.com/questions/8490852/spring-transactional-isolation-propagation) – Chin Huang Apr 21 '22 at 17:29
  • Thanks everyone. Updated the question with edit. The links did help – Locke Lamora May 06 '22 at 08:36

0 Answers0