美文网首页
spring-事务

spring-事务

作者: modou1618 | 来源:发表于2019-01-18 11:38 被阅读0次

一 事务信息配置

  • 配置事务管理器及数据库连接池
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
  • 配置事务注解@Transactional处理,注入事务管理器
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
  • 注入事务管理器transactionManager到事务调用代理函数TransactionInterceptor中,代理类调用时用于创建并管理事务。
RootBeanDefinition interceptorDef = new RootBeanDefinition(TransactionInterceptor.class);
interceptorDef.setSource(eleSource);
interceptorDef.setRole(2);
AnnotationDrivenBeanDefinitionParser.registerTransactionManager(element, interceptorDef);
interceptorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
String interceptorName = parserContext.getReaderContext().registerWithGeneratedName(interceptorDef);
  • TransactionInterceptor实现接口MethodInterceptor用于aop代理调用
public interface MethodInterceptor extends Interceptor {
    
    /**
     * Implement this method to perform extra treatments before and
     * after the invocation. Polite implementations would certainly
     * like to invoke {@link Joinpoint#proceed()}.
     *
     * @param invocation the method invocation joinpoint
     * @return the result of the call to {@link
     * Joinpoint#proceed()}, might be intercepted by the
     * interceptor.
     *
     * @throws Throwable if the interceptors or the
     * target-object throws an exception.  */
    Object invoke(MethodInvocation invocation) throws Throwable;
}
  • 封装Advisor类,aop创建代理类时会获取advisor接口实现类,匹配待创建的bean,匹配成功则创建aop代理。
RootBeanDefinition advisorDef = new RootBeanDefinition(BeanFactoryTransactionAttributeSourceAdvisor.class);
advisorDef.setSource(eleSource);
advisorDef.setRole(2);
//pointcut 切入点,通过注解参数初始化
advisorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
//advise 通知,代理调用
advisorDef.getPropertyValues().add("adviceBeanName", interceptorName);
if(element.hasAttribute("order")) {
    advisorDef.getPropertyValues().add("order", element.getAttribute("order"));
}
parserContext.getRegistry().registerBeanDefinition(txAdvisorBeanName, advisorDef);
  • aop调用TransactionAttributeSourcePointcut类方法match匹配目标类方法是否需要创建事务代理
@Override
public boolean matches(Method method, Class<?> targetClass) {
    if (TransactionalProxy.class.isAssignableFrom(targetClass)) {
        return false;
    }
    TransactionAttributeSource tas = getTransactionAttributeSource();
    return (tas == null || tas.getTransactionAttribute(method, targetClass) != null);
}

二 事务创建流程

spring事务.png

相关文章

  • Java Spring-声明式事务

    Spring-声明式事务

  • spring-事务

    事务 原子性 :强调事务的不可分割.一致性 :事务的执行的前后数据的完整性保持一致(转账).隔离性 :一个事务执行...

  • spring-事务

    1. 事务本身 ACID属性 原子性 Atomicity事务所包含的全部操作是一个不可分割的整体,这些操作要么全部...

  • spring-事务

    一 事务信息配置 配置事务管理器及数据库连接池 配置事务注解@Transactional处理,注入事务管理器

  • Spring-事务

    1.什么是事务 (1)事务是数据库操作最基本单元,逻辑上一组操作要么都成功,如果有一个失败所有操作都失败。 (2)...

  • Java进阶-Spring-其他

    一、Spring-其他 1.1 事务 原子性:一个事务就是一个不可再分解的单位,事务中的操作要么全部做,要么全部不...

  • Spring-事务机制

    一、Spring事务 事务管理概述 Spring事务管理分为编程式事务管理和声明式事务管理两种 编程式事务:允许用...

  • 19、Spring-事务机制-使用

    一、简介 Spring事务管理分为编程式事务管理和声明式事务管理两种, 声明式事务管理:底层是建立在Spring ...

  • Spring-声明式事务管理

    spring框架的声明式事务管理是通过spring AOP实现的,与EJB CMT类似,可以将事务行为指定到单个方...

  • Spring-编程式事务管理

    spring框架提供了两种编程式事务管理方式: 使用TransactionTemplate 直接使用Platfor...

网友评论

      本文标题:spring-事务

      本文链接:https://www.haomeiwen.com/subject/yftndqtx.html