0

I have simple code where I am adding into the tables and if one of the table entry fails to add the data into the table then delete the data from all the table.

Code :

@Component
public class Operation {

    ...

    public void execute(Long seqNumber) {

        try {
                addTable1(seqNumber);
                addTable2(seqNumber);
                addTable3(seqNumber);
            }
        } catch(Exception e) {
            deleteAllRecords(seqNumber);
        }
    }

    private void addTable1(Long seqNumber) throws Exception {
        Table1 data = new Table1();
        ...
        table1Repository.save(data);
    }

    private void addTable2(Long seqNumber) throws Exception {
        Table1 data = new Table1();
        ...
        table2Repository.save(data);
    }

    private void addTable3(Long seqNumber) throws Exception {
        Table1 data = new Table1();
        ...
        table3Repository.save(data);
    }

    private void deleteAllRecords(Long sequenceNumber) {
        table1Repository.deleteBySequenceNumber(sequenceNumber);
        table2Repository.deleteBySeqNum(sequenceNumber);
        table3Repository.deleteBySeqNum(sequenceNumber);
    }

    ...
}

So here, if the system fails to add the data into table3 then data from table1 and table2 also should be removed. However, I get the following error when the code goes to catch block and call deleteAllRecords(...):

org.springframework.dao.InvalidDataAccessApiUsageException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call

Questions :

  1. Am I doing anything wrong?

  2. How can I revert/delete the changes from different tables when I get an exception?

Any help would be appreciated.

Developer
  • 493
  • 2
  • 8
  • 19
  • 1. Yes. 2. You can't. The database will do that automatically for you as soon as you enclose all the insertions inside a **transaction**. This is a fundamental concept. I strongly advise you to google the term (see https://en.wikipedia.org/wiki/Database_transaction, https://en.wikipedia.org/wiki/ACID for example), and to read the Spring documentation about transactions, especially the part about declarative transactions using the Transactional annotation. – JB Nizet Sep 03 '19 at 18:54
  • I understand I cant revert the changes. But, it is not even letting me delete the line and throwing the exception. – Developer Sep 03 '19 at 18:55
  • Again, you shouldn't try to delete anything. Just let the database do its job, and rollback the current transaction. It takes more than 1 minute to learn what a transaction and its principles are. It takes even more to learn what Spring offers to deal with declaative transactions. Take the needed time. It's important. They are to persistence what an addition is to mathematics. You need to understand that concept. – JB Nizet Sep 03 '19 at 18:56
  • Again, by reading the Spring documentation and the articles I linked to that explain what a transaction is. – JB Nizet Sep 03 '19 at 18:59
  • @JBNizet thank you very much. "declarative transactions" documentation helped me. – Developer Sep 03 '19 at 19:27

0 Answers0