You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/12/17 16:57:32 UTC

[GitHub] Ouyangan opened a new issue #354: springboot1.5.17.RELEASE saga not work

Ouyangan opened a new issue #354: springboot1.5.17.RELEASE saga not work
URL: https://github.com/apache/servicecomb-pack/issues/354
 
 
   env:
   ```
   springboot:1.5.17.RELEASE
   orm: jpa
   db:mysql
   java -Dspring.profiles.active=mysql -jar alpha-server-0.3.0-SNAPSHOT-exec.jar
   ```
   application.properties
   ```
   app.datasource.first.url=jdbc:mysql://localhost/dt_1
   app.datasource.first.username=root
   app.datasource.first.password=root
   app.datasource.second.url=jdbc:mysql://localhost/dt_2
   app.datasource.second.username=root
   app.datasource.second.password=root
   app.datasource.second.max-total=30
   server.context-path=/saga
   logging.level.root=info
   spring.jpa.show-sql=true
   server.port=8083
   spring.application.name=/sagaTransaction
   alpha.cluster.address=127.0.0.1:8080
   ```
   ```
   @EnableTransactionManagement
   @EnableOmega
   @SpringBootApplication
   public class SagaApplication {
       public static void main(String[] args) {
           SpringApplication.run(SagaApplication.class, args);
       }
   }
   ```
   service code:
   ```
   @Service
   public class BuyService {
       @Autowired
       private AccountRepository accountRepository;
       @Autowired
       private GoodsRepository goodsRepository;
   
       @SagaStart(timeout = 10)
       public void buySuccess() {
           minusAmount(1, 1);
           minusGoods(1, 1);
       }
   
       /**
        * 扣减金额
        *
        * @param id
        * @param amount
        */
       @Compensable(timeout = 5, compensationMethod = "plusAmount")
       @Transactional(transactionManager = "firstTransactionManager", rollbackFor = Exception.class)
       public void minusAmount(long id, long amount) {
           accountRepository.minusAmount(id, amount);
       }
   
       @Transactional(transactionManager = "firstTransactionManager", rollbackFor = Exception.class)
       public void plusAmount(long id, long amount) {
           //todo 幂等处理
           accountRepository.plusAmount(id, amount);
       }
   
       /**
        * 扣减数量
        *
        * @param id
        * @param count
        */
       @Compensable(timeout = 5, compensationMethod = "plusGoods")
       @Transactional(transactionManager = "secondTransactionManager", rollbackFor = Exception.class)
       public void minusGoods(long id, long count) {
           goodsRepository.minusGoodsCount(id, count);
           //mock transaction fail
           int i = 10 / 0;
       }
   
       @Transactional(transactionManager = "secondTransactionManager", rollbackFor = Exception.class)
       public void plusGoods(long id, long count) {
           //todo 幂等处理
           goodsRepository.plusGoods(id, count);
       }
   
   }
   
   ```
   ```
   @Repository
   public interface AccountRepository extends CrudRepository<Account, Long> {
       @Transactional
       @Modifying
       @Query(value = "update account set amount=amount-?2 where id=?1",
               nativeQuery = true)
       void minusAmount(long id, long minusAmount);
   
       @Transactional
       @Modifying
       @Query(value = "update account set amount=amount+?2 where id=?1",
               nativeQuery = true)
       void plusAmount(long id, long amount);
   }
   ```
   ```
   @Repository
   public interface GoodsRepository extends CrudRepository<Goods, Long> {
       @Transactional
       @Modifying
       @Query(value = "update goods set quantity=quantity-?2 where id=?1",
               nativeQuery = true)
       void minusGoodsCount(long id, long minusCount);
   
       @Transactional
       @Modifying
       @Query(value = "update goods set quantity=quantity+?2 where id=?1",
               nativeQuery = true)
       void plusGoods(long id, long count);
   }
   ```
   application console:
   ```
   Hibernate: update account set amount=amount-? where id=?
   Hibernate: update goods set quantity=quantity-? where id=?
   2018-12-18 00:47:02.619 ERROR 49627 --- [nio-8083-exec-1] o.a.s.s.o.transaction.SagaStartAspect    : Transaction e59d7b01-b225-49cf-b8a8-c2494e09b97e failed.
   ```
   ![image](https://user-images.githubusercontent.com/8913987/50101985-ee515600-025e-11e9-86a6-cd9d08fc5352.png)
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services