事务调整

This commit is contained in:
Wang.Huan
2025-03-05 12:23:42 +08:00
parent 6b219fb219
commit 85333551e8

View File

@@ -1,54 +1,59 @@
// package com.core.framework.aspectj; package com.core.framework.aspectj;
//
// import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterReturning;
// import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.AfterThrowing;
// import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect;
// import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Before;
// import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
// import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
// import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.TransactionStatus;
// import org.springframework.transaction.support.DefaultTransactionDefinition; import org.springframework.transaction.support.DefaultTransactionDefinition;
//
/// ** /**
// * 事务处理 * 事务处理
// */ */
// @Aspect @Aspect
// @Component @Component
// public class TransactionAspect { public class TransactionAspect {
//
// private final PlatformTransactionManager transactionManager; private final PlatformTransactionManager transactionManager;
// private TransactionStatus transactionStatus; private final ThreadLocal<TransactionStatus> transactionStatus = new ThreadLocal<>();
//
// public TransactionAspect(PlatformTransactionManager transactionManager) { public TransactionAspect(PlatformTransactionManager transactionManager) {
// this.transactionManager = transactionManager; this.transactionManager = transactionManager;
// } }
//
// @Before("@annotation(org.springframework.web.bind.annotation.PostMapping) || " + @Before("@annotation(org.springframework.web.bind.annotation.PostMapping) || " +
// "@annotation(org.springframework.web.bind.annotation.GetMapping) || " + "@annotation(org.springframework.web.bind.annotation.GetMapping) || " +
// "@annotation(org.springframework.web.bind.annotation.PutMapping) || " + "@annotation(org.springframework.web.bind.annotation.PutMapping) || " +
// "@annotation(org.springframework.web.bind.annotation.DeleteMapping)") "@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
// public void beginTransaction() { public void beginTransaction() {
// transactionStatus = transactionManager.getTransaction(new DefaultTransactionDefinition()); TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
// } transactionStatus.set(status);
// }
// @AfterReturning("@annotation(org.springframework.web.bind.annotation.PostMapping) || " +
// "@annotation(org.springframework.web.bind.annotation.GetMapping) || " + @AfterReturning("@annotation(org.springframework.web.bind.annotation.PostMapping) || " +
// "@annotation(org.springframework.web.bind.annotation.PutMapping) || " + "@annotation(org.springframework.web.bind.annotation.GetMapping) || " +
// "@annotation(org.springframework.web.bind.annotation.DeleteMapping)") "@annotation(org.springframework.web.bind.annotation.PutMapping) || " +
// public void commitTransaction() { "@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
// if (transactionStatus != null && !transactionStatus.isCompleted()) { public void commitTransaction() {
// transactionManager.commit(transactionStatus); TransactionStatus status = transactionStatus.get();
// } if (status != null && !status.isCompleted()) {
// } transactionManager.commit(status);
// transactionStatus.remove(); // 清除 ThreadLocal 中的状态
// @AfterThrowing(pointcut = "@annotation(org.springframework.web.bind.annotation.PostMapping) || " + }
// "@annotation(org.springframework.web.bind.annotation.GetMapping) || " + }
// "@annotation(org.springframework.web.bind.annotation.PutMapping) || " +
// "@annotation(org.springframework.web.bind.annotation.DeleteMapping)", @AfterThrowing(pointcut = "@annotation(org.springframework.web.bind.annotation.PostMapping) || " +
// throwing = "ex") "@annotation(org.springframework.web.bind.annotation.GetMapping) || " +
// public void rollbackTransaction(Exception ex) { "@annotation(org.springframework.web.bind.annotation.PutMapping) || " +
// if (transactionStatus != null && !transactionStatus.isCompleted()) { "@annotation(org.springframework.web.bind.annotation.DeleteMapping)",
// transactionManager.rollback(transactionStatus); throwing = "ex")
// } public void rollbackTransaction(Exception ex) {
// } TransactionStatus status = transactionStatus.get();
// } if (status != null && !status.isCompleted()) {
transactionManager.rollback(status);
transactionStatus.remove(); // 清除 ThreadLocal 中的状态
}
}
}