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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zcmu-1960
- 下一篇: linux shell加密压缩,linu