//Given two beans. An exception occurred during the third iteration in method m1. How many records will be in the database? public class BeanA() { private BeanB b; @Transactional public void m1(List<Integer> list) { for (Integer i : list) { b.m2(i); } } } public class BeanB() { @Transactional public void m2(Integer i) { // Add record to table } }
sobes.tech AI
Answer from AI
In this example, the method m1 is annotated with @Transactional, and it calls method m2, which is also @Transactional.
By default, in Spring, nested transactions are not created; instead, the current transaction is used (Propagation.REQUIRED). This means that the call to m2 occurs within the same transaction as m1.
If an exception occurs on the third iteration in m1, the entire transaction is rolled back, including all calls to m2 made before the error.
As a result, no records will be saved in the database because the transaction was not committed but rolled back.
If m2 had a different transaction propagation policy (for example, REQUIRES_NEW), each insert would be committed separately, and records would remain in the database until the error. But this is not visible in the current code.