zoukankan      html  css  js  c++  java
  • 高并发秒杀系统--Service事务管理与继承测试

    [Spring IoC的类型及应用场景]

     [Spring事务使用方式]

     

    [Spring事务的特性]

    [Spring事务回滚的理解]

    [Service声明式事务的配置]

    1.配置事务管理器

    2.配置基于注解的声明式事务

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 扫描service包下所有使用注解的类型 -->
        <context:component-scan base-package="org.azcode.service"/>
    
        <!-- 声明式事务的配置 -->
        <!-- step1: 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 注入数据库连接池 -->
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!-- step2: 配置基于注解的声明式事务
            默认使用注解来管理事务行为
        -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>

    [使用注解控制事务的优点]

    1:开发团队达成一致约定,明确标注事务方法的编程风格

    2:保证事务方法的执行时间尽可能短,不要穿插其他的网络操作,RPC/HTTP请求或者剥离到事务方法外.

    3:不是所有的方法都需要事务,如只有一条修改操作,只读操作不需要事务.(区别AOP+tx:advice的方式)

    [Service单元测试总结]

    1.对于已知异常需要捕获

    public void executeSeckill() throws Exception {
            long id = 1004;
            long phone = 13665263598L;
            String md5 = "1e8672b6c06f90e5f4991cde12ed15cd";
            try {
                SeckillExecution seckillExecution = seckillService.executeSeckill(id, phone, md5);
                logger.info("seckillExecution={}", seckillExecution);
            } catch (RepeatKillException e) {
                logger.error(e.getMessage());
            } catch (SeckillCloseException e) {
                logger.error(e.getMessage());
            }
            //seckillExecution=SeckillExecution{
            // seckillId=1004, state=1, stateInfo='秒杀成功',
            // successKilled=SuccessKilled{seckillId=1004, userPhone=13665263598,
            // status=0, createTime=Sun Apr 16 09:26:35 CST 2017}}
    
            //org.azcode.exception.SeckillException: seckill data rewrite
            //org.azcode.exception.RepeatKillException: seckill repeated
        }

    2.业务相关的测试方法应整合在一起,形成一个完整的逻辑,保证可重复执行

    暴露秒杀接口+执行秒杀

    public void testSeckillLogic() throws Exception {
            long id = 1001;
            Exposer exposer = seckillService.exportSeckillUrl(id);
            if(exposer.isExposed()){
                //秒杀业务开始
                logger.info("exposer={}",exposer);
                long phone = 13665263598L;
                String md5 = exposer.getMd5();
                try {
                    SeckillExecution seckillExecution = seckillService.executeSeckill(id, phone, md5);
                    logger.info("seckillExecution={}", seckillExecution);
                } catch (RepeatKillException e) {
                    logger.error(e.getMessage());
                } catch (SeckillCloseException e) {
                    logger.error(e.getMessage());
                }
            }else{
                //秒杀业务未开启
                logger.warn("exposer={}",exposer);
                //exposer=Exposer{exposed=false, md5='null',
                // seckillId=1001, now=1492307486311,
                // start=1492099200000, end=1492185600000}
            }
        }
  • 相关阅读:
    Windows Azure Web Site (19) Azure Web App链接到VSTS
    Windows Azure Virtual Machine (35) Azure VM通过Linked DB,执行SQL Job
    Azure PowerShell (16) 并行开关机Azure ARM VM
    Windows Azure Virtual Network (12) 虚拟网络之间点对点连接VNet Peering
    Azure ARM (21) Azure订阅的两种管理模式
    Windows Azure Platform Introduction (14) 申请海外的Windows Azure账户
    Azure ARM (20) 将非托管磁盘虚拟机(Unmanage Disk),迁移成托管磁盘虚拟机(Manage Disk)
    Azure ARM (19) 将传统的ASM VM迁移到ARM VM (2)
    Azure ARM (18) 将传统的ASM VM迁移到ARM VM (1)
    Azure Automation (6) 执行Azure SQL Job
  • 原文地址:https://www.cnblogs.com/azcode/p/6718028.html
Copyright © 2011-2022 走看看