上一篇 <<<Spring声明事务原理及核心源码分析
下一篇 >>>Spring常用注解汇总
手动事务
@Component
@Scope("prototype") // 每个事务都是新的实例 目的解决线程安全问题 多例子
public class TransactionUtils {
// 全局接受事务状态
private TransactionStatus transactionStatus;
// 获取事务源
@Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
// 开启事务
public TransactionStatus begin() {
System.out.println("开启事务");
//DefaultTransactionAttribute定义了传播属性和隔离机制
transactionStatus = dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
return transactionStatus;
}
// 提交事务
public void commit(TransactionStatus transaction) {
System.out.println("提交事务");
dataSourceTransactionManager.commit(transaction);
}
// 回滚事务
public void rollback() {
System.out.println("回滚事务...");
dataSourceTransactionManager.rollback(transactionStatus);
}
}
// 代码样例
public void add() {
TransactionStatus transactionStatus = null;
try {
// 开启事务
transactionStatus = transactionUtils.begin();
userDao.add("test001", 20);
System.out.println("开始报错啦!@!!");
int i = 1 / 0;
System.out.println("################");
userDao.add("test002", 21);
// 提交事务
if (transactionStatus != null)
transactionUtils.commit(transactionStatus);
} catch (Exception e) {
e.getMessage();
// 回滚事务
if (transactionStatus != null)
transactionUtils.rollback(transactionStatus);
}
}
自定义注解事务
//自定义注解
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface ExtTransaction {
}
//AOP+手动事务
@Aspect
@Component
public class AopExtTransaction {
@Autowired
private TransactionUtils transactionUtils;
// 使用异常通知进行 回滚事务
@AfterThrowing("execution(* com.itmayiedu.service.*.*.*(..))")
public void afterThrowing() {
// TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
transactionUtils.rollback();
}
// 环绕通知 在方法之前和之后处理事情
@Around("execution(* com.itmayiedu.service.*.*.*(..))")
public void around(ProceedingJoinPoint pjp) throws Throwable {
// 1.获取该方法上是否加上注解
ExtTransaction extTransaction = getMethodExtTransaction(pjp);
TransactionStatus transactionStatus = begin(extTransaction);
// 2.调用目标代理对象方法
pjp.proceed();
// 3.判断该方法上是否就上注解
commit(transactionStatus);
}
private TransactionStatus begin(ExtTransaction extTransaction) {
if (extTransaction == null) {
return null;
}
return transactionUtils.begin();
}
private void commit(TransactionStatus transactionStatus) {
if (transactionStatus != null) {
transactionUtils.commit(transactionStatus);
}
}
// 获取方法上是否存在事务注解
private ExtTransaction getMethodExtTransaction(ProceedingJoinPoint pjp)
throws NoSuchMethodException, SecurityException {
String methodName = pjp.getSignature().getName();
// 获取目标对象
Class<?> classTarget = pjp.getTarget().getClass();
// 获取目标对象类型
Class<?>[] par = ((MethodSignature) pjp.getSignature()).getParameterTypes();
// 获取目标对象方法
Method objMethod = classTarget.getMethod(methodName, par);
ExtTransaction extTransaction = objMethod.getDeclaredAnnotation(ExtTransaction.class);
return extTransaction;
}
}
//注解使用
@ExtTransaction
public void add() {
userDao.add("test001", 20);
// int i = 1 / 0;
System.out.println("################");
userDao.add("test002", 21);
}
推荐阅读:
<<<Spring IOC的初始化原理
<<<SpringBean的生命周期流程图
<<<SpringBean单例情况下解决循环依赖的原理
<<<Spring AOP的底层原理
<<<Spring AOP通知责任链模式图解
<<<Spring AOP核心源码解读
<<<Spring事务常识汇总
<<<Spring声明事务原理及核心源码分析
<<<Spring常用注解汇总