You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MongoDB API exposes users to data loss due to the way the shared transaction API is designed. Take for example the following snippet:
public Task Handle(MyMessage message, IMessageHandlerContext context)
{
var session = context.SynchronizedStorageSession.GetClientSession();
var collection = session.Client.GetDatabase("mydatabase").GetCollection<MyBusinessObject>("mycollection");
return collection.InsertOneAsync(session, new MyBusinessObject());
}
The last line is really easy to get wrong:
return collection.InsertOneAsync(session, new MyBusinessObject());
The InsertOneAsync method has many overloads one of which is very similar to the above one but doesn't take a session parameter as the first argument. If a customer writes the aforementioned snippet like follows:
public Task Handle(MyMessage message, IMessageHandlerContext context)
{
var session = context.SynchronizedStorageSession.GetClientSession();
var collection = session.Client.GetDatabase("mydatabase").GetCollection<MyBusinessObject>("mycollection");
return collection.InsertOneAsync(new MyBusinessObject());
}
it compiles fine and it might also work, however the MyBusinessObject save attempt is not included in the shared transaction created by the endpoint.
It would be nice to add to the MongoDB package an analyzer to detect and warn users in case such a mistake is done.
The text was updated successfully, but these errors were encountered:
MongoDB API exposes users to data loss due to the way the shared transaction API is designed. Take for example the following snippet:
The last line is really easy to get wrong:
The
InsertOneAsync
method has many overloads one of which is very similar to the above one but doesn't take a session parameter as the first argument. If a customer writes the aforementioned snippet like follows:it compiles fine and it might also work, however the
MyBusinessObject
save attempt is not included in the shared transaction created by the endpoint.It would be nice to add to the MongoDB package an analyzer to detect and warn users in case such a mistake is done.
The text was updated successfully, but these errors were encountered: