Series 30, Spring Affairs (Practical) – NESTED

1. Demonstrate Spring’s communication behavior (NESTED)

1.1, StockServiceImplNESTED

/**
 * @Author: A leaf of duckweed returns to the sea
 * @Date: 2023/10/30 15:43
 * @Description: Demonstrates the propagation behavior of NESTED (the outer layer affects the inner layer, and the inner layer does not affect the outer layer)
 * NESTED is a nested transaction, as a nested transaction of an external transaction
 * There is no external transaction: open a new transaction
 * External existing affairs: integrated into external affairs
 */
@Service(value = "stockServiceImplNESTED")
public class StockServiceImplNESTED extends ServiceImpl<StockMapper, StockDO> implements StockService {

    @Resource
    private StockMapper stockMapper;

    @Resource
    private IntegralService integralServiceImplNESTED;

    /**
     * NESTED’s communication behavior
     * @param id
     * @param num
     * Scenario 1: There is an external transaction (with exception), and there is also an internal transaction (the propagation behavior is NESTED and no exception), but the exception is thrown externally
     * Expected results: Both external and internal transactions are rolled back
     * Actual result: both external and internal transactions are rolled back
     * Corresponding method: m1()
     *
     * Scenario 2: There is an external transaction (with an exception) and an internal transaction (the propagation behavior is NESTED and no exception), but the external exception is captured and handled by oneself.
     * Expected results: Both external affairs and internal affairs are committed
     * Actual result: Both external affairs and internal affairs are committed
     * Corresponding method: m2()
     *
     * Scenario 3: There is an external transaction (no exception) and an internal transaction (the propagation behavior is NESTED and there is an exception), but the exception is thrown internally, and the exception is handled externally after being caught.
     * Expected results: External transactions are submitted normally and internal transactions are rolled back
     * Actual results: External transactions are submitted normally and internal transactions are rolled back
     * Corresponding method: m3()
     *
     * Scenario 4: There is an external transaction (no exception) and an internal transaction (the propagation behavior is NESTED and there is an exception), but the exception is thrown internally, and the exception is thrown after the exception is captured externally.
     * Expected results: Both external and internal transactions will be rolled back
     * Actual result: both external and internal transactions will be rolled back
     * Corresponding method: m4()
     *
     *...
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void reduceStock(Long id, Integer num) {
        m4(id, num);
    }

    private void m4(Long id, Integer num) {
        System.out.println("==============>m4() of StockServiceImplNESTED was executed");
        try {
            // Reduce inventory
            StockDO dbStock = stockMapper.selectById(id);
            StockDO updateStock = new StockDO();
            BeanUtils.copyProperties(dbStock, updateStock);
            updateStock.setNum(dbStock.getNum() - num);
            stockMapper.updateById(updateStock);

            // Increase points
            IntegralDO updateIntegral = new IntegralDO();
            updateIntegral.setPreIntegral(0);
            updateIntegral.setCurrentIntegral(1000);
            updateIntegral.setUserId(1L);
            integralServiceImplNESTED.addIntegral(updateIntegral);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void m3(Long id, Integer num) {
        System.out.println("==============>m3() of StockServiceImplNESTED was executed");
        try {
            // Reduce inventory
            StockDO dbStock = stockMapper.selectById(id);
            StockDO updateStock = new StockDO();
            BeanUtils.copyProperties(dbStock, updateStock);
            updateStock.setNum(dbStock.getNum() - num);
            stockMapper.updateById(updateStock);

            // Increase points
            IntegralDO updateIntegral = new IntegralDO();
            updateIntegral.setPreIntegral(0);
            updateIntegral.setCurrentIntegral(1000);
            updateIntegral.setUserId(1L);
            integralServiceImplNESTED.addIntegral(updateIntegral);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void m2(Long id, Integer num) {
        System.out.println("==============>m2() of StockServiceImplNESTED was executed");
        try {
            // Reduce inventory
            StockDO dbStock = stockMapper.selectById(id);
            StockDO updateStock = new StockDO();
            BeanUtils.copyProperties(dbStock, updateStock);
            updateStock.setNum(dbStock.getNum() - num);
            stockMapper.updateById(updateStock);

            // Increase points
            IntegralDO updateIntegral = new IntegralDO();
            updateIntegral.setPreIntegral(0);
            updateIntegral.setCurrentIntegral(1000);
            updateIntegral.setUserId(1L);
            integralServiceImplNESTED.addIntegral(updateIntegral);

            int i = 10 / 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void m1(Long id, Integer num) {
        System.out.println("==============>m1() of StockServiceImplNESTED was executed");
        try {
            // Reduce inventory
            StockDO dbStock = stockMapper.selectById(id);
            StockDO updateStock = new StockDO();
            BeanUtils.copyProperties(dbStock, updateStock);
            updateStock.setNum(dbStock.getNum() - num);
            stockMapper.updateById(updateStock);

            // Increase points
            IntegralDO updateIntegral = new IntegralDO();
            updateIntegral.setPreIntegral(0);
            updateIntegral.setCurrentIntegral(1000);
            updateIntegral.setUserId(1L);
            integralServiceImplNESTED.addIntegral(updateIntegral);

            int i = 10 / 0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

1.2, IntegralServiceImplNESTED

/**
 * @Author: A leaf of duckweed returns to the sea
 * @Date: 2023/10/30 15:43
 * @Description: Demonstrates the propagation behavior of NESTED
 * There is no external transaction:
 * External affairs:
 */
@Service(value = "integralServiceImplNESTED")
public class IntegralServiceImplNESTED extends ServiceImpl<IntegralMapper, IntegralDO> implements IntegralService {

    @Resource
    private IntegralMapper integralMapper;

    @Transactional(propagation = Propagation.NESTED,rollbackFor = Exception.class)
    @Override
    public void addIntegral(IntegralDO updateIntegral) {
        m4(updateIntegral);
    }

    private void m4(IntegralDO updateIntegral) {
        try {
            System.out.println("==============>m4() of IntegralServiceImplNESTED was executed");
            integralMapper.insert(updateIntegral);
            int i = 10 / 0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void m3(IntegralDO updateIntegral) {
        try {
            System.out.println("==============>m3() of IntegralServiceImplNESTED was executed");
            integralMapper.insert(updateIntegral);
            int i = 10 / 0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void m2(IntegralDO updateIntegral) {
        System.out.println("==============>m2() of IntegralServiceImplNESTED was executed");
        integralMapper.insert(updateIntegral);
    }

    private void m1(IntegralDO updateIntegral) {
        System.out.println("==============>m1() of IntegralServiceImplNESTED was executed");
        integralMapper.insert(updateIntegral);
    }
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138781 people are learning the system