@RestController
public Controller {
@Resource
private Service service;
@GetMapping("test")
public void test() {
service.service();
}
}
@Service
public Service {
@Resource
private DAO dao;
public void test1() {
//同一个类里无事务调有事务,事务不会生效
test2();
}
@Transactional
public void test2() {
dao.insert(record);
throw new RuntimeException();
}
}
以上代码执行后数据库记录插入成功,事务没回滚;因此,如果要让事务生效,需要将test2()方法放于另一个类中。