Given this example code:
public class MyServiceImpl implements MyService {
@Transactional
public void myTransactionalMethod() {
List<Item> itemList = itemService.findItems();
for (Item anItem : itemList) {
try {
processItem(anItem);
catch (Exception e) {
// dont rollback here
// rollback just one item
}
}
}
@Transactional
public void processItem(Item anItem) {
anItem.setSomething(new Something);
anItem.applyBehaviour();
itemService.save(anItem);
}
}
Here is what I want to achieve:
- Only
processItem(anItem);
should rollback if exception occurs inside it. - If exception occurs,
myTransactionalMethod
should continue, that means the for-each should end. - If exception occurs inside
myTransactionalMethod
but not inprocessItem(anItem)
,myTransactionalMethod
should rollback completely.
Is there a solution that doesn't involve managing transactions manually (without annotations)?.
Edit: I was thinking of using @Transactional(PROPAGATION=REQUIRES_NEW)
, don't know if it will work within the same bean though.