Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB shared transaction API exposes users to data loss #330

Open
mauroservienti opened this issue Nov 11, 2019 · 0 comments
Open

MongoDB shared transaction API exposes users to data loss #330

mauroservienti opened this issue Nov 11, 2019 · 0 comments

Comments

@mauroservienti
Copy link
Member

mauroservienti commented Nov 11, 2019

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.

@WilliamBZA WilliamBZA transferred this issue from another repository Nov 8, 2021
@kbaley kbaley transferred this issue from another repository Jul 20, 2022
@kbaley kbaley transferred this issue from another repository Jul 27, 2022
@kbaley kbaley transferred this issue from Particular/NServiceBus Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants