被標記為事務的方法互相呼叫的坑(下)
參考:https://www.iteye.com/topic/1122740
上一節,主要分析了 被標記為事務的方法互相呼叫,事務失效的原因,思考比較多,這一節主要說說解決方案,思考會少一些。
解決方案的核心: 通過代理物件去呼叫方法
1.把方法放到不同的類:
我們需要新建一個介面:
public interface OtherService { void insertCodeMonkey(); }
再定義一個類去實現這個介面:
@Service public class OtherServiceImpl implements OtherService { @Autowired AccountMapper mapper; @Override @Transactional(propagation=Propagation.REQUIRES_NEW) public void insertCodeMonkey() { Account account = new Account(); account.setAccount("CodeMonkey"); account.setPassword("CodeMonkey"); mapper.insert(account); int a = 1 / 0; } }
修改原本的實現類:
@Service public class AccountSerivceImpl implements AccountService { @Autowired AccountMapper mapper; @Autowired OtherService otherService; @Transactional @Override public void insertCodeBear() { try { otherService.insertCodeMonkey(); } catch (Exception e) { e.printStackTrace(); } Account account = new Account(); account.setAccount("CodeBear"); account.setPassword("CodeBear"); mapper.insert(account); } }
讓我們再看看控制檯的日誌:

可以看到是開了兩個事務去執行的。
這種解決方案最簡單,不需要了解其他東西,但是這種方案需要修改程式碼結構,本來兩個方法都是屬於同一個類的,現在需要強行把它們拆開。
2. AopContext:
我們的目標是要在實現類中獲取本類的代理物件,Spring提供了Aop上下文,即:AopContext,通過AopContext,可以很方便的獲取到代理物件:
@Service public class AccountSerivceImpl implements AccountService { @Autowired AccountMapper mapper; @Transactional @Override public void insertCodeBear() { try { ((AccountService)AopContext.currentProxy()).insertCodeMonkey(); } catch (Exception ex) { ex.printStackTrace(); } Account account = new Account(); account.setAccount("CodeBear"); account.setPassword("CodeBear"); mapper.insert(account); } @Transactional(propagation = Propagation.REQUIRES_NEW) @Override public void insertCodeMonkey() { Account account = new Account(); account.setAccount("CodeMonkey"); account.setPassword("CodeMonkey"); mapper.insert(account); int a = 1 / 0; } }
當寫好程式碼,很愉快的去測試,發現竟然報錯了:
翻譯下:不能找到當前的代理,需要設定exposeProxy屬性為 true使其可以。
expose字面意思就是 暴露。也就是說 我們需要允許暴露代理。
我們需要在Spring Boot啟動類上+一個註解:
@EnableAspectJAutoProxy(exposeProxy = true) @SpringBootApplication @MapperScan(basePackages = "com.codebear.Dao") public class SpringbootApplication { public static void main(String[] args) throws Exception { SpringApplication.run(SpringbootApplication.class, args); } }
再次執行:
確實是開啟了兩個事務去執行的。
再看看資料庫,也沒有問題。
3. ApplicationContext:
@Service public class AccountSerivceImpl implements AccountService { @Autowired AccountMapper mapper; @Autowired ApplicationContext context; AccountService service; @PostConstruct private void setSelf() { service = context.getBean(AccountService.class); } @Transactional @Override public void insertCodeBear() { try { service.insertCodeMonkey(); } catch (Exception e) { e.printStackTrace(); } Account account = new Account(); account.setAccount("CodeBear"); account.setPassword("CodeBear"); mapper.insert(account); } @Transactional(propagation = Propagation.REQUIRES_NEW) @Override public void insertCodeMonkey() { Account account = new Account(); account.setAccount("CodeMonkey"); account.setPassword("CodeMonkey"); mapper.insert(account); int a = 1 / 0; } }
驗證的圖片就省略了。
此方法不適用於prototype
在這裡,我用了一個@PostConstruct註解,在初始化的時候,會呼叫被@PostConstruct標記的方法(注意,僅僅是初始化的時候,才會被呼叫。以後都不會被呼叫了,大家可以打個斷點試一下),這裡這麼做的目的就是為了提升一下效率,不用每次都getBean。所以如果這個類是prototype的,就不適用這個方法了。如果是prototype的話,就在insertCodeBear方法中使用getBean方法吧。
上兩種方法比較方便,沒有新建其他的介面或者是類,但是沒有很好的封裝獲得Aop代理物件的過程,也不是很符合 迪位元法則,也就是最少知識原則。
4. 重寫BeanPostProcessor介面:
關於這個介面是做什麼的,這裡就不詳細闡述了,簡單的來說這是Spring提供的介面,我們可以通過重寫它,在初始化Bean之前或者之後,自定義一些額外的邏輯。
首先,我們需要定義一個介面:
public interface WeavingSelfProxy { void setSelfProxy(Object bean); }
要獲得代理物件的類,需要去實現它:
@Service public class AccountSerivceImpl implements AccountService, WeavingSelfProxy { @Autowired AccountMapper mapper; AccountService service; @Override public void setSelfProxy(Object bean) { System.out.println("進入到setSelfProxy方法"); service = (AccountService) bean; } @Transactional @Override public void insertCodeBear() { try { service.insertCodeMonkey(); } catch (Exception e) { e.printStackTrace(); } Account account = new Account(); account.setAccount("CodeBear"); account.setPassword("CodeBear"); mapper.insert(account); } @Transactional(propagation = Propagation.REQUIRES_NEW) @Override public void insertCodeMonkey() { Account account = new Account(); account.setAccount("CodeMonkey"); account.setPassword("CodeMonkey"); mapper.insert(account); int a = 1 / 0; } }
重寫BeanPostProcessor介面:
@Component public class SetSelfProxyProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if(bean instanceof WeavingSelfProxy){ System.out.println("實現了WeavingSelfProxy介面"); ((WeavingSelfProxy) bean).setSelfProxy(bean); } return bean; } }
這樣就可以了,驗證的圖片也省略了。
以上就是四種解決方案,可以說 各有千秋,沒有哪個好,哪個壞,只有適不適合。