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

Saga executes compensation, but excludes interface which throws exception. #221

Closed
jiuang opened this issue Jul 16, 2018 · 4 comments
Closed

Comments

@jiuang
Copy link

jiuang commented Jul 16, 2018

Does the compensation depends on Spring Transaction?

I defined an compensable interface without Spring Transaction, but it didn't call rollback method when produced an exception in it.

My code:

@Compensable(compensationMethod = "updateMoneyByUserId_rollback")
public int updateMoneyByUserId(@Param("userId") long userId, @Param("money") long money) {
	System.err.println("[" + sdf.format(new Date()) + "] 进入 " + this.getClass() + ".updateMoneyByUserId 提交方法……\t\tid = " + userId + ", money = " + money);
		
	int result = userRepository.updateMoneyByUserId(userId, money);// commit to update balance immediately
		
	Integer.parseInt("");
		
	return result;
}

public int updateMoneyByUserId_rollback(@Param("userId") long userId, @Param("money") long money) {
	System.err.println("[" + sdf.format(new Date()) + "] 进入 " + this.getClass() + ".rollback 补偿方法……\t\tid = " + userId + ", money = " + -money);
	return userRepository.updateMoneyByUserId(userId, -money);
}

Information for txevent data:
image

@WillemJiang
Copy link
Member

Can you show us the big picture of the service invocation?
Current Saga implementation won't call the compensation method of ServiceA when ServiceA through the exception. You need to leverage spring transaction to revert the change in the call of ServiceA.

@jiuang
Copy link
Author

jiuang commented Jul 16, 2018

@WillemJiang "You need to leverage spring transaction to revert the change in the call of ServiceA."
Okay, I got.

@WillemJiang
Copy link
Member

@Gannalyo You can take the Dubbo demo as an example, the Transactional annotation could avoid the database commit if there is an exception throw. In this way we don't need to call the compensation method of ServiceA.
Please close this issue if you got the answer.

@jiuang
Copy link
Author

jiuang commented Jul 18, 2018

Right, it indeed save a call for the compensation of Service A. I have added the spring transaction for Service A, then it worked. Maybe Spring Cloud was better than Dubbo & GRPC.
Okay, I'll close it right now. Thanks.

@jiuang jiuang closed this as completed Jul 18, 2018
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

2 participants