1

I'm having a little trouble getting a multi delete transaction working using SubSonic in an ASP.NET/SQL Server 2005. It seems it always makes the change(s) in the database even without a call the complete method on the transactionscope object?

I've been reading through the posts regarding this and tried various alternatives (switching the order of my using statements), using DTC, not using DTC etc but no joy so far.

I'm going to assume it's my code that's the problem but I can't spot the issue - is anyone able to help? I'm using SubSonic 2.2. Code sample below:

using (TransactionScope ts = new TransactionScope())
            {
                using (SharedDbConnectionScope sts = new SharedDbConnectionScope())
                {
                    foreach (RelatedAsset raAsset in relAssets)
                    {
                        // grab the asset id:
                        Guid assetId = new Select(RelatedAssetLink.AssetIdColumn)
                            .From<RelatedAssetLink>()
                            .Where(RelatedAssetLink.RelatedAssetIdColumn).IsEqualTo(raAsset.RelatedAssetId).ExecuteScalar<Guid>();

                        // step 1 - delete the related asset:
                        new Delete().From<RelatedAsset>().Where(RelatedAsset.RelatedAssetIdColumn).IsEqualTo(raAsset.RelatedAssetId).Execute();

                        // more deletion steps...
                    }

                    // complete the transaction:
                    ts.Complete();
                }
            }
Knoxy
  • 31
  • 3

1 Answers1

1

The order of your using statements is correct (I remember the order myself with this trick: The Connection needs to know about the transaction while it is created, and it does that by checking `System.Transactions.Transaction.Current).

One hint: you don't need to use double brackets. And you don't need a reference to the SharedDbConnectionScope(). This looks far more readable.

using (var ts = new TransactionScope())
using (new SharedDbConnectionScope())
{
    // some db stuff

    ts.Complete();
}

Anyway, I don't see, why this shouldn't work. If the problem would be related to the MSDTC, an exception would occur.

I only could imagine, that there is a problem in the SqlServer 2005 Configuration but I am not a SqlServer expert.

Maybe you should try some demo code to verify that transactions work:

using (var conn = new SqlConnection("your connection String");
{
    conn.Open();
    var tx = conn.BeginTransaction();

    using (var cmd = new SqlCommand(conn)
        cmd.ExecuteScalar("DELETE FROM table WHERE id = 1");

    using (var cmd2 = new SqlCommand(conn)
        cmd2.ExecuteScalar("DELETE FROM table WHERE id = 2");


    tx.Commit();
}

And subsonic supports native transactions without using TransactionScope: http://subsonicproject.com/docs/BatchQuery

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189