关于@Transactional 的sao操作

在使用@Transactional  关键字的时候,总会有这样 或者 那有的问题。

一下做一些测试来验证问题

case1 无try catch、无嵌套,没有加 rollbackFor = Exception.class 事务正常回滚

@Transactional
	public int testSave()  {
    
    
		 User user = new User();
         user.setName("岳不群2");
         user.setAge(70);
         user.setEmail("[email protected]");
         userMapper.insert(user);
         int m = 1/0;
         return 1;
	}

case2 无try catch、无嵌套,加 rollbackFor = Exception.class 事务正常回滚

@Transactional(rollbackFor = Exception.class)
	public int testSave()  {
    
    
		 User user = new User();
         user.setName("岳不群2");
         user.setAge(70);
         user.setEmail("[email protected]");
         userMapper.insert(user);
         int m = 1/0;
         return 1;
	}

case3 没有加 rollbackFor = Exception.class 事务没有回滚

@Transactional
	public int testSave()  {
    
    
		 try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         int m = 1/0;
	         return 1;
		} catch (Exception e) {
    
    
			// TODO: handle exception
		}
		 return 0;
	}

case4 有try catch、无嵌套,加 rollbackFor = Exception.class 事务没有回滚

@Transactional(rollbackFor = Exception.class)
	public int testSave()  {
    
    
		 try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         int m = 1/0;
	         return 1;
		} catch (Exception e) {
    
    
			// TODO: handle exception
		}
		 return 0;
	}

case5 有try catch、无嵌套,加 rollbackFor = Exception.class ,抛出异常 。 事务正常回滚

@Transactional(rollbackFor = Exception.class)
	public int testSave() throws Exception  {
    
    
		 try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         int m = 1/0;
	         return 1;
		} catch (Exception e) {
    
    
			throw new Exception(e.getMessage());
		}
	}

case6 有try catch、无嵌套,加 rollbackFor = Exception.class ,抛出异常。方法调用,事务没有回滚

public int testSave() throws Exception {
    
    
		return testSave2();
	}
	@Transactional(rollbackFor = Exception.class)
	public int testSave2() throws Exception  {
    
    
		try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         int m = 1/0;
	         return 1;
		} catch (Exception e) {
    
    
			System.out.println(e.getMessage());
			throw new Exception(e.getMessage());
		}
	}

case7 事务没有回滚

@Transactional
	public int testSave() throws Exception {
    
    
		return testSave2();
	}
	@Transactional(rollbackFor = Exception.class)
	public int testSave2() throws Exception  {
    
    
		try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         int m = 1/0;
	         return 1;
		} catch (Exception e) {
    
    
			System.out.println(e.getMessage());
			throw new Exception(e.getMessage());
		}
	}

case8 事务回滚

@Transactional(rollbackFor = Exception.class)
	public int testSave() throws Exception {
    
    
		return testSave2();
	}
	public int testSave2() throws Exception  {
    
    
		try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         int m = 1/0;
	         return 1;
		} catch (Exception e) {
    
    
			System.out.println(e.getMessage());
			throw new Exception(e.getMessage());
		}
	}

case9 事务回滚

@Transactional(rollbackFor = Exception.class)
	public int testSave() throws Exception {
    
    
		return testSave2();
	}
	@Transactional(rollbackFor = Exception.class)
	public int testSave2() throws Exception  {
    
    
		try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         int m = 1/0;
	         return 1;
		} catch (Exception e) {
    
    
			System.out.println(e.getMessage());
			throw new Exception(e.getMessage());
		}
	}

case10 事务未回滚

@Transactional
	public int testSave() throws Exception {
    
    
		return testSave2();
	}
	@Transactional(rollbackFor = Exception.class)
	public int testSave2() throws Exception  {
    
    
		try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         int m = 1/0;
	         return 1;
		} catch (Exception e) {
    
    
			System.out.println(e.getMessage());
			throw new Exception(e.getMessage());
		}
	}

case11 事务回滚

@Transactional(rollbackFor = Exception.class)
	public int testSave() throws Exception {
    
    
		return testSave2();
	}
	@Transactional
	public int testSave2() throws Exception  {
    
    
		try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         int m = 1/0;
	         return 1;
		} catch (Exception e) {
    
    
			System.out.println(e.getMessage());
			throw new Exception(e.getMessage());
		}
	}

case12 事务未回滚

@Transactional(rollbackFor = Exception.class)
	public int testSave() throws Exception {
    
    
		return testSave2();
	}
	@Transactional
	public int testSave2()  {
    
    
		try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         int m = 1/0;
	         return 1;
		} catch (Exception e) {
    
    
			System.out.println(e.getMessage());
		}
		return 0;
	}

case13 事务回滚

@Transactional(rollbackFor = Exception.class)
	public int testSave() throws Exception {
    
    
		int m =  testSave2();
		int n = 1/0;
		return m;
	}
	@Transactional
	public int testSave2()  {
    
    
		try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         return 1;
		} catch (Exception e) {
    
    
			System.out.println(e.getMessage());
		}
		return 0;
	}

case14 事务回滚

@Transactional(rollbackFor = Exception.class)
	public int testSave() throws Exception {
    
    
		int m =  testSave2();
		int n = 1/0;
		return m;
	}
	public int testSave2()  {
    
    
		try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         return 1;
		} catch (Exception e) {
    
    
			System.out.println(e.getMessage());
		}
		return 0;
	}

case15 事务回滚

@Transactional(rollbackFor = Exception.class)
	public int testSave() throws Exception {
    
    
		int m =  testSave2();
		int n = 1/0;
		return m;
	}
	@Transactional(rollbackFor = Exception.class)
	public int testSave2()  {
    
    
		try {
    
    
			 User user = new User();
	         user.setName("岳不群2");
	         user.setAge(70);
	         user.setEmail("[email protected]");
	         userMapper.insert(user);
	         return 1;
		} catch (Exception e) {
    
    
			System.out.println(e.getMessage());
		}
		return 0;
	}

Guess you like

Origin blog.csdn.net/weixin_39472101/article/details/118388843
Sao