Skip to content

MongoDB shared transaction API exposes users to data loss #330

@mauroservienti

Description

@mauroservienti

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions