spring中@Transactional注解的作用,使用场景举例
一,spring中管理事务一般使用@Transactional 注解,下面对@Transactional 使用的各个场景做一个列举,尽可能的将所有场景罗列出来
1,场景一,最常见的用法,在方法上使用@Transactional 注解,事务正常起作用。无异常时正常提交,有异常时数据回滚,代码如下。
@Service
public class ComeServiceImpl implements ComeService {
@Autowired
UserMapper userMapper;
@Override
@Transactional
public int saveUser() {
User user1 = new User(11,"a",111,"a");
userMapper.saveUser(user1);
// int i = 1 / 0;
User user2 = new User(11,"b",111,"b");
userMapper.saveUser(user2);
return 0;
}
}
2,场景二,常见的用法,在类上使用@Transactional 注解,对整个类的方法,事务起作用。无异常时正常提交,有异常时数据回滚,代码如下。
@Service
@Transactional
public class ComeServiceImpl implements ComeService {
@Autowired
UserMapper userMapper;
@Override
public int saveUser() {
User user1 = new User(11,"a",111,"a");
userMapper.saveUser(user1);
User user2 = new User(11,"b",111,"b");
userMapper.saveUser(user2);
// int i = 1 / 0;
return 0;
}
}
3,场景三,将异常信息使用try-catch 包裹,异常被处理,@Transactional 注解不起作用,数据提交,没有回滚,代码如下。
@Service
@Slf4j
public class ComeServiceImpl implements ComeService {
@Autowired
UserMapper userMapper;
@Override
@Transactional
public int saveUser() {
User user1 = new User(11,"a",111,"a");
userMapper.saveUser(user1);
User user2 = new User(11,"b",111,"b");
userMapper.saveUser(user2);
try {
int i = 1 / 0;
}catch (Exception e){
System.out.println("异常。。。");
}
return 0;
}
}
4,场景四,同一个Service内方法调用,当@Transactional 注解作用在A方法上时,事务起作用。方法A中的数据回滚,方法B中的数据回滚,代码如下。
@Service
@Slf4j
public class ComeServiceImpl implements ComeService {
@Autowired
UserMapper userMapper;
@Override
@Transactional
public int A() {
User user1 = new User(11,"a",111,"a");
userMapper.saveUser(user1);
this.B();
return 0;
}
@Override
public int B() {
User user2 = new User(11,"b",111,"b");
userMapper.saveUser(user2);
int i = 1 / 0;
return 0;
}
}
5,场景五,同一个Service内方法调用,当@Transactional 注解作用在B方法上时,事务不起作用。方法A中的数据提交,方法B中数据提交,遇到异常没有回滚,代码如下。
@Service
@Slf4j
public class ComeServiceImpl implements ComeService {
@Autowired
UserMapper userMapper;
@Override
public int A() {
User user1 = new User(11,"a",111,"a");
userMapper.saveUser(user1);
this.B();
return 0;
}
@Override
@Transactional
public int B() {
User user2 = new User(11,"b",111,"b");
userMapper.saveUser(user2);
int i = 1 / 0;
return 0;
}
}
6,场景六,同一个Service内方法调用,当@Transactional 注解作用在类上时,事务起作用,数据回滚,代码如下。
@Service
@Slf4j
@Transactional
public class ComeServiceImpl implements ComeService {
@Autowired
UserMapper userMapper;
@Override
public int A() {
User user1 = new User(11,"a",111,"a");
userMapper.saveUser(user1);
this.B();
return 0;
}
@Override
public int B() {
User user2 = new User(11,"b",111,"b");
userMapper.saveUser(user2);
int i = 1 / 0;
return 0;
}
}
Java注解@Transactional事务类内调用不生效问题及解决办法pdf0星超过10%的资源63KB下载
7,场景七,同一个Service内方法调用私有的方法C,当@Transactional 注解作用在方法A上时,事务起作用,数据回滚,代码如下。
@Service
@Slf4j
public class ComeServiceImpl implements ComeService {
@Autowired
UserMapper userMapper;
@Override
@Transactional
public int A() {
User user1 = new User(11,"a",111,"a");
userMapper.saveUser(user1);
this.C();
return 0;
}
private int C() {
User user2 = new User(11,"b",111,"b");
userMapper.saveUser(user2);
int i = 1 / 0;
return 0;
}
@Override
public int B() {
return 0;
}
}
8,场景八,同一个Service内方法调用私有的方法C,当@Transactional 注解作用在方法C上时,事务不起作用,方法A中的数据提交,方法C中的数据提交,代码如下。
@Service
@Slf4j
public class ComeServiceImpl implements ComeService {
@Autowired
UserMapper userMapper;
@Override
public int A() {
User user1 = new User(11,"a",111,"a");
userMapper.saveUser(user1);
this.C();
return 0;
}
@Transactional
private int C() {
User user2 = new User(11,"b",111,"b");
userMapper.saveUser(user2);
int i = 1 / 0;
return 0;
}
@Override
public int B() {
return 0;
}
}
9,场景九,不同Service方法间调用,当@Transactional 注解作用在方法A上时,事务起作用,方法A中的数据回滚,方法saveClassInfo中的数据回滚,代码如下。
@Service
@Slf4j
public class ComeServiceImpl implements ComeService {
@Autowired
UserMapper userMapper;
@Autowired
ClassInfoService classInfoService;
@Override
@Transactional
public int A() {
User user1 = new User(11,"a",111,"a");
userMapper.saveUser(user1);
classInfoService.saveClassInfo();
return 0;
}
@Override
public int B() {
return 0;
}
}
@Service
public class ClassInfoServiceImpl implements ClassInfoService {
@Autowired
ClassInfoMapper classInfoMapper;
@Override
public int saveClassInfo() {
ClassInfo classInfo = new ClassInfo("c","c",69D);
classInfoMapper.saveClassInfo(classInfo);
int i = 1/0;
return 0;
}
}
10,场景十,不同Service方法间调用,当@Transactional 注解作用在方法saveClassInfo上时,事务对A不起作用,方法A中的数据提交,方法saveClassInfo数据回滚,代码如下。
@Service
@Slf4j
public class ComeServiceImpl implements ComeService {
@Autowired
UserMapper userMapper;
@Autowired
ClassInfoService classInfoService;
@Override
public int A() {
User user1 = new User(11,"a",111,"a");
userMapper.saveUser(user1);
classInfoService.saveClassInfo();
return 0;
}
@Override
public int B() {
return 0;
}
}
@Service
public class ClassInfoServiceImpl implements ClassInfoService {
@Autowired
ClassInfoMapper classInfoMapper;
@Override
@Transactional
public int saveClassInfo() {
ClassInfo classInfo = new ClassInfo("c","c",69D);
classInfoMapper.saveClassInfo(classInfo);
int i = 1/0;
return 0;
}
}