Pass transaction for reuse

Whats the best way for these method calls to use the transaction defined in the outer scope? If one of the method calls fails I want the other changes to be undone.


        using var transaction = Db.BeginTransaction();
        try
        {
            await DeleteOrphanedMappingsAsync(sourceDatabaseSchema);
            await UpdateChangedMappingsAsync(sourceDatabaseSchema);
            await SaveMissingMappingsAsync(sourceDatabaseSchema);

            transaction.Commit();
        }
        catch
        {
            transaction.Rollback();
            throw;
        }

See OrmLite docs on Database Transactions, i.e. you should be using Db.OpenTransaction().

You’re only going to rollback changes made to the database with the same Db connection which I’m not seeing anywhere. Otherwise an Exception calling transaction.Rollback(); will rollback Db changes made within the scope of the transaction.

1 Like