While doing certain modification in the database some time you need to lock the data so that no one can else perform modification in that data. There are two commonly known approaches for locking database they are optimistic locking and pessimistic locking. Both these approaches are used to maintain concurrency in the database. Pessimistic concurrency locking is done at rows of the data source to prevent users from modifying data in a way that affects other users. In a pessimistic model, when a user performs an action that causes a lock to be applied, no one else can perform action until unless owner releases that lock. But this is not case with optimistic currency model. In optimistic concurrency model user does not lock row while reading it, while user only locks the row while updating changes to the database.
In .NET we use DataSet object for modifying changes in the database. The DataSet object uses optimistic concurrency model with the help of DataAdaptor. The DataSet object is designed to encourage the use of optimistic concurrency for long-running activities such as when you are working in distributed environment.
In real time execution DataSet maintains the versions of data that means if anyone modify any data in the DataSet then it get maintain in the dataset as old version and new version. While updating modified data in the database if any of the concurrency conflict occur it raises Exception, which sets DataRow’s HasError Boolean value. This we can easily handle with DataAdaptor event and with our own programming logic. A simple code sample, which explains you how can you manage, concurrency control in .NET environment
string connectionString = "......................."; SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter myAdaptor = new SqlDataAdapter("SELECT Name, City FROM Employee ORDER BY EmpID", myConnection);
// Add the RowUpdated event handler. myAdaptor.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
DataSet supplierData = new DataSet(); myAdaptor.Fill(supplierData, "Supplier");
// Modify the DataSet contents. …………………………………………….
myAdaptor.Update(supplierData, "Supplier");
foreach (DataRow myRow in supplierData.Tables["Supplier"].Rows) { if (myRow.HasErrors) Console.WriteLine(myRow[0] + "\n" + myRow.RowError); }
protected static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs args) { if (args.RecordsAffected == 0) { args.Row.RowError = "Optimistic Concurrency Violation Encountered"; args.Status = UpdateStatus.SkipCurrentRow;
|
| Author: critic 29 Nov 2004 | Member Level: Bronze Points : 0 |
This article already exists here : http://www.c-sharpcorner.com/Code/2002/Aug/TransactionsNConcurr.asp.
Please let us know if you own this article.
|
| Author: Nikki Kaushik 16 Dec 2004 | Member Level: Bronze Points : 0 |
it is a good article. it helps me a lot but i want to know how to maintain Concurrency control for SqlDataReader in ASP.NET with Vb.net
Thanks a lot.
|
| Author: Nikki Kaushik 16 Dec 2004 | Member Level: Bronze Points : 0 |
it is a good article. it helps me a lot but i want to know how to maintain Concurrency control for SqlDataReader in ASP.NET with Vb.net
Thanks a lot.
|