Closed edgarfelizmenio closed 1 year ago
Regarding concurrency, the Revisions feature may help: https://roman-right.github.io/beanie/tutorial/revision/
This issue is stale because it has been open 30 days with no activity.
This issue was closed because it has been stalled for 14 days with no activity.
Hi,
We are trying to implement an "accession number" field called
acc
in our Beanie Documents. Sinceacc
is a counter that follows a specific format, we decided to make a new field instead of using MongoDB's ObjectID.Implementation-wise, we are still deciding which approach to follow, since we are not entirely sure with Beanie's behaviour. Here is an example to illustrate:
The
Record
object is the Beanie Document that we are saving to the database, whilenext_acc_number
is the next accession number that will be assigned to a record, and is incremented whenever a new Document is saved to the database. We are avoiding to use Pydantic'sdefault_factory
since we only want to assign the accession number by the time the record will be saved to the database.Currently here are our ideas:
Assign and increment as a before insert event:
The problem that we see here is when the Insert operation fails for whatever reason, the accession number has already incremented when it shouldn't.
Assign accession number as a before insert event, increment accession number as an after insert event:
Here, my concern is more on concurrency. Is it possible that two different records have the same accession number assigned (e.g., before_event on second record is executed before the after_event of the first record)? I have checked the source code on how the events are executed. Since the wrapper function is async, can we make an atomic operation (before, action, after) if we simply use
await
(e.g.,await record.save()
) or does our use case call for the use of transactions (i.e., no events, enclose assign-save-increment in a transaction using the AsyncIOMotorClient)?