[CSHARP-3220] Migrating from 2.10.3 to 2.11.2 made transactions to fail on a local standalone server Created: 07/Oct/20  Updated: 27/Oct/23  Resolved: 02/Nov/20

Status: Closed
Project: C# Driver
Component/s: API
Affects Version/s: 2.11.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Jose Fernandez Alameda Assignee: Dmitry Lukyanov (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OSX (most likely all)



 Description   

When we were using the version 2.10.3 we could create simple transactions, but after upgrading to the latest version the client would throw the following error when starting the transaction: 'Standalone servers do not support transaction mongo'.

I am not sure if this is an intended change or a bug.

 

using (var session = mongoClient.StartSession())
            {
                try
                {
                    session.StartTransaction();
 
                    if (insert)
                    {
                        await collection.InsertOneAsync(family);
                        session.CommitTransaction();
                        return true;
                    }
                    else    
                    {
                        var filter = Builders<FamilyModel>.Filter.Eq(x => x.Id, family.Id);
                        var result = await collection.ReplaceOneAsync(filter, family);
                        session.CommitTransaction();
                        return result.ModifiedCount > 0;
                    }
                }
         }

 



 Comments   
Comment by Jeffrey Yemin [ 07/Oct/20 ]

Hi jose@ally.se

This was an intended change. Standalone servers do not actually support transactions.

Looking at your sample code, it seems you are not using transactions correctly, which may be why this failed silently on standalones. You have to pass the session as the first parameter to every CRUD method that you want to participate in the transaction.

           using (var session = mongoClient.StartSession())
            {
                try
                {
                    session.StartTransaction();
 
                    if (insert)
                    {
                        await collection.InsertOneAsync(session, family);
                        session.CommitTransaction();
                        return true;
                    }
                    else    
                    {
                        var filter = Builders<FamilyModel>.Filter.Eq(x => x.Id, family.Id);
                        var result = await collection.ReplaceOneAsync(session, filter, family);
                        session.CommitTransaction();
                        return result.ModifiedCount > 0;
                    }
                }
         }

Generated at Wed Feb 07 21:44:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.