CQRS 架構中 一個事務生成的多個領域event 如何執行

sinaID87521發表於2014-06-19
CQRS 架構中 多個事務生成的領域event 誰先執行誰後執行?

https://github.com/banq/jdonframework/blob/master/example/cqrs%2Bes/match/src/main/java/sample/domain/Match.java

@Model
public class Match {
	private String id;
	private Date matchDate;
	private Team teams[] = new Team[2];
	private boolean finished;
	@Inject
	public EventSourcing es;
	public void handle(MatchCreatedEvent matchCreatedEvent) {
		this.id = matchCreatedEvent.getMatchId();
		this.teams[0] = new Team(matchCreatedEvent.getMatchTeamName1());
		this.teams[1] = new Team(matchCreatedEvent.getMatchTeamName2());
		this.finished = false;
	}
	public void startMatch(Date matchDate) {
		if (this.matchDate != null)
			System.err.print("the match has started");
		es.started(new MatchStartedEvent(this.id, matchDate));
	}
	public void handle(MatchStartedEvent event) {
		this.matchDate = event.getMatchDate();
	}
	public void finishWithScore(Score score, Date matchDate) {
		if (this.matchDate == null)
			System.err.print("the match has not started");
		if (finished)
			System.err.print("the match has finished");
		es.finished(new MatchFinishedEvent(this.id, matchDate, score.getHomeGoals(), score.getAwayGoals()));
	}
	public void handle(MatchFinishedEvent event) {
		this.finished = true;
	}
	public String getId() {
		return id;
	}
	public String getHomeTeamName() {
		return teams[0].getName();
	}
	public String getAwayTeamName() {
		return teams[1].getName();
	}
	public boolean isFinished() {
		return finished;
	}
}
<p class="indent">

非同步框架,透過事件進行非同步化.
可能一個事務生成了一場比賽事件MatchStartedEvent1,還有其他實體的事件.比如每個人出場次數+1Event PersonEvent1;
另外一個事務生成了另外兩個事件. MatchStartedEvent2 ,PersonEvent2.
然後會處理這些事件,因為MatchStartedEvent1和PersonEvent1之前是屬於一個事務的, 那麼我的問題是執行兩個事件,把Match和Persion的變化持久化到資料庫.
.是否也是放在一個事務內執行?
如果不是,如何保證事務內操作的要麼全部回滾,要麼全部成功的特性

相關文章