@Transactional 事务
https://blog.csdn.net/u010963948/article/details/79208328
https://ask.csdn.net/questions/718905
https://blog.csdn.net/zdyueguanyun/article/details/80236401
https://www.jianshu.com/p/eaeb0c99d109
https://my.oschina.net/happyBKs/blog/1624482 (很不错)
愚海加
/** * 加了事务需要回滚的 需要是捕捉到异常才会回滚 rollbackFor=Exception.class * @param data * @return * @throws Exception */ @RequestMapping("addProfess") @Transactional(rollbackFor=Exception.class) public ResultEntity addProfess(@RequestParam(name = "data", required = true) String data) throws Exception { try { UserTest userTest= parseObject(data,UserTest.class);//该方法稍微有点改动 boolean result = userTest.insert(); if(result) { System.out.println("应该添加去了");
// 如果报错通过try抛出,那就不会回滚 int i = 100 / 0;//错误 // List<Integer> artlist = new ArrayList<Integer>(); // artlist.add((Integer) 100); // List<UserBaseInfo> selectUserListById = userBaseInfoService.selectUserListById(artlist); return success(); }else { return error(ResultCode.SYS_ERROR); } } catch (Exception e) { // TODO Auto-generated catch block System.err.println("回滚需要抛错误! "); throw e; // e.printStackTrace(); // return error(ResultCode.SYS_ERROR); } }
我 主要因为try
@RequestMapping("addProfess") @Transactional(rollbackFor=Exception.class) public ResultEntity addProfess(@RequestParam(name = "data", required = true) String data) { boolean result=false; try { UserProfess userProfess= decodeByBase64(data,UserProfess.class); result = userProfess.insert(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return error(ResultCode.SYS_ERROR); } if(result) { // List<Integer> artlist = new ArrayList<Integer>(); // artlist.add((Integer) 100); return success(); }else { return error(ResultCode.SYS_ERROR); } }
import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableTransactionManagement @SpringBootApplication public class SmdemoApplication { public static void main(String[] args) {
@Transactional(readOnly = false, rollbackFor = Exception.class)
几乎所有的readOnly = false, 增,删,改事务类,都加了 rollbackFor = Exception.class
方法只要出现Exception时就会回滚,如果不加的话,Exception只有属于RuntimeException时才会回滚
回归或提交的情况
propagation 传播行为,就是控制回滚和提交的
被嵌套的事务失败了,不影响主事务,但如果没异常,还是要依赖主事务决定提交还是回滚
isolation 事务的隔离
不可从复读是更新数据引发的
幻读是增,删引发的
SERIALIZABLE串型话个人理解为,就是事务1开启,如果来了个事务2,那么事务2就得排队等待,等事务1执行完,才能执行事务2
锁和事务隔离不同,两者之间没有任何关系