在平时开发中,同一个Service类中非事务方法调用事务方法,事务会失效失效,这里简单解释一下原因:spring采用动态代理机制来实现事务控制,而动态代理最终都是要调用原始对象的,而原始对象在去调用方法时,是不会再触发代理了!可以理解为同一个类中非事务方法调用方法时用的是当前对象去调用,而不是spring生成的代理对象,所以会导致事务失效。
演示一下事务失效:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
@Transactional(readOnly = true)
public List<User> find() throws Exception {
return userMapper.selectList(null);
}
@Override
@Transactional(rollbackFor = Exception.class)
public int saveUser(User user) throws Exception {
int result = userMapper.insert(user);
result = 1 / 0;
return result;
}
/**
* 非事务方法
*/
@Override
public int doSomething() throws Exception {
User user = new User();
user.setName("张三");
user.setAge(23);
user.setCreateTime(LocalDateTime.now());
return saveUser(user);
}
}
调用doSomething()后,日志打印:
DEBUG==> Preparing: INSERT INTO t_user ( id, name, age, create_time ) VALUES ( ?, ?, ?, ? )
DEBUG==> Parameters: 1255426392998391809(Long), 张三(String), 23(Integer), 2020-04-29T17:19:11.418(LocalDateTime)
DEBUG<== Updates: 1
java.lang.ArithmeticException: / by zero
at com.learn.service.impl.UserServiceImpl.saveUser(UserServiceImpl.java:30)
数据库:
可以看到虽然程序出现了异常,可是数据库却依然保存了数据,并没有回滚事务。原因开头已经说过了,下面说一下解决方法:
1.将需要进行事务管理的方法单独写到一个Service文件中:
@Service
public class UserSaveService {
@Autowired
private UserMapper userMapper;
@Transactional(rollbackFor = Exception.class)
public int saveUser(User user) throws Exception {
int result = userMapper.insert(user);
result = 1 / 0;
return result;
}
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserSaveService userSaveService; // 注入另一个Service对象
/**
* 非事务方法
*/
@Override
public int doSomething() throws Exception {
User user = new User();
user.setName("李四");
user.setAge(23);
user.setCreateTime(LocalDateTime.now());
return userSaveService.saveUser(user);
}
}
异常结果:
DEBUG==> Preparing: INSERT INTO t_user ( id, name, age, create_time ) VALUES ( ?, ?, ?, ? )
DEBUG==> Parameters: 1255428835366748161(Long), 李四(String), 23(Integer), 2020-04-29T17:28:52.789(LocalDateTime)
DEBUG<== Updates: 1
java.lang.ArithmeticException: / by zero
at com.learn.service.impl.UserSaveService.saveUser(UserSaveService.java:18)
2.使用 AopContext.currentProxy() 获取代理对象:
增加maven依赖:
org.springframework.boot</groupId>
spring-boot-starter-aop</artifactId>
</dependency>
springboot启动类加上注解:@EnableAspectJAutoProxy(exposeProxy = true)
@SpringBootApplication
@MapperScan(basePackages = "com.learn.dao")
@EnableTransactionManagement
@EnableAspectJAutoProxy(exposeProxy = true)
public class SpringbootMybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisplusApplication.class, args);
}
}
Service类:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Transactional(rollbackFor = Exception.class)
public int saveUser(User user) throws Exception {
int result = userMapper.insert(user);
result = 1 / 0;
return result;
}
/**
* 非事务方法
*/
@Override
public int doSomething() throws Exception {
User user = new User();
user.setName("王五");
user.setAge(23);
user.setCreateTime(LocalDateTime.now());
UserServiceImpl currentProxy = (UserServiceImpl) AopContext.currentProxy(); // 获取代理对象
return currentProxy.saveUser(user);
}
}
异常信息:
DEBUG==> Preparing: INSERT INTO t_user ( id, name, age, create_time ) VALUES ( ?, ?, ?, ? )
DEBUG==> Parameters: 1255432158203445249(Long), 王五(String), 23(Integer), 2020-04-29T17:42:05.152(LocalDateTime)
DEBUG<== Updates: 1
java.lang.ArithmeticException: / by zero
at com.learn.service.impl.UserServiceImpl.saveUser(UserServiceImpl.java:22)
3.将该类对象在其他类中维护成一个成员变量,借助这个成员变量调用方法:
例如构造一个工具类:
@Component
public class ServiceUtil {
private static ServiceUtil serviceUtil;
@Autowired
private UserService userService;
@PostConstruct
public void init() {
serviceUtil = this;
serviceUtil.userService = this.userService;
}
public static UserService getUserService() {
return serviceUtil.userService;
}
}
使用工具类调用方法:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Transactional(rollbackFor = Exception.class)
public int saveUser(User user) throws Exception {
int result = userMapper.insert(user);
result = 1 / 0;
return result;
}
/**
* 非事务方法
*/
@Override
public int doSomething() throws Exception {
User user = new User();
user.setName("赵六");
user.setAge(23);
user.setCreateTime(LocalDateTime.now());
// 使用工具类调用方法
return ServiceUtil.getUserService().saveUser(user);
}
}
异常信息:
DEBUG==> Preparing: INSERT INTO t_user ( id, name, age, create_time ) VALUES ( ?, ?, ?, ? )
DEBUG==> Parameters: 1255665465251971073(Long), 赵六(String), 23(Integer), 2020-04-30T09:09:10.708(LocalDateTime)
DEBUG<== Updates: 1
java.lang.ArithmeticException: / by zero
at com.learn.service.impl.UserServiceImpl.saveUser(UserServiceImpl.java:22)
另外,你也可以在类上加注解@Transactional,这样所有方法都可以进行事务管理,但是不推荐这么做,特别是有些方法执行时间很长,这种尽量不添加事务管理,而是在里面调用的具体方法上进行事务管理,不然可能产生长事务,导致数据库连接被长期占用,导致未知异常,比如一个简单查询都不能及时执行!