Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
@Transactional multiple update; commit every single query

I have 2 methods in my service

public void updateAll() {
   long[] ids = new long[] {1,2,3,4,5,6,7,8,9,10};
   for (long id : ids) {
        updateId(id);
    }
}

public updateId(long id) {
  repository.update(id);
}

Let's assume that after the 5th update I have an exception, I would like that the first 4 operations would be committed anyway.

I'm using the @Transactional annotation but if I put the annotation in both methods it doesn't work.

Do I need other parameter?? It might be propagation??

Could you show me how to set this methods?

Thank you!!

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You need a separate @Transactional on updateId with REQUIRES_NEW.

about 4 years ago · Santiago Trujillo Report

0

Just have:

@Transactional(propagation = Propagation.REQUIRES_NEW)
public updateId(long id) {
}

But, the important bit, call that method from another class. i.e. move your loop out of this class.

The transactional annotations only kick-in when that public method is called from the outside. Within the same class, calling one transactional method from another will still only use the transaction of the first method.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!