MultiTransactionAop.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package org.jeecg.config.transactional;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.Around;
  4. import org.aspectj.lang.annotation.Aspect;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8. /**
  9. * 功能描述:
  10. *
  11. * @Author: chenchuang
  12. * @Date: 2022/3/31 16:50
  13. */
  14. @Aspect
  15. @Component
  16. public class MultiTransactionAop {
  17. private final ComboTransaction comboTransaction;
  18. @Autowired
  19. public MultiTransactionAop(ComboTransaction comboTransaction) {
  20. this.comboTransaction = comboTransaction;
  21. }
  22. @Pointcut("@annotation(org.jeecg.config.transactional.MultiTransactional)")
  23. public void pointCut() {
  24. }
  25. @Around("pointCut() && @annotation(multiTransactional)")
  26. public Object inMultiTransactions(ProceedingJoinPoint pjp, MultiTransactional multiTransactional) {
  27. return comboTransaction.inCombinedTx(() -> {
  28. try {
  29. return pjp.proceed();
  30. } catch (Throwable throwable) {
  31. if (throwable instanceof RuntimeException) {
  32. throw (RuntimeException) throwable;
  33. }
  34. throw new RuntimeException(throwable);
  35. }
  36. }, multiTransactional.value());
  37. }
  38. }