Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

急!基于soa的事务管理疑问 #50

Closed
buguniao opened this issue Dec 10, 2016 · 4 comments
Closed

急!基于soa的事务管理疑问 #50

buguniao opened this issue Dec 10, 2016 · 4 comments

Comments

@buguniao
Copy link

buguniao commented Dec 10, 2016

按照您的代码实现来看,请问如何解决下面这个场景的问题:
1、一个事务下有两个参与者service1、service2,service1 转账100元给service2
2、try 阶段,service1 - 100 ,成功。service2 + 100 ,失败。
3、cancel阶段,service1、service2都执行cancel方法,service1 + 100,service2 - 100
4、 结果就是 service1、 service2 回滚到未转账之前的状态,一切看起来很完美!

但是, service2虽然失败了,但一定需要cancel吗?

比如 service2中的方法是这样写的:

public void transferTo(TransactionContext transactionContext, long accountId, int amount) {
       throw new Exception("我故意抛异常"); // 方法立即抛异常
       accoutDao.addBalanceAmount(accountId,amount);// service2 加了100元 
}

然后在cancel 阶段 ,service1+100、service2-100
但是service2之前根本没有加过100
结果就是service1 金额正确,service2 金额被无缘无故的减了100

问题就在于:基于服务而不是基于数据库资源的事务管理,在不能判断抛异常是由于其他业务逻辑造成还是因为数据库操作失败造成的情况下就进行回滚是存在问题的

@buguniao buguniao changed the title 基于soa的事务管理疑问 急!基于soa的事务管理疑问 Dec 10, 2016
@changmingxie
Copy link
Owner

@buguniao 根据你的例子,事务应该是按照下面流程:

  1. try :service1-100,
  2. confirm: 如果service2没有加100,则service2+100.
  3. cancel: 如果service1减了100,则service1+100.

@buguniao
Copy link
Author

感谢回信,不过还有点疑问:
1、我的理解是在try阶段应该尽可能把该做的事情都做了, service1 - 100 && service2 +100
confirm和cancel阶段只是针对try阶段做确认或者回滚,而不是跟你上边说的,try阶段只是做 service1 - 100

@kixiong
Copy link

kixiong commented Dec 16, 2016 via email

@alexqdjay
Copy link

我想问下,service1的本地事务是什么时候提交的?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants