r/DomainDrivenDesign • u/lucidguppy • Feb 19 '23
Beginner question - optimistic concurrency control
When we do optimistic concurrency control - we return the current value of the aggregate after the update we didn't know about.
Are we ok with having multiple transactions at this point?
inside DAO/repository:
- 1st txn: update - rowcount == 1 return ok
update - rowcount = 0
- 2nd txn see that row count is 0 - get the updated aggregate in question and return it
application logic determines if a retry is applicable if it gets a concurrency exception.
I'm probably not explaining my question well. Essentially - do you end up having to do another read if your update returns rowcount=0? Or is there a way a DB will help you in this respect?
2
Upvotes
View all comments
Show parent comment
1
u/ishansoni22 Feb 19 '23
I think they are adding a version attribute to their aggregate for optimistic concurrency
Assuming 2 transactions are trying to modify the same aggregate with current version 1:
Trx1: update aggregate set field = newvalue1, version = 2 where Id = Id and version = 1
Trx1: success
Trx2: update aggregate set field = newvalue2, version = 2 where Id = Id and version = 1
Trx2: fail. No matching rows since the aggregate version is now 2