I am trying to use the declarative transaction management feature provide by Spring. I have set the spring configs and beans as described in reference documentation (i.e including AOP, tx namespaces and using the <tx:annotation-driven />
tag) and am using the @Transactional annotation on the method I want to be made transactional.
This is how the code looks like :
public interface Worker {
public workOnEvents(List<Events> eventsForACustomer);
}
public class WorkerImpl {
@Transactional
public workOnEvents(List<Events> eventsForACustomer) {
for(Event event : eventsForACustomer) {
// get DAO's based on event types at runtime,
// so we will have different DAO's acting within this loop
DAOFactory.getDAO(event.getType()).persistEvent(event);
}
}
}
Now, I want that if any of the DAOs in the loop above fails to handle the event, all changes made to the database by other DAOs that came in the loop before this one, should get rolled back.
So to test the rollback, I took a list of some events say (e1, e2, e3) which would result in picking up of DAOs say (d1, d2, d3) and then I intentionally throw a runtime exception in the persistEvent method of DAO d2. However, the result I get is that the program terminates without moving on to event e3 in the loop, not handling the exception thrown. Also, the data persisted by DAO d1 is not rolled back.
Please let me know what could I be doing wrong here?