Explanation of the combination of Transactional annotation and try catch

Code

@Service
public class SysNewsServiceImpl implements SysNewsService {

    private Logger logger = LoggerFactory.getLogger(SysNewsServiceImpl.class);

    @Autowired
    private SysNewsMapper sysNewsMapper;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addSysNews(SysNews sysNews) {

        SysNews sysNews1 = new SysNews( "2", "2", 2, 2, new Date());
        SysNews sysNews2 = new SysNews( "东风然后涂华军软件园ada法师法师发大事发生", "1", 1, 1, new Date());
        SysNews sysNews3 = new SysNews( "3", "3", 3, 3, new Date());
        SysNews sysNews4 = new SysNews( "4", "4", 4, 4, new Date());
        SysNews sysNews5 = new SysNews( "5", "5", 5, 5, new Date());

        // 东风然后涂华军软件园ada法师法师发大事发生

        List<SysNews> sysNewsList = new ArrayList<>();
        sysNewsList.add(sysNews1);
        sysNewsList.add(sysNews2);
        sysNewsList.add(sysNews3);
        sysNewsList.add(sysNews4);
        sysNewsList.add(sysNews5);

        for (SysNews news : sysNewsList) {
             sysNewsMapper.insert(news);
        }
    }
}

The role of transactional annotations
If during the process of adding data, one piece of data fails to be added, then all the data previously added to the database will be rolled back. The following data will not be added to the database to
ensure data security

Use with try catch

@Service
public class SysNewsServiceImpl implements SysNewsService {

    private Logger logger = LoggerFactory.getLogger(SysNewsServiceImpl.class);

    @Autowired
    private SysNewsMapper sysNewsMapper;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addSysNews(SysNews sysNews) {

        SysNews sysNews1 = new SysNews( "2", "2", 2, 2, new Date());
        SysNews sysNews2 = new SysNews( "东风然后涂华军软件园ada法师法师发大事发生", "1", 1, 1, new Date());
        SysNews sysNews3 = new SysNews( "3", "3", 3, 3, new Date());
        SysNews sysNews4 = new SysNews( "4", "4", 4, 4, new Date());
        SysNews sysNews5 = new SysNews( "5", "5", 5, 5, new Date());

        // 东风然后涂华军软件园ada法师法师发大事发生

        List<SysNews> sysNewsList = new ArrayList<>();
        sysNewsList.add(sysNews1);
        sysNewsList.add(sysNews2);
        sysNewsList.add(sysNews3);
        sysNewsList.add(sysNews4);
        sysNewsList.add(sysNews5);

        for (SysNews news : sysNewsList) {
            try {
                sysNewsMapper.insert(news);
            } catch (Exception e) {
                logger.info("增加消息出错",e);
            }
        }

    }
}

The role of try catch annotation
If during the process of adding data, one piece of data fails to be added, then the role of catch is to catch the exception and only let this piece of data not be added to the database. The following data or the data before this data can still be added to the database.
The purpose is to prevent business interruption. Because catch you have already caught the exception, so the business will not be interrupted.
The above does not add try catch, because there is an exception, the business will also be interrupted if it is not caught.

Persistence or non-persistence in this life is not terrible. What I am afraid of is walking on the road of persistence alone! ! !

Guess you like

Origin blog.csdn.net/taiguolaotu/article/details/112480910