Microsoft ADO .NET 4 Step by Step - p 24 pot

10 228 0
Microsoft ADO .NET 4 Step by Step - p 24 pot

Đang tải... (xem toàn văn)

Thông tin tài liệu

206 Microsoft ADO.NET 4 Step by Step Processing with a Distributed Transaction: C# Note This exercise uses the “Chapter 12 CSharp” sample project and continues from where the previous exercise in this chapter left off. 1. Open the code for the AccountTransfe r class. Locate the TransferDis tributed function. This code performs a transfer between two bank account records using a distributed transaction. The body of the routine hosts two nested using blocks. // Create the withdrawal and deposit connections. using (SqlConnection sourceLink = new SqlConnection(GetConnectionString())) { using (SqlConnection destLink = new SqlConnection(GetConnectionString())) { // Lots of database-related code here } } 2. Surround the two nested using blocks with a third outer using block. using (TransactionScope envelope = new TransactionScope()) { // The two original nested using blocks appear here } This statement block creates the TransactionScope, which is the object responsible for managing the distributed transaction. 3. Just after the “Transfer complete. Commit the transaction” comment, add the following line: envelope.Complete(); This method call commits the entire transaction. 4. Run the program. In the local-transaction sample earlier in this chapter, attempting to overdraw funds caused the transaction to fail in the first half of the two-part update: the withdrawal portion. You can force a failure in the second half of the transaction by selecting a transfer in the opposite direction and entering a negative value (with an ab- solute value that exceeds the target account) in the Transfer Amount field. Select From Savings To Checking as the transfer type and enter -1000 (negative 1000) in the Transfer Amount field—or any value that, if positive, would exceed the balance in the checking account. Select the Use A Distributed Transaction field and then click Transfer. The error that occurs triggers a rollback of the distributed transaction due to a check constraint failure in the deposit portion (the second half) of the transaction. Chapter 12 Guaranteeing Data Integrity 207 Processing with a Distributed Transaction: Visual Basic Note This exercise uses the “Chapter 12 VB” sample project and continues from where the pre- vious exercise in this chapter left off. 1. Open the code for the AccountTransfe r class. Locate the TransferDis tributed function. This code performs a transfer between two bank account records using a distributed transaction. The body of the routine hosts two nested Using blocks. ' Create the withdrawal and deposit connections. Using sourceLink As SqlConnection = New SqlConnection(GetConnectionString()) Using destLink As SqlConnection = New SqlConnection(GetConnectionString()) ' Lots of database-related code here End Using End Using 2. Surround the two nested Using blocks with a third outer Using block. Using envelope As TransactionScope = New TransactionScope() ' The two original nested using blocks appear here End Using This statement block creates the TransactionScope, which is the object responsible for managing the distributed transaction. 3. Just after the “Transfer complete. Commit the transaction” comment, add the following line: envelope.Complete() This method call commits the entire transaction. 208 Microsoft ADO.NET 4 Step by Step 4. Run the program. In the local-transaction sample earlier in this chapter, attempting to overdraw funds caused the transaction to fail in the first half of the two-part update: the withdrawal portion. You can force a failure in the second half of the transaction by selecting a transfer in the opposite direction and entering a negative value (with an ab- solute value that exceeds the target account) in the Transfer Amount field. Select From Savings To Checking as the transfer type and enter -1000 (negative 1000) in the Transfer Amount field—or any value that, if positive, would exceed the balance in the checking account. Select the Use A Distributed Transaction field, and then click Transfer. The error that occurs triggers a rollback of the distributed transaction due to a check constraint failure in the deposit portion (the second half) of the transaction. Summary This chapter introduced the SqlTransaction class and its distributed counterpart, TransactionScope. Both tools allow you to treat multiple discrete database updates as a single, undivided whole. This provides a greater level of data reliability when the risks associated with a partial data update are high. ADO.NET’s disconnected model lends itself well to the optimistic concurrency data locking model. It’s a common scenario for systems where the chance of simultaneous updates to a single record is very low. For situations where pessimistic concurrency and a more preemp- tive record locking strategy have been the norm, ADO.NET might require you to try different ways of accomplishing the same tasks. Chapter 12 Guaranteeing Data Integrity 209 Chapter 12 Quick Reference To Do This Process statements using a local transaction Open a connection to a database using SqlConnection. Call the connection object’s BeginTransaction method to obtain the transaction object. Call the necessary SQL statements for data modification, including the transaction object in each SqlCommand. Call the transaction object’s Commit method to save the changes. Roll back a local transaction Create a valid SqlConnection and process data modifica- tion statements as needed. Call the transaction object’s Rollback method. Process statements using a distributed transaction Start the MSDTC if not already running. Create an instance of TransactionScope. Process your SQL statements as needed, excluding the use of SqlTransaction. If the transaction is successful, call the TransactionScope object’s Complete method. Call the TransactionScope object’s Dispose method. Microsoft ADO.NET 4 Step by Step 211 Part III Entity Framework Chapter 13: Introducing the Entity Framework Chapter 14: Visualizing Data Models Chapter 15: Querying Data in the Framework Chapter 16: Understanding Entities Through Objects . TransactionScope object’s Dispose method. Microsoft ADO. NET 4 Step by Step 211 Part III Entity Framework Chapter 13: Introducing the Entity Framework Chapter 14: Visualizing Data Models Chapter 15:. 206 Microsoft ADO. NET 4 Step by Step Processing with a Distributed Transaction: C# Note This exercise uses the “Chapter 12 CSharp” sample project and continues from where the previous exercise. “Transfer complete. Commit the transaction” comment, add the following line: envelope.Complete() This method call commits the entire transaction. 208 Microsoft ADO. NET 4 Step by Step 4. Run the program.

Ngày đăng: 05/07/2014, 19:20

Tài liệu cùng người dùng

Tài liệu liên quan