zoukankan      html  css  js  c++  java
  • Is multiple aggregates update in one transaction good?

    I don't believe there is anything wrong with using multiple repositories to fetch data in a transaction. Often during a transaction an aggregate will need information from other aggregates in order to make a decision on whether to, or how to, change state. That's fine. It is, however, the modifying of state on multiple aggregates within one transaction that is deemed undesirable, and I think this what your referenced quote was trying to imply.

    The reason this is undesirable is because of concurrency. As well as protecting the in-variants within it's boundary, each aggregate should be protected from concurrent transactions. e.g. two users making a change to an aggregate at the same time.

    This protection is typically achieved by having a version/timestamp on the aggregates' DB table. When the aggregate is saved, a comparison is made of the version being saved and the version currently stored in the db (which may now be different from when the transaction started). If they don't match an exception is raised.

    It basically boils down to this: In a collaborative system (many users making many transactions), the more aggregates that are modified in a single transaction will result in an increase of concurrency exceptions.

    The exact same thing is true if your aggregate is too large & offers many state changing methods; multiple users can only modify the aggregate one at a time. By designing small aggregates that are modified in isolation in a transaction reduces concurrency collisions.

    Vaughn Vernon has done an excellent job explaining this in his 3 part article.

    However, this is just a guiding principle and there will be exceptions where more than one aggregate will need to be modified. The fact that you are considering whether the transaction/use case could be re-factored to only modify one aggregate is a good thing.

  • 相关阅读:
    图片反转效果
    css实现三角效果
    漂亮的阴影效果
    css名词解释
    偷学来的资料
    Git、GitHub、GitLab三者之间的联系以及区别
    分模块、分工程管理
    Spring AOP面向切面编程
    为什么要用存储过程,什么时候要用存储过程,存储过程的优点
    Spring扫描组件的使用详解
  • 原文地址:https://www.cnblogs.com/netfocus/p/2945450.html
Copyright © 2011-2022 走看看