在平時開發中,同一個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依賴:
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>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,這樣所有方法都可以進行事務管理,但是不推薦這么做,特別是有些方法執行時間很長,這種盡量不添加事務管理,而是在里面調用的具體方法上進行事務管理,不然可能產生長事務,導致數據庫連接被長期占用,導致未知異常,比如一個簡單查詢都不能及時執行!