[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}
}
}

高并发秒杀系统--Service事务管理与继承测试的更多相关文章

  1. 高并发秒杀系统--Service接口设计与实现

    [DAO编写之后的总结] DAO层    -->    接口设计 + SQL编写 DAO拼接等逻辑    -->    统一在Service层完成 [Service层的接口设计] 1.接口 ...

  2. Java高并发秒杀系统API之SSM框架集成swagger与AdminLTE

    初衷与整理描述 Java高并发秒杀系统API是来源于网上教程的一个Java项目,也是我接触Java的第一个项目.本来是一枚c#码农,公司计划部分业务转java,于是我利用业务时间自学Java才有了本文 ...

  3. 【高并发】Redis如何助力高并发秒杀系统,看完这篇我彻底懂了!!

    写在前面 之前,我们在<[高并发]高并发秒杀系统架构解密,不是所有的秒杀都是秒杀!>一文中,详细讲解了高并发秒杀系统的架构设计,其中,我们介绍了可以使用Redis存储秒杀商品的库存数量.很 ...

  4. Java高并发秒杀系统【观后总结】

    项目简介 在慕课网上发现了一个JavaWeb项目,内容讲的是高并发秒杀,觉得挺有意思的,就进去学习了一番. 记录在该项目中学到了什么玩意.. 该项目源码对应的gitHub地址(由观看其视频的人编写,并 ...

  5. 高并发秒杀系统方案(分布式session)

    编程要有一个习惯:做参数校验 所谓的分布式session:就是用redis统一管理session. 我们这里的思路是:把token写入cookie中,客户端在随后的访问中携带cookie,服务端就能根 ...

  6. 高并发秒杀系统--junit测试类与SpringIoc容器的整合

    1.原理是在Junit启动时加载SpringIoC容器 2.SpringIoC容器要根据Spring的配置文件加载 [示例代码] package org.azcode.dao; import org. ...

  7. 高并发秒杀系统--mybatis整合技巧

    mybatis实现DAO接口编码技巧 1.XML文件通过namespace命名空间关联接口类 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD ...

  8. 高并发秒杀系统方案(集成Mybatis和Redis)

    1.集成Mybatis 第一步,添加依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> < ...

  9. 高并发秒杀系统--SpringMVC整合

    [SpringMVC运行流程] [Handler注解映射技巧] [请求方法的细节处理] 1.如何处理请求参数和方法参数的绑定? 2.如何限制方法接收的请求方式? 3.如何进行请求转发和重定向? 4.如 ...

随机推荐

  1. python离线安装包

    一.用download命令离线下载包  *.whl , 这个方法好像python3.7以上才能用 那么我的requirement.txt内容就是: django==1.8.11 simplejson= ...

  2. Python 函数初识 (1)

    一.今日主要内容 认识函数 函数:对功能或者动作的封装(定义) 语法: def 函数名字(形参) 函数体 函数的调用格式:函数名(实参) 函数的返回值 关键字:return 终止函数的运行 1.函数内 ...

  3. 《JAVA程序设计》_第三周学习总结

    20175217吴一凡 一.IDEA学生免费版申请后续 收到这个邮件,就说明你申请成功了,点这里进去就行了 点击接受 在下一个界面登录你之前注册的账号绑定许可证就行了,重新登录你的账号就有了一年的许可 ...

  4. Spring Security(三十五):Part III. Testing

    This section describes the testing support provided by Spring Security. 本节介绍Spring Security提供的测试支持. ...

  5. drawer

    import 'package:flutter/material.dart'; class DrawerPage extends StatefulWidget { @override _DrawerP ...

  6. 【Topcoder 8572】TheLuckySum

    题意:给一个数\(n\),要把它分成lucky numbers的和. 问个数最少.字典序最小的方案. 思路:果断搜索.个数最少,所以迭代加深.枚举要的个数\(m\). 首先我们看\(n\)的个位.它肯 ...

  7. tcping ,一个好用的TCP端口检测工具

    1.常用的用法(windows) tcp -w 10 -t -d -i 5 -j --color 81.156.165.66 443 2. http模式 -u,与-h命令连用,每一行输出目标的url ...

  8. 基于 HTML5 WebGL 的 3D 工控裙房系统

    前言 工业物联网在中国的发展如火如荼,网络基础设施建设,以及工业升级的迫切需要都为工业物联网发展提供了很大的机遇.中国工业物联网企业目前呈现两种发展形式并存状况:一方面是大型通讯.IT企业的布局:一方 ...

  9. 好的LCT板子和一句话

    typedef long long ll; const int maxn = 400050; struct lct { int ch[maxn][2], fa[maxn], w[maxn]; bool ...

  10. OpenStack-Keystone(2)

    一. Keystone 概述 管理用户及其权限 维护OpenStack Services的Endpoint Authentication(认证)和 Authorization(授权) 1.验证用户 验 ...