If you want to fail or commit on each iteration, the iteration loop needs to be outside of the transaction, the logic in the iteration needs to be wrapped in the transaction, e.g.
This is tricky because transactional proxies and annotations don't work when you call a method on the same object, they only work when you are entering the object.Code:while(true) { // start new transaction dao1.doStuff(); dao2.doStuff(); // end transaction }
What this means is that the method that does the work needs to be in another object than the method that runs the loop so you can apply the proxy to the work and not the loop.
Code:public class Looper { private Worker worker; public void setWorker(Worker worker) { this.worker = worker; } public void loop() { while (true) { try { worker.work(); } catch(Throwable t){// ignore or log} } } } public interface Worker { public void work(); } public class WorkerImpl implements Worker { private Dao dao1; private Dao dao2; // Setters elided @Transactional(propagation=Propagation.REQUIRES_NEW rollbackFor=DaoException.class) public void work() { dao1.doStuff(); dao2.doStuff(); } }


Reply With Quote
