I have a process that iterates through a list of events and saves them to a table. If a particular event throws an exception, I need to be able to rollback that event's transactions with the database without effecting the flow through the other events.
To achieve this, I have the following setup:
public class EventService
{
public void processEvents()
{
List<Event> events = getEvents();
foreach(Event event : events)
{
try
{
processEvent(event);
}
catch(Exception e)
{
// log the exception and continue processing additional events
}
}
}
@Transactional
public void processEvent(Event event)
{
// Process event and insert rows into database
// Some event will throw a runtime exception
}
}
However, this is not rolling back the event if there is an exception thrown.
Is there a way to achieve what I'm trying to do here?
If you call a method within the same class, Spring AOP does not have a chance to intercept the method. Therefore the @Transactional
annotation is ignored. Trying moving the processEvent
method to another class that is Spring injected.