Spring transaction, update has an exception rollback problem in the loop?

in a transactional method, by reading a collection of table records and cycling the values of update or save fields, the administrative account is updated every time an ordinary account is updated.
so re-query the system account

.
@Transactional(rollbackFor = Exception.class)
public int test4()  {
    int  r= 0;
    for (int i = 0; i<10; iPP) {
        try {
            //
            SystemAccout systemAccout = gaodeMapper.getSysAccout(1);

            //
            Gaode gao = new Gaode();
            gao.setId(i);
            gao.setAddrName("testException-save-one " + i);
            gao.setPoint(systemAccout.getPoint());
            gao.setBal(systemAccout.getBal());
            r = gaodeMapper.updateUser(gao);
            if(r > 0) {
                //,update  save,
                if(i == 5) {
                    throw new RuntimeException();
                }
                //
                SystemAccout sys = new SystemAccout();
                sys.setId(1);
                sys.setPoint(systemAccout.getPoint() - ThreadLocalRandom.current().nextInt(10, 100));
                gaodeMapper.updateSystemAccoutn(sys);

            }
        }catch (Exception e) {
            throw e;
        }
    }
    return r;
}

how do teachers deal with this situation in the project?
is there any good alternative to this method of reading to a collection and then traversing update or save?

Mar.23,2021

can refer to the propagation property of the spring transaction to solve this problem. A new method is called within the for loop to process this data, and the method is marked @ Transactional (propagation = Propagation.REQUIRES_NEW) to start a new transaction.

Menu