java service层 事务_Java高并发秒时啊API之Service层1
---2-1 使用Spring托管Service依赖理论----------------------------
spring ioc优势(工厂模式):
1.对象创建统一托管
2.规范的生命周期管理
3.灵活的依赖注入
4.一致的获取对象
Spring IOC 功能的理解
DAO依赖+Service依赖最终形成一致访问接口;
随意访问依赖对象
Spring IOC 容器下的 对象 默认都是单例的。
业务对象依赖图:Spring IOC容器 通过 DI 解决 依赖链(对象之间的依赖关系)
SeckillService ->SeckillDao ->SqlSessionFactry->DataSource...
->successKilledDao
Spring-IOC注入方式和场景
本项目IOC使用:
XML配置
package-scan
Annotation注解
---2-2 使用Spring托管Service依赖配置---------------------------------------------------------
SeckillServiceImpl.java:
配置spring-service.xml
标注注解@Service和@Autowired
packageorg.seckill.service.impl;importjava.util.Date;importjava.util.List;importorg.seckill.dao.SeckillDao;importorg.seckill.dao.SuccessKilledDao;importorg.seckill.dto.Exposer;importorg.seckill.dto.SeckillExecution;importorg.seckill.entity.Seckill;importorg.seckill.entity.SuccessKilled;importorg.seckill.enums.SeckillStatEnum;importorg.seckill.exception.RepeatKillExeception;importorg.seckill.exception.SeckillCloseException;importorg.seckill.exception.SeckillException;importorg.seckill.service.SeckillService;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.util.DigestUtils;//@Component @service @Dao @Controller
@Servicepublic class SeckillServiceImpl implementsSeckillService {private Logger logger = LoggerFactory.getLogger(this.getClass());//注入Service依赖
@Autowired //在Spring 查找Dao类型的实例(Mybatis实现的,),并注入。不在需要自己new 实例//还有@Resource,@Inject等J2EE规范的注解
privateSeckillDao seckillDao;
@AutowiredprivateSuccessKilledDao successKilledDao;//md5盐值字符串,用于混淆MD5
private final String slat = "sldijfldkjfpaojj@#(#$sldfj`123";
@Overridepublic ListgetSeckillList() {return seckillDao.queryAll(0, 4);
}
@Overridepublic Seckill getById(longseckillId) {//TODO Auto-generated method stub
returnseckillDao.queryById(seckillId);
}
@Overridepublic Exposer exportSeckillUrl(longseckillId) {
Seckill seckill=seckillDao.queryById(seckillId);if(seckill == null){return new Exposer(false,seckillId);
}
Date startTime=seckill.getStartTime();
Date endTime=seckill.getEndTime();//系统当前时间
Date nowTime = newDate();if(nowTime.getTime() endTime.getTime()){return new Exposer(false,seckillId,nowTime.getTime(),startTime.getTime(),
endTime.getTime());
}//转化特定字符串的过程,不可逆
String md5 = getMD5(seckillId);//TODO
return new Exposer(true,md5,seckillId);
}private String getMD5(longseckillId){
String base= seckillId+"/"+slat;
String md5=DigestUtils.md5DigestAsHex(base.getBytes());returnmd5;
}
@Overridepublic SeckillExecution executeSeckill(long seckillId, longuserPhone, String md5)throwsSeckillException, RepeatKillExeception, SeckillCloseException {if(md5==null|| !md5.equals(getMD5(seckillId))){throw new SeckillException("seckill data rewrite");
}//执行秒杀逻辑:减库存 + 记录购买行为
Date nowTime = newDate();try{int updateCount =seckillDao.reduceNumber(seckillId, nowTime);if(updateCount <=0){//没有更新记录
throw new SeckillCloseException("seckill is closed");
}else{//记录购买行为
int insertCount=successKilledDao.insertSuccessKilled(seckillId, userPhone);//唯一:insert ignore
if(insertCount <=0){//重复秒杀
throw new RepeatKillExeception("seckill repeated");
}else{//秒杀成功
SuccessKilled successKilled =successKilledDao.queryByIdWithSeckill(seckillId, userPhone);//return new SeckillExecution(seckillId,1,"秒杀成功",successKilled);
return newSeckillExecution(seckillId,SeckillStatEnum.SUCCESS);
}
}
}catch(SeckillCloseException e1) {throwe1;
}catch(RepeatKillExeception e2) {throwe2;
}catch(Exception e){
logger.error(e.getMessage(),e);//所有编译期异常 转化为运行期异常//spring事务会做roll back
throw new SeckillException("seckill inner error : "+e.getMessage());
}
}
}
spring-service.xml
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
---3-1 使用Spring声明式事务理论---------------------------------------------------------
1.什么是声明式事务
通过Spring来管理(全自动),解脱事务代码
2.声明式事务使用方式:
ProxyFactoryBean + XML -------> 早期使用方式(2.0)
tx:advice + aop命名空间 ------> 一次配置永久生效
注解@Transactional (推荐) ------>注解控制()
注解控制:在控制事务方法上加入注解,在开发中大家遵守约定。
3.事务方法嵌套:
声明式事务独有的概念
传播行为--->propagation_required,如果有事务就加入到事务的原有逻辑,如果没有就创建一个新事务。
4.什么时候回滚:
抛出运行期异常(RuntimeException)
小心不当的try-catch(如果不小心抛出了编译期异常,Spring不会回滚)
---3-2 使用Spring声明式事务配置---------------------------------------------------------
建议:使用注解而不是XML控制事务
事务要小心设计
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
使用AOP的方式配置不推荐,对于真正需要标注的方法定位不精确。
SeckillServiceImpl.java :需要使用事务管理的方法添加@Transactional注解
使用注解控制事务方法的优点:
1、开发团队达成一致约定,明确标注事务方法的编程风格。
ps:使用aop管理事务会造成可能遗忘需要使用什么方法命名等问题
2、保证事务方法的执行时间尽可能短,不要穿插其他网络操作rpc/http等或者剥离到事务外部。
ps:因为这些操作一次要几毫秒到几十毫秒,影响事务速度。
3、不是所有的方法都需要事务,如只有一条修改操作,只读操作不需要事务控制。
ps:如果在配置文件里配置永久使用aop控制事务,不同的人的命名习惯可能会给不需要事务的方法添加事务
@Override
@Transactional/** 使用注解控制事务方法的优点:
* 1、开发团队达成一致约定,明确标注事务方法的编程风格。
* ps:使用aop管理事务会造成可能遗忘需要使用什么方法命名等问题
* 2、保证事务方法的执行时间尽可能短,不要穿插其他网络操作rpc/http等或者剥离到事务外部。
* ps:因为这些操作一次要几毫秒到几十毫秒,影响事务速度。
* 3、不是所有的方法都需要事务,如只有一条修改操作,只读操作不需要事务控制。
* ps:如果在配置文件里配置永久使用aop控制事务,不同的人的命名习惯可能会给不需要事务的方法添加事务
**/
public SeckillExecution executeSeckill(long seckillId, longuserPhone, String md5)throwsSeckillException, RepeatKillExeception, SeckillCloseException {if(md5==null|| !md5.equals(getMD5(seckillId))){throw new SeckillException("seckill data rewrite");
}//执行秒杀逻辑:减库存 + 记录购买行为
Date nowTime = newDate();try{int updateCount =seckillDao.reduceNumber(seckillId, nowTime);if(updateCount <=0){//没有更新记录
throw new SeckillCloseException("seckill is closed");
}else{//记录购买行为
int insertCount=successKilledDao.insertSuccessKilled(seckillId, userPhone);//唯一:insert ignore
if(insertCount <=0){//重复秒杀
throw new RepeatKillExeception("seckill repeated");
}else{//秒杀成功
SuccessKilled successKilled =successKilledDao.queryByIdWithSeckill(seckillId, userPhone);//return new SeckillExecution(seckillId,1,"秒杀成功",successKilled);
return newSeckillExecution(seckillId,SeckillStatEnum.SUCCESS);
}
}
}catch(SeckillCloseException e1) {throwe1;
}catch(RepeatKillExeception e2) {throwe2;
}catch(Exception e){
logger.error(e.getMessage(),e);//所有编译期异常 转化为运行期异常//spring事务会做roll back
throw new SeckillException("seckill inner error : "+e.getMessage());
}
}
---4-1 完成Service集成测试---------------------------------------------------------
1.logback配置
logback-test.xml place it into a directory accessible from the class path(放在classpath下:resources下)
public void testGetSeckillList() {
List list= seckillService.getSeckillList();
logger.info("list={}",list);
}
打印日志可以用占位符,输出在{}内
来自一个代码洁癖患者的忠告:
关于logback文档警告问题的解决:
在文档头加入空的dtd即可:
参考:
http://stackoverflow.com/questions/5731162/xml-schema-or-dtd-for-logback-xml
\seckill\src\main\resources\logback.xml
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
2.JUnit 测试类
packageorg.seckill.service;importjava.util.List;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.seckill.dto.Exposer;importorg.seckill.dto.SeckillExecution;importorg.seckill.entity.Seckill;importorg.seckill.exception.RepeatKillExeception;importorg.seckill.exception.SeckillCloseException;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml","classpath:spring/spring-service.xml"})public classSeckillServiceImplTest {private final Logger logger = LoggerFactory.getLogger(this.getClass());
@AutowiredprivateSeckillService seckillService;
@Testpublic void testGetSeckillList() throwsException{
List list=seckillService.getSeckillList();
logger.info("list={}",list);
}
@Testpublic void testGetById() throwsException{long id = 1000;
Seckill seckill=seckillService.getById(id);
logger.info("seckill={}",seckill);
}
@Testpublic void testExportSeckillUrl() throwsException{long id = 1000;
Exposer exposer=seckillService.exportSeckillUrl(id);
logger.info("exposere={}",exposer.toString());/** exposed=true,
* md5=76e96c3b47df23d4239478bf599aae92
**/}
@Testpublic void testExecuteSeckill() throwsException{long id = 1000;long phone = 13411112222L;
String md5= "76e96c3b47df23d4239478bf599aae92";//SeckillExecution正确返回初次执行,SeckillCloseException,RepeatKillExeception(同id+phone重复执行时)都是正确期待结果
try{
SeckillExecution execution=seckillService.executeSeckill(id, phone, md5);
logger.info("SeckillExecution={}",execution);
}catch(SeckillCloseException e) {
logger.error(e.getMessage());
}catch(RepeatKillExeception e) {
logger.error(e.getMessage());
}
}
@Test//结合3,4组成逻辑测试方法//测试代码完整逻辑,注意可重复执行
public void testSeckillLogic() throwsException{long id = 1000;
Exposer exposer=seckillService.exportSeckillUrl(id);if(exposer.isExposed()){
logger.info("exposere={}",exposer.toString());long phone = 13411112222L;
String md5=exposer.getMd5();//SeckillExecution正确返回初次执行,SeckillCloseException,RepeatKillExeception都是正确期待结果
try{
SeckillExecution execution=seckillService.executeSeckill(id, phone, md5);
logger.info("SeckillExecution={}",execution);
}catch(SeckillCloseException e) {
logger.error(e.getMessage());
}catch(RepeatKillExeception e) {
logger.error(e.getMessage());
}
}else{//秒杀未开启
logger.warn("exposer={}",exposer);
}
}
}
spring事务管理该过程:
13:47:56.706 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
13:47:56.716 [main] DEBUG org.mybatis.spring.SqlSessionUtils -Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2049a9c1]
13:47:56.725 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@934b6cb]will be managed by Spring
13:47:56.733 [main] DEBUG o.s.dao.SeckillDao.reduceNumber - ==> Preparing: update seckill set number = number - 1 where seckill_id = ? and start_time <= ? and end_time >= ? and number > 0;
13:47:56.784 [main] DEBUG o.s.dao.SeckillDao.reduceNumber - ==> Parameters: 1000(Long), 2017-07-10 13:47:56.698(Timestamp), 2017-07-10 13:47:56.698(Timestamp)
13:47:56.855 [main] DEBUG o.s.dao.SeckillDao.reduceNumber - <== Updates: 1
13:47:56.856 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2049a9c1]
13:47:56.856 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2049a9c1] from current transaction
13:47:56.856 [main] DEBUG o.s.d.S.insertSuccessKilled - ==> Preparing: insert ignore into Success_killed(seckill_id,user_phone,state) values(?,?,0);
13:47:56.858 [main] DEBUG o.s.d.S.insertSuccessKilled - ==> Parameters: 1000(Long), 13411112222(Long)
13:47:56.970 [main] DEBUG o.s.d.S.insertSuccessKilled - <== Updates: 1
13:47:56.980 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2049a9c1]
13:47:56.981 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2049a9c1] from current transaction
13:47:56.984 [main] DEBUG o.s.d.S.queryByIdWithSeckill - ==> Preparing: select sk.seckill_id, sk.user_phone, sk.state, sk.create_time, s.seckill_id "seckill.seckill_id", s.name "seckill.name", s.number "seckill.number", s.start_time "seckill.start_time", s.end_time "seckill.end_time", s.create_time "seckill.create_time" from Success_killed sk inner join seckill s on sk.seckill_id =s.seckill_id where sk.seckill_id = ? and sk.user_phone = ?;
13:47:56.984 [main] DEBUG o.s.d.S.queryByIdWithSeckill - ==> Parameters: 1000(Long), 13411112222(Long)
13:47:57.013 [main] DEBUG o.s.d.S.queryByIdWithSeckill - <== Total: 1
13:47:57.022 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2049a9c1]
13:47:57.023 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2049a9c1]
13:47:57.023 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2049a9c1]
13:47:57.023 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2049a9c1]
13:47:57.077 [main] INFO o.s.service.SeckillServiceImplTest - SeckillExecution=org.seckill.dto.SeckillExecution@7d898981
*/
java service层 事务_Java高并发秒时啊API之Service层1相关推荐
- JAVA秒杀mysql层实现_Java高并发秒杀API之web层
第1章 设计Restful接口 1.1前端交互流程设计 1.2 学习Restful接口设计 什么是Restful?它就是一种优雅的URI表述方式,用来设计我们资源的访问URL.通过这个URL的设计,我 ...
- java 内存模型 多线程_Java 高并发三:Java内存模型和线程安全详解
网上很多资料在描述Java内存模型的时候,都会介绍有一个主存,然后每个工作线程有自己的工作内存.数据在主存中会有一份,在工作内存中也有一份.工作内存和主存之间会有各种原子操作去进行同步. 下图来源于这 ...
- java queue源码_Java高并发系列之ArrayBlockingQueue源码解析
JUC包下定义了一个接口:BlockingQueue.其实现类有ArrayBlockingQueue等.本文先来介绍一下ArrayBlockingQueue.从字面可以看出,ArrayBlocking ...
- java 爱哦大容量并发_Java高并发的常见应对方案
Java高并发的常见应对方案 一.关于并发我们说的高并发是什么? 在互联网时代,高并发,通常是指,在某个时间点,有很多个访问同时到来. 高并发,通常关心的系统指标与业务指标? QPS:每秒钟查询量,广 ...
- Java生鲜电商平台-高并发核心技术订单与库存实战
Java生鲜电商平台-高并发核心技术订单与库存实战 一. 问题 一件商品只有100个库存,现在有1000或者更多的用户来购买,每个用户计划同时购买1个到几个不等商品. 如何保证库存在高并发的场景下是安 ...
- Java多线程精讲(非高并发-授课专用)附synchronized
Java多线程精讲(非高并发-授课专用) 目录 程序,进程,线程的基本概念 start与run的区别 函数测试demo: 创建线程(一)[new Thread()] 创建线程(二)[extends T ...
- Java——使用多线程模拟真实高并发业务并保证安全性(一)
作者专注于Java.架构.Linux.小程序.爬虫.自动化等技术. 工作期间含泪整理出一些资料,微信搜索[javaUp],回复 [java][黑客][爬虫][小程序][面试]等关键字免费获取资料.技术 ...
- Java面试中常见的高并发解决方案
Java面试中常见的高并发解决方案 一般来讲,提高系统应对高并发能力的解决方案可以从以下几个方面入手: (1)高性能服务器 (2)高性能数据库 (3)高效编程语言 (4)高性能web容器 提高数据库性 ...
- 高并发访问服务器时前端优化
高并发访问服务器时前端优化 高并发的痛点:数据流动过程两端失衡了,会导致前端到后台部分的请求会被后台拒掉甚至可能会击垮后台.需要把两端重新回到数据流动的平衡状态.前端层面尽可能地加强其作为用户与后台之 ...
最新文章
- Spring Roo开发初评
- 单片机声光电子琴程序流程图_基于单片机的智能家居控制系统毕业设计
- 数据库:redis和MySQL如何做到数据的一致性?
- ubuntu装jdk
- 当下的互联网时代,“急功近利”不是坏事
- 【ES6】ES6简介,ES6常用操作,let 、var和const的区别,看完我写的ES6,马上会的,你就是高手
- 01Postgresql下载安装和配置
- JavaWeb学习--课程设计《小米商城》
- 大学计算机基础第一章知识点归纳,《大学计算机基础》主要知识点
- QQ被盗后,如何找回好友
- K8S集群部署kube-Prometheus监控Ceph(版本octopus)集群、并实现告警。
- 软件性能测试论文草稿
- 原神白铁矿位在哪里 原神白铁矿采集点位置分布图
- 小米平板1 android5,小米平板5即将到来?对小米平板前几代做了一下总结
- 【线性代数的本质|笔记】线性组合、张成的空间、基
- 城市路口闯红灯监测系统
- fancybox参数设置
- 百灵达um2和umc22_百灵达UMC22和百灵达UMC404声卡简评?
- android官方文档翻译,Android API Guide:Search 中文翻译
- 纹理分析以及结合深度学习来提升纹理分类效果
热门文章
- python中的pymysql_(转)Python中操作mysql的pymysql模块详解
- linux打包cpio命令例子,linux压缩命令——tar、cpio详解
- java队列优先级_优先级队列-Java的PriorityQueue与最小堆有何不同?
- Jmeter跨线程组传递参数
- mysql用代码建表基础语法
- 平行四边形的特殊性质
- 【原创】大叔经验分享(6)Oozie如何查看提交到Yarn上的任务日志
- Warning: Received `false` for a non-boolean attribute `xxx`.
- 【贪心算法】POJ-1017
- hdu 3641 数论 二分求符合条件的最小值数学杂题