Optimistic Concurrency and Database Transactions in VB.NET

Make QR Code JIS X 0510 in VB.NET Optimistic Concurrency and Database Transactions

Optimistic Concurrency and Database Transactions
Making QR In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create Quick Response Code image in Visual Studio .NET applications.
www.OnBarcode.com
QR Code Decoder In VB.NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
What we have done to this point works well only if we are the only ones working on a set of data. If an application uses a LINQ query to retrieve data from a table already accessed by another user and then it tries to modify some rows, it could get an exception. This is because LINQ to SQL uses optimistic concurrency. LINQ to SQL tracks changes to our objects after they are retrieved by a query and filled by a For Each statement or a call to a caching method such as ToList(). If another user has retrieved a row from the database and already changed its contents, when we try to submit our changes we ll get an exception. In fact, LINQ s change-tracking service discovers that the row has been changed from its original state (as of when we retrieved it) and raises the
Code 3 Of 9 Printer In VB.NET
Using Barcode encoder for VS .NET Control to generate, create Code 39 image in VS .NET applications.
www.OnBarcode.com
1D Maker In Visual Basic .NET
Using Barcode creator for VS .NET Control to generate, create 1D image in VS .NET applications.
www.OnBarcode.com
firstPress: LINQ to ADO.NET
Generate Matrix In VB.NET
Using Barcode maker for .NET Control to generate, create Matrix Barcode image in .NET framework applications.
www.OnBarcode.com
GTIN - 12 Creator In VB.NET
Using Barcode maker for .NET framework Control to generate, create GS1 - 12 image in .NET framework applications.
www.OnBarcode.com
exception. To test the optimistic concurrency feature, write and execute the code in Listing 2-13.
PDF-417 2d Barcode Generator In Visual Basic .NET
Using Barcode printer for VS .NET Control to generate, create PDF 417 image in VS .NET applications.
www.OnBarcode.com
USD-3 Printer In VB.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create Code 93 image in .NET framework applications.
www.OnBarcode.com
Listing 2-13. Testing the Optimistic Concurrency Feature
Create Quick Response Code In Java
Using Barcode generator for Java Control to generate, create QR Code ISO/IEC18004 image in Java applications.
www.OnBarcode.com
Create QR Code JIS X 0510 In Java
Using Barcode creation for Android Control to generate, create Quick Response Code image in Android applications.
www.OnBarcode.com
Public Class OptimisticConcurrencyExample Public Shared Function whereCondition(ByVal p As Person) As Boolean Return p.ID = 1 End Function Sub Listing2_13() Dim people = New PeopleDataContext() Dim predicateDelegate As New Func(Of Person, Boolean)(AddressOf whereCondition) Dim person = people.People.Single(predicateDelegate) person.FirstName = "Optimistic" person.LastName = "Concurrency" Console.ReadLine() people.SubmitChanges() End Sub End Class
EAN-13 Maker In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create EAN-13 image in ASP.NET applications.
www.OnBarcode.com
Encoding UPC-A Supplement 5 In Objective-C
Using Barcode printer for iPad Control to generate, create GS1 - 12 image in iPad applications.
www.OnBarcode.com
The code simply retrieves the Person row whose identifier is equal to 1, changes some attributes, and submits the changes after a key is pressed. This allows us to execute another instance of the same application that retrieves the same row before we press a key in the other instance of the application. Pressing a key in the first application will modify the row, whereas pressing a key in the second application will cause the exception shown in Figure 2-9.
Generating Code 128 In .NET Framework
Using Barcode drawer for Reporting Service Control to generate, create Code-128 image in Reporting Service applications.
www.OnBarcode.com
EAN13 Decoder In .NET Framework
Using Barcode reader for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Figure 2-9. The exception thrown by LINQ when the optimistic concurrency is violated
Make UCC-128 In C#
Using Barcode printer for .NET Control to generate, create UCC-128 image in .NET applications.
www.OnBarcode.com
Make PDF 417 In C#.NET
Using Barcode generator for .NET framework Control to generate, create PDF-417 2d barcode image in .NET applications.
www.OnBarcode.com
Concurrency is managed by the DataContext class. When we call SubmitChanges(), the data context creates a local transaction using the ReadCommit isolation level; that is, using optimistic concurrency.
Making Code-39 In None
Using Barcode generator for Excel Control to generate, create Code 3/9 image in Excel applications.
www.OnBarcode.com
GS1-128 Creation In Java
Using Barcode drawer for Java Control to generate, create GS1 128 image in Java applications.
www.OnBarcode.com
firstPress: LINQ to ADO.NET
Barcode Reader In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Make EAN / UCC - 13 In None
Using Barcode generator for Microsoft Word Control to generate, create European Article Number 13 image in Word applications.
www.OnBarcode.com
This is the default. When we decorate the properties of the entity classes we can indicate which of them participate in optimistic concurrency. Using the UpdateCheck property of the Column attribute we can specify Never and LINQ will ignore the column during concurrency checking.
<Column(Name:="FirstName", Storage:="_firstName", _ DbType:="nvarchar NOT NULL", _ UpdateCheck:=UpdateCheck.Never)> _ Public Property FirstName() As String Get Return Me._firstName End Get Set(ByVal value As String) Me._firstName = value End Set End Property <Column(Name:="LastName", Storage:="_lastName", _ DbType:="nvarchar NOT NULL", _ UpdateCheck:=UpdateCheck.Never)> _ Public Property LastName() As String Get Return Me._lastName End Get Set(ByVal value As String) Me._lastName = value End Set End Property
After we modify the Person entity class as shown, the code in Listing 2-13 will work without exceptions because the two columns don t participate in optimistic concurrency checking. LINQ to SQL provides an advanced technique to manage update conflicts. When we call SubmitChanges(), we can specify a ConflictMode enum value to change the way optimistic concurrency is managed by LINQ. Using ConflictMode.ContinueOnConflict the OptimisticConcurrencyException is filled with some attributes that we can use to personalize the way optimistic concurrency is managed. Using a Try statement we can catch the OptimisticConcurrencyException then use its Resolve() method to specify one of three values that in turn specify three different way to resolve update conflicts:
KeepChanges: The old values contained in the object are refreshed with the new values changed by the other client. A new SubmitChanges() call is executed automatically and
the current values within the object are used to update the row.
firstPress: LINQ to ADO.NET
KeepCurrentValues: This rolls back each change made by the other client to the original database state. A new SubmitChanges() call is executed automatically and the current
values within the object are used to update the row.
OverwriteCurrentValues: The object replaces its data with the new state of the row in the
database. The code in Listing 2-14 calls the Resolve method with KeepChanges after an optimistic concurrency exception has been detected.
Copyright © OnBarcode.com . All rights reserved.