高并发秒杀系统--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}
}
}
高并发秒杀系统--Service事务管理与继承测试的更多相关文章
- 高并发秒杀系统--Service接口设计与实现
[DAO编写之后的总结] DAO层 --> 接口设计 + SQL编写 DAO拼接等逻辑 --> 统一在Service层完成 [Service层的接口设计] 1.接口 ...
- Java高并发秒杀系统API之SSM框架集成swagger与AdminLTE
初衷与整理描述 Java高并发秒杀系统API是来源于网上教程的一个Java项目,也是我接触Java的第一个项目.本来是一枚c#码农,公司计划部分业务转java,于是我利用业务时间自学Java才有了本文 ...
- 【高并发】Redis如何助力高并发秒杀系统,看完这篇我彻底懂了!!
写在前面 之前,我们在<[高并发]高并发秒杀系统架构解密,不是所有的秒杀都是秒杀!>一文中,详细讲解了高并发秒杀系统的架构设计,其中,我们介绍了可以使用Redis存储秒杀商品的库存数量.很 ...
- Java高并发秒杀系统【观后总结】
项目简介 在慕课网上发现了一个JavaWeb项目,内容讲的是高并发秒杀,觉得挺有意思的,就进去学习了一番. 记录在该项目中学到了什么玩意.. 该项目源码对应的gitHub地址(由观看其视频的人编写,并 ...
- 高并发秒杀系统方案(分布式session)
编程要有一个习惯:做参数校验 所谓的分布式session:就是用redis统一管理session. 我们这里的思路是:把token写入cookie中,客户端在随后的访问中携带cookie,服务端就能根 ...
- 高并发秒杀系统--junit测试类与SpringIoc容器的整合
1.原理是在Junit启动时加载SpringIoC容器 2.SpringIoC容器要根据Spring的配置文件加载 [示例代码] package org.azcode.dao; import org. ...
- 高并发秒杀系统--mybatis整合技巧
mybatis实现DAO接口编码技巧 1.XML文件通过namespace命名空间关联接口类 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD ...
- 高并发秒杀系统方案(集成Mybatis和Redis)
1.集成Mybatis 第一步,添加依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> < ...
- 高并发秒杀系统--SpringMVC整合
[SpringMVC运行流程] [Handler注解映射技巧] [请求方法的细节处理] 1.如何处理请求参数和方法参数的绑定? 2.如何限制方法接收的请求方式? 3.如何进行请求转发和重定向? 4.如 ...
随机推荐
- ElasticSearch(七):Java操作elasticsearch基于smartcn中文分词查询
package com.gxy.ESChap01; import java.net.InetAddress; import org.elasticsearch.action.search.Search ...
- web框架开发-Django模型层(2)-多表操作
很重要,都是精华 多表关系模型 一对一 一旦确定表关系是一对一,在两张表中的任意一张表中建立关联字段+Unique 一对多 一旦确定表关系是一对多,创建关联字段在多的表中 多对多 一旦确定表关系是多对 ...
- (一) Getting Started
Elasticsearch is a highly scalable open-source full-text search and analytics engine. It allows you ...
- 使用CompletableFuture优化你的代码执行效率
这篇文章详细讲解java8中CompletableFuture的特性,方法以及实例. 在java8以前,我们使用java的多线程编程,一般是通过Runnable中的run方法来完成,这种方式,有个很明 ...
- ORA-01578 data block corrupted 数据文件损坏 与 修复 (多为借鉴 linux)
好吧,先说说造成崩溃的原因: 使用redhat 5.9 Linux 作为数据库服务器, 周五数据库正在使用中,硬关机造成数据库文件部分损坏(周一上班时,应用程序启动不起来,查看日志文件时,发现一个数据 ...
- mapreduce map 的个数
在map阶段读取数据前,FileInputFormat会将输入文件分割成split.split的个数决定了map的个数.影响map个数(split个数)的主要因素有: 1) 文件的大小.当块(dfs. ...
- rs485引脚定义
转自:http://blog.chinaunix.net/uid-9688646-id-3275796.html rs485有两种,一种是半双工模式,只有DATA+和DATA-两线,另一种是全双工模式 ...
- Shell命令-文件及目录操作之ls、cd
文件及目录操作 - ls.cd 1.ls:列出目录的内容及其内容属性信息 ls命令的功能说明 ls命令用于列出目录的内容及其内容属性信息. ls命令的语法格式 ls [OPTION]... [FILE ...
- Java中反射机制详解
序言 在学习java基础时,由于学的不扎实,讲的实用性不强,就觉得没用,很多重要的知识就那样一笔带过了,像这个马上要讲的反射机制一样,当时学的时候就忽略了,到后来学习的知识中,很多东西动不动就用反射, ...
- mysql主从复制、redis基础、持久化和主从复制
一.mysql(mariadb)基础 1.基础命令(centos7操作系统下) 1.启动mysql systemctl start mariadb 2.linux客户端连接自己 mysql -uroo ...