これが私のトランザクション スコープ ソース コードの現在のアーキテクチャです。3 番目の挿入は .NET 例外 (SQL 例外ではない) をスローし、前の 2 つの挿入ステートメントをロールバックしていません。私が間違っていることは何ですか?
編集: insert2 と insert3 から try/catch を削除しました。また、insert1 の try/catch から例外処理ユーティリティを削除し、"throw ex" を配置しました。それでもトランザクションはロールバックされません。
編集 2: Insert3 メソッドに try/catch バックを追加し、catch ステートメントに「スロー」を追加しました。それでもトランザクションはロールバックされません。
更新:私が受け取ったフィードバックに基づいて、「SqlHelper」クラスは SqlConnection オブジェクトを使用してデータベースへの接続を確立し、次に SqlCommand オブジェクトを作成し、CommandType プロパティを「StoredProcedure」に設定して、SqlCommand の ExecuteNonQuery メソッドを呼び出します。
また、Transaction Binding=Explicit Unbind を現在の接続文字列に追加しませんでした。次のテストで追加します。
public void InsertStuff()
{
try
{
using(TransactionScope ts = new TransactionScope())
{
//perform insert 1
using(SqlHelper sh = new SqlHelper())
{
SqlParameter[] sp = { /* create parameters for first insert */ };
sh.Insert("MyInsert1", sp);
}
//perform insert 2
this.Insert2();
//perform insert 3 - breaks here!!!!!
this.Insert3();
ts.Complete();
}
}
catch(Exception ex)
{
throw ex;
}
}
public void Insert2()
{
//perform insert 2
using(SqlHelper sh = new SqlHelper())
{
SqlParameter[] sp = { /* create parameters for second insert */ };
sh.Insert("MyInsert2", sp);
}
}
public void Insert3()
{
//perform insert 3
using(SqlHelper sh = new SqlHelper())
{
SqlParameter[] sp = { /*create parameters for third insert */ };
sh.Insert("MyInsert3", sp);
}
}