Spring-Tx原理

package com.xxx.tx;

import java.beans.PropertyVetoException;

import javax.sql.DataSource;

import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration;
import org.springframework.transaction.annotation.TransactionManagementConfigurationSelector;
import org.springframework.transaction.annotation.Transactional;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 声明式事务:
 * 
 * 环境搭建:
 * 1、导入相关依赖
 *         数据源、数据库驱动、Spring-jdbc模块
 * 2、配置数据源、JdbcTemplate(Spring提供的简化数据库操作的工具)操作数据
 * 3、给方法上标注 @Transactional 表示当前方法是一个事务方法;
 * 4、 @EnableTransactionManagement 开启基于注解的事务管理功能;
 *         @EnableXXX
 * 5、配置事务管理器来控制事务;
 *         @Bean
 *         public PlatformTransactionManager transactionManager()
 * 
 * 
 * 原理:
 * 1)、@EnableTransactionManagement
 *             利用TransactionManagementConfigurationSelector给容器中会导入组件
 *             导入两个组件
 *             AutoProxyRegistrar
 *             ProxyTransactionManagementConfiguration
 * 2)、AutoProxyRegistrar:
 *             给容器中注册一个 InfrastructureAdvisorAutoProxyCreator 组件;
 *             InfrastructureAdvisorAutoProxyCreator:?
 *             利用后置处理器机制在对象创建以后,包装对象,返回一个代理对象(增强器),代理对象执行方法利用拦截器链进行调用;
 * 
 * 3)、ProxyTransactionManagementConfiguration 做了什么?
 *             1、给容器中注册事务增强器;
 *                 1)、事务增强器要用事务注解的信息,AnnotationTransactionAttributeSource解析事务注解
 *                 2)、事务拦截器:
 *                     TransactionInterceptor;保存了事务属性信息,事务管理器;
 *                     他是一个 MethodInterceptor;
 *                     在目标方法执行的时候;
 *                         执行拦截器链;
 *                         事务拦截器:
 *                             1)、先获取事务相关的属性
 *                             2)、再获取 PlatformTransactionManager,如果事先没有添加指定任何transactionmanger
 *                                 最终会从容器中按照类型获取一个PlatformTransactionManager;
 *                             3)、执行目标方法
 *                                 如果异常,获取到事务管理器,利用事务管理回滚操作;
 *                                 如果正常,利用事务管理器,提交事务

 *             
 */
@EnableTransactionManagement
@ComponentScan("com.xxx.tx")
@Configuration
public class TxConfig {
    
    //数据源
    @Bean
    public DataSource dataSource() throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        return dataSource;
    }
    
    @Bean
    public JdbcTemplate jdbcTemplate() throws Exception{
        //Spring对@Configuration类会特殊处理;给容器中加组件的方法,多次调用都只是从容器中找组件
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        return jdbcTemplate;
    }
    
    //注册事务管理器在容器中
    @Bean
    public PlatformTransactionManager transactionManager() throws Exception{
        return new DataSourceTransactionManager(dataSource());
    }   

}

package com.xxx.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserService {
    
    @Autowired
    private UserDao userDao;
    
    @Transactional
    public void insertUser(){
        userDao.insert();
        //otherDao.other();xxx
        System.out.println("插入完成...");
//        int i = 10/0;
    }

}

package com.xxx.tx;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void insert(){
        String sql = "INSERT INTO `tbl_user`(username,age) VALUES(?,?)";
        String username = UUID.randomUUID().toString().substring(0, 5);
        jdbcTemplate.update(sql, username,19);
        
    }

package com.xxx.test;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.atguigu.tx.TxConfig;
import com.atguigu.tx.UserService;

public class IOCTest_Tx {
    
    @Test
    public void test01(){
        AnnotationConfigApplicationContext applicationContext = 
                new AnnotationConfigApplicationContext(TxConfig.class);
        UserService userService = applicationContext.getBean(UserService.class);
        userService.insertUser();
        applicationContext.close();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值