Hakunata opened a new issue #733:
URL: https://github.com/apache/servicecomb-pack/issues/733


   父事物逻辑:
   `@TccStart
       public Result transfer(TransferEntity transferEntity){
           String errMsg = "";
           Integer errCode = 0;
           try{
               ResponseEntity<UserAccount> outResp = 
alphaClient.getForEntity(getBankAlphaURL(transferEntity),UserAccount.class,transferEntity.getFromId(),transferEntity.getMoney()
 );
               System.out.println(outResp.getBody().toString());
               ResponseEntity<UserAccount> inResp = 
betaClient.getForEntity(getBankBetaURL(transferEntity),UserAccount.class,transferEntity.getToId(),transferEntity.getMoney()
 );
               System.out.println(inResp.getBody().toString());
               errMsg = "user[id:"+transferEntity.getFromId()+"] transfer 
meney("+transferEntity.getMoney()+") to user[id:"+transferEntity.getToId()+"] 
success";
           }catch (Exception e){
               e.printStackTrace();
               errMsg = e.getMessage();
               errCode = -1;
           }
           return new Result(errCode,errMsg);
       }
   
       private String getBankAlphaURL(TransferEntity transferEntity){
           return "http://localhost:8001/api/transfer/out/{id}/{money}";;
       }
   
       private String getBankBetaURL(TransferEntity transferEntity){
           return "http://localhost:8002/api/transfer/in/{id}/{money}";;
       }`
   
   ---------------------------------------------------------------------------
   子事务逻辑(服务:bank_alpha):
   `
   
       @Participate(confirmMethod = "confirm", cancelMethod = "cancel")
       @Transactional
       public UserAccount transferOut(TransferEntity entity){
           UserAccount userAccount = 
accountMapper.selectByIdForUpdate(entity.getId());
           if (Objects.isNull(userAccount)){
               throw new IllegalArgumentException("Transfer failed,account is 
not exists");
           }
           if (userAccount.getAmount().compareTo(new 
BigDecimal(entity.getMoney().toString())) < 0){
               throw new IllegalArgumentException("Transfer failed,not enough 
money");
           }
           userAccount.setFreezedAmount(userAccount.getFreezedAmount().add(new 
BigDecimal(entity.getMoney().toString())));
           accountMapper.updateById(userAccount);
           return userAccount;
       }
       // bank_beta入账事务失败,结果失败通知调用的是confirm方法
       @Transactional
       public void confirm(TransferEntity entity){
           UserAccount userAccount = 
accountMapper.selectByIdForUpdate(entity.getId());
           if (userAccount.getAmount().compareTo(new 
BigDecimal(entity.getMoney().toString())) < 0){
               throw new IllegalArgumentException("Transfer failed,not enough 
money");
           }
           
userAccount.setFreezedAmount(userAccount.getFreezedAmount().subtract(new 
BigDecimal(entity.getMoney().toString())));
           userAccount.setAmount(userAccount.getAmount().subtract(new 
BigDecimal(entity.getMoney().toString())));
           accountMapper.updateById(userAccount);
       }
   
       @Transactional
       public void cancel(TransferEntity entity){
           UserAccount userAccount = 
accountMapper.selectByIdForUpdate(entity.getId());
           
userAccount.setFreezedAmount(userAccount.getFreezedAmount().subtract(new 
BigDecimal(entity.getMoney().toString())));
           accountMapper.updateById(userAccount);
       }`
   
   ---------------------------------------------------------------------
   子事务逻辑(服务:bank_beta):
   `
   
       @Participate(confirmMethod = "confirm", cancelMethod = "cancel")
       @Transactional
       public UserAccount transferIn(TransferEntity entity){
           UserAccount userAccount = 
accountMapper.selectByIdForUpdate(entity.getId());
           if (Objects.isNull(userAccount)){
               // 这里会出发异常,终止入账事务
               throw new IllegalArgumentException("Transfer failed,account is 
not exists");
           }
           if (userAccount.getAmount().compareTo(new 
BigDecimal(entity.getMoney().toString())) < 0){
               throw new IllegalArgumentException("Transfer failed,not enough 
money");
           }
           userAccount.setFreezedAmount(userAccount.getFreezedAmount().add(new 
BigDecimal(entity.getMoney().toString())));
           accountMapper.updateById(userAccount);
           return userAccount;
       }
   
       @Transactional
       public void confirm(TransferEntity entity){
           UserAccount userAccount = 
accountMapper.selectByIdForUpdate(entity.getId());
           if (userAccount.getAmount().compareTo(new 
BigDecimal(entity.getMoney().toString())) < 0){
               throw new IllegalArgumentException("Transfer failed,not enough 
money");
           }
           
userAccount.setFreezedAmount(userAccount.getFreezedAmount().subtract(new 
BigDecimal(entity.getMoney().toString())));
           userAccount.setAmount(userAccount.getAmount().add(new 
BigDecimal(entity.getMoney().toString())));
           accountMapper.updateById(userAccount);
       }
   
       @Transactional
       public void cancel(TransferEntity entity){
           UserAccount userAccount = 
accountMapper.selectByIdForUpdate(entity.getId());
           
userAccount.setFreezedAmount(userAccount.getFreezedAmount().subtract(new 
BigDecimal(entity.getMoney().toString())));
           accountMapper.updateById(userAccount);
       }`
   
   
---------------------------------------------------------------------------------
   开始以为是我自己编译alpha版本的问题,结果用apache的二进制包还是一样;


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to