高并发秒杀系统--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.如 ...
随机推荐
- 【转】Android开发:Service和Thread的关系
不少Android初学者都可能会有这样的疑惑,Service和Thread到底有什么关系呢?什么时候应该用Service,什么时候又应该用Thread?答案可能会有点让你吃惊,因为Service和Th ...
- 理解mysql执行多表联合查询
阅读目录 一:inner join(内连接) 二:left join(左连接) 三:right join(右连接) 四:cross join(交叉连接) 五:union操作 六:node+mysql ...
- Linux启动时间优化-内核和用户空间启动优化实践
关键词:initcall.bootgraph.py.bootchartd.pybootchart等. 启动时间的优化,分为两大部分,分别是内核部分和用户空间两大部分. 从内核timestamp 0.0 ...
- TensorRT&Sample&Python[fc_plugin_caffe_mnist]
本文是基于TensorRT 5.0.2基础上,关于其内部的fc_plugin_caffe_mnist例子的分析和介绍. 本例子相较于前面例子的不同在于,其还包含cpp代码,且此时依赖项还挺多.该例子展 ...
- 全文搜索引擎 ElasticSearch 还是 Solr?
最近项目组安排了一个任务,项目中用到了全文搜索,基于全文搜索 Solr,但是该 Solr 搜索云项目不稳定,经常查询不出来数据,需要手动全量同步,而且是其他团队在维护,依赖性太强,导致 Solr 服务 ...
- PHP(SentCMS)网站 “新手”捉虫记
我拖着疲惫的身躯,努力打开眼皮在写...... 昨晚弄到12点,我感觉应该弄好了. 故事开头是这样的:我呢朋友有个网站需要开发,我当时没时间就包给外面的公司了,由于外面公司维护费用比较贵. 那么网站维 ...
- TensorFlow基础
TensorFlow基础 SkySeraph 2017 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站点:www.skyseraph.com Over ...
- Java的selenium代码随笔(1)
package ShareClass; import java.awt.AWTException;import java.awt.Robot;import java.awt.Toolkit;impor ...
- SQLiteOpenHelper+ContentProvider的使用
效果图: PetDbHelper package com.example.admin.pets; import android.content.Context;import android.datab ...
- 量化交易之下单函数和context对象
一.下单函数 聚宽设计的函数(如前文所说准确叫法是API)的用法都写在API文档里,位置在聚宽网站导航栏-帮助-API文档 1.order按股数下单 order(security, amount, s ...