Java @Transactional手动回滚异常

事件原有:

我需要读A库的数据写到B库中 中途需要切换数据源 但是我们又不是分布式的项目如果加上了@Transactional切换数据源就不会生效了 这时候就用到了事务的手动回滚

        try {
                //进行删除操作
                managementReadWriteDao.delUserHeroByUserId(writeIdUserId);
                managementReadWriteDao.delUserSoldierByUserId(writeIdUserId);
                managementReadWriteDao.delUserDataByUserId(writeIdUserId);
                managementReadWriteDao.delUserExtendsByUserId(writeIdUserId);

                //进行数据库写入操作
                managementReadWriteDao.insertUserHero(u_user_hero);
                managementReadWriteDao.insertUserSoldier(u_user_soldier);
                managementReadWriteDao.insertUserData(u_user_data);
                managementReadWriteDao.insertUserExtends(u_user_extends);

            } catch (Exception e) {
                //手动回滚异常
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                e.printStackTrace();
                return commonResultUtil.getCommonResultUtil(StaticData.codeType.other_error);

            }
            return commonResultUtil.getCommonResultUtil(StaticData.codeType.ok);

核心代码:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

posted @ 2021-08-13 17:31  难忘是想起  阅读(0)  评论(0)    收藏  举报  来源