[ 
https://issues.apache.org/jira/browse/SCB-785?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16561332#comment-16561332
 ] 

Willem Jiang edited comment on SCB-785 at 7/30/18 2:24 AM:
-----------------------------------------------------------

I think we could introduce an transaction context to let the user leverage the 
internal Saga implementation, just like this
{code:java}
@Compensable(timeout=5, compensationMethod="cancel")
public boolean transferOut(String from, int amount, @TransactionContext 
context) {
  //access the transaction related attribute from the TransactionContext
  System.out.println(context.globalTxId());
  System.out.println(context.localTxId());
  repo.reduceBalanceByUsername(from, amount);
}

public boolean cancel(String from, int amount, @TransactionContext context) {
  //access the transaction related attribute from the TransactionContext
  System.out.println(context.globalTxId());
  System.out.println(context.localTxId());
  repo.addBalanceByUsername(from, amount);
}
{code}
In this way we don't need to override the OmegaContext. 


was (Author: njiang):
I think we could introduce an transaction context to let the user leverage the 
internal Saga implementation, just like this
{code:java}
@Compensable(timeout=5, compensationMethod="cancel")
public boolean transferOut(String from, int amount, @TransactionContext 
context) {
  // 这里可以获得
  System.out.println(context.globalTxId());
  System.out.println(context.localTxId());
  repo.reduceBalanceByUsername(from, amount);
}

public boolean cancel(String from, int amount, @TransactionContext context) {
  // 这里不可以获得
  System.out.println(context.globalTxId());
  System.out.println(context.localTxId());
  repo.addBalanceByUsername(from, amount);
}
{code}
In this way we don't need to override the OmegaContext. 

> Cannot get the GlobalTxId and LocalTxId in the compensation method
> ------------------------------------------------------------------
>
>                 Key: SCB-785
>                 URL: https://issues.apache.org/jira/browse/SCB-785
>             Project: Apache ServiceComb
>          Issue Type: Improvement
>          Components: Saga
>    Affects Versions: saga-0.2.0
>            Reporter: Jeremy Xu
>            Priority: Minor
>
> 补偿方法里无法获取分布式事务的全局事务ID及本地事务ID
> 因为在子事务业务处理方法中会将一部分中间数据存入外部缓存中,对应的补偿方法将可以通过这些中间数据进行合理的补偿操作,当时存入中间数据时会附带上globalTxId,
>  
> localTxId,后面将根据这个查找到之前存入的中间数据。但发现在业务方法中可以获取分布式事务的全局事务ID及本地事务ID,但对应的补偿方法里却不行。示例代码如下:
>  
> {code:java}
> @Autowired
> OmegaContext omegaContext;
> @Compensable(timeout=5, compensationMethod="cancel")
> public boolean transferOut(String from, int amount) {
>   // 这里可以获得
>   System.out.println(omegaContext.globalTxId());
>   System.out.println(omegaContext.localTxId());
>   repo.reduceBalanceByUsername(from, amount);
> }
> public boolean cancel(String from, int amount) {
>   // 这里不可以获得
>   System.out.println(omegaContext.globalTxId());
>   System.out.println(omegaContext.localTxId());
>   repo.addBalanceByUsername(from, amount);
> }
> {code}
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to