2012-06-25 27 views
8

Tôi đã thực hiện rất nhiều tìm kiếm nhưng không thể tìm thấy một dấu hiệu thẳng.Khuôn khổ thực thể: Cách đặt nhiều thủ tục được lưu trữ trong một giao dịch?

Tôi có hai thủ tục lưu trữ và cả hai đều chức năng được nhập khẩu vào đối tượng DBContext

  1. InsertA()
  2. InsertB()

Tôi muốn đặt chúng trong một giao dịch. (ví dụ: nếu InsertB() không thành công, hãy cuộn lại InsertA())

Tôi làm như thế nào? Tôi có thể chỉ cần khai báo một đối tượng TransactionScope và quấn quanh hai thủ tục được lưu trữ?

Cảm ơn

+0

Tại sao bạn không tạo thủ tục lưu trữ được gọi là hai thủ tục này trong giao dịch hoặc, nếu các thủ tục này luôn được gọi cùng nhau, kết hợp logic vào một quy trình duy nhất ngay từ đầu? –

+0

Hai quy trình này là mã kế thừa. Và cả hai đều rất lớn. – c830

+0

Được rồi, nhưng bạn vẫn có thể viết một quy trình rất nhỏ hoạt động như một trình bao bọc. –

Trả lời

13

Bạn cần phải tranh thủ hoạt động của bạn trong một phạm vi giao dịch, như sau:

using(TransactionScope tranScope = new TransactionScope()) 
{ 
    InsertA(); 
    InsertB(); 

    tranScope.Complete(); 
} 

On lỗi, phạm vi giao dịch sẽ tự động được cuộn lại. Tất nhiên, bạn vẫn cần phải xử lý các ngoại lệ và làm bất cứ điều gì ngoại lệ của bạn xử lý thiết kế dictates (đăng nhập, vv). Nhưng trừ khi bạn gọi theo cách thủ công Complete(), giao dịch được cuộn lại khi phạm vi using kết thúc.

Phạm vi giao dịch sẽ không được quảng bá cho giao dịch phân phối trừ khi bạn mở các kết nối cơ sở dữ liệu khác trong cùng phạm vi giao dịch (xem here).

Đây là quan trọng cần biết vì nếu không bạn sẽ cần phải cấu hình MSDTC trên tất cả các máy chủ của bạn tham gia vào hoạt động này (web, tầng giữa, máy chủ sql). Vì vậy, miễn là giao dịch không được quảng bá cho giao dịch được phân phối, bạn sẽ ổn.

Lưu ý: Để tinh chỉnh các tùy chọn giao dịch của bạn, chẳng hạn như thời gian chờ và mức cách ly, hãy xem this TransactionScope constructor. Mức cô lập mặc định là serializable.

Mẫu bổ sung:here.

+0

Cảm ơn câu trả lời chi tiết. – c830

4

Bạn có thể sử dụng đối tượng TransactionScope hoặc bạn có thể sử dụng phương thức SqlConnection.BeginTransaction. Hãy cẩn thận khi sử dụng TransactionScope, các giao dịch có thể được phân bổ cho các giao dịch phân tán khi gọi các thủ tục được lưu trữ trong một cơ sở dữ liệu khác. Phân phối giao dịch có thể là tài nguyên chuyên sâu.

Làm thế nào để sử dụng sqlConnection.BeginTransaction ... (http://msdn.microsoft.com/en-us/library/86773566.aspx)

private static void ExecuteSqlTransaction(string connectionString) 
{ 
    using (SqlConnection connection = new SqlConnection(connectionString)) 
    { 
     connection.Open(); 

     SqlCommand command = connection.CreateCommand(); 
     SqlTransaction transaction; 

     // Start a local transaction. 
     transaction = connection.BeginTransaction("SampleTransaction"); 

     // Must assign both transaction object and connection 
     // to Command object for a pending local transaction 
     command.Connection = connection; 
     command.Transaction = transaction; 

     try 
     { 
      command.CommandText = 
       "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"; 
      command.ExecuteNonQuery(); 
      command.CommandText = 
       "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"; 
      command.ExecuteNonQuery(); 

      // Attempt to commit the transaction. 
      transaction.Commit(); 
      Console.WriteLine("Both records are written to database."); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); 
      Console.WriteLine(" Message: {0}", ex.Message); 

      // Attempt to roll back the transaction. 
      try 
      { 
       transaction.Rollback(); 
      } 
      catch (Exception ex2) 
      { 
       // This catch block will handle any errors that may have occurred 
       // on the server that would cause the rollback to fail, such as 
       // a closed connection. 
       Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); 
       Console.WriteLine(" Message: {0}", ex2.Message); 
      } 
     } 
    } 
} 

Làm thế nào để sử dụng TransactionScope ... (http : //msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx)

// This function takes arguments for 2 connection strings and commands to create a transaction 
// involving two SQL Servers. It returns a value > 0 if the transaction is committed, 0 if the 
// transaction is rolled back. To test this code, you can connect to two different databases 
// on the same server by altering the connection string, or to another 3rd party RDBMS by 
// altering the code in the connection2 code block. 
static public int CreateTransactionScope(
    string connectString1, string connectString2, 
    string commandText1, string commandText2) 
{ 
    // Initialize the return value to zero and create a StringWriter to display results. 
    int returnValue = 0; 
    System.IO.StringWriter writer = new System.IO.StringWriter(); 

    try 
    { 
     // Create the TransactionScope to execute the commands, guaranteeing 
     // that both commands can commit or roll back as a single unit of work. 
     using (TransactionScope scope = new TransactionScope()) 
     { 
      using (SqlConnection connection1 = new SqlConnection(connectString1)) 
      { 
       // Opening the connection automatically enlists it in the 
       // TransactionScope as a lightweight transaction. 
       connection1.Open(); 

       // Create the SqlCommand object and execute the first command. 
       SqlCommand command1 = new SqlCommand(commandText1, connection1); 
       returnValue = command1.ExecuteNonQuery(); 
       writer.WriteLine("Rows to be affected by command1: {0}", returnValue); 

       // If you get here, this means that command1 succeeded. By nesting 
       // the using block for connection2 inside that of connection1, you 
       // conserve server and network resources as connection2 is opened 
       // only when there is a chance that the transaction can commit. 
       using (SqlConnection connection2 = new SqlConnection(connectString2)) 
       { 
        // The transaction is escalated to a full distributed 
        // transaction when connection2 is opened. 
        connection2.Open(); 

        // Execute the second command in the second database. 
        returnValue = 0; 
        SqlCommand command2 = new SqlCommand(commandText2, connection2); 
        returnValue = command2.ExecuteNonQuery(); 
        writer.WriteLine("Rows to be affected by command2: {0}", returnValue); 
       } 
      } 

      // The Complete method commits the transaction. If an exception has been thrown, 
      // Complete is not called and the transaction is rolled back. 
      scope.Complete(); 

     } 

    } 
    catch (TransactionAbortedException ex) 
    { 
     writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message); 
    } 
    catch (ApplicationException ex) 
    { 
     writer.WriteLine("ApplicationException Message: {0}", ex.Message); 
    } 

    // Display messages. 
    Console.WriteLine(writer.ToString()); 

    return returnValue; 
} 
+0

Nhưng đó là một toàn bộ rất nhiều mã cho một cái gì đó có thể được thực hiện chỉ với một vài dòng mã, sử dụng hỗ trợ giao dịch thích hợp. –

Các vấn đề liên quan