@Async異步不影響事務提交@Transaction


測試代碼

@Service
public class TestServiceImpl implements ITestService {
    @Resource
    private IStudentMapper studentMapper;
    @Autowired
    private IAsyncService asyncService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveUser(Student student) {
        studentMapper.insert(student);
        asyncService.test(student);
    }
}
@Service
public class AsyncServiceImpl implements IAsyncService {
    @Async("executor")
    @Override
    public void test(Student student) {
        System.out.println(1/0);
    }
}

測試類:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = StartBootMybatisApplication.class)
public class Test1 {
    @Autowired
    private ITestService testService;
    @Test
    public void test1(){
        Student student=new Student();
        student.setSno("112");
        student.setSname("小明");
        student.setSsex("男");
        student.setSclass("95032");
        student.setSbirthday(new Date());
        testService.saveUser(student);
    }
}

日志:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM