Click here for Vacation Photos

Archive for the 'C# / VS' Category

Concurrency Conflict Facts

Saturday, February 9th, 2008

Concurrency is the process of ensuring that changes to a database take place in an orderly manner so that changes made by one user do not conflict with changes made by another user. A concurrency conflict occurs when multiple users attempt to modify data at the same time.

There are two methods you can use to manage database concurrency: pessimistic concurrency and optimistic concurrency.

Concurrency Method Description
Pessimistic With pessimistic concurrency, the data source is locked, allowing only one user access at a time. Pessimistic concurrency eliminates concurrency errors because the data source remains locked for all read and write operations. Other users cannot make changes until the lock is released.Pessimistic concurrency uses the connected data access model. For this reason, it is not very scalable and does not allow simultaneous data access. Use pessimistic concurrency when:

  • There is high data contention.
  • The cost of resolving concurrency conflicts is high.
  • Multiple update actions must be performed in a set. By locking the data source, you can help ensure that all actions can complete without a conflict.
  • The lock times will be short. Typically, you want to lock the data source only for quick, programmatic write operations. Do not lock the database for interactive read operations.
Optimistic With optimistic concurrency, locks are only placed on the data source during update operations. Using optimistic concurrency conserves server connections and allows multiple user access to the data source. However, it also requires that checks are performed to detect concurrency conflicts.Concurrency errors happen in a disconnected data access model because the client system works with a local copy of the data and because multiple users can modify data at the same time. The following example describes when the concurrency conflict would occur:

  1. You fill a DataSet from the data source.
  2. You make changes to the DataSet.
  3. Another user updates the same data on the data source.
  4. You attempt to update the data source. A conflict occurs because the data you are updating has already been changed.

When you configure a DataAdapter using the Wizard, selecting the Use optimistic concurrency option will turn on concurrency conflict detection. With optimistic concurrency enabled:

  • Update commands in the DataAdapter are modified to check for conflicts before the update is performed.
  • If no conflicts exist, changes are made to the data source.
  • If a conflict occurs, the command will throw the System.Data.DBConcurrencyException exception and changes will not be made to the data source.
  • By catching this exception, you can add code to respond to the concurrency conflict.

Note: You can also write your own code to manually detect conflicts before updating the data source. However, using the DataAdaper configuration Wizard automates the process.

There are four general approaches you can take to handle the concurrency conflicts.

Resolution Method Description Implementation
Last in Wins Changes are made to the data source regardless of any other changes that might have been made.
  • Create the DataAdapter with Use optimistic concurrency unchecked.
  • Call the Update method to update the data source.
  • Do not catch DBConcurrencyException (conflicts will not be detected and the exception will not be thrown).
Dead Heat or Stalemate Concurrency conflicts are detected, and the data source is not modified when a conflict occurs.
Keep your changes in the DataSet.
  • Create the DataAdapter with Use optimistic concurrency checked.
  • Call the Update method to update the data source.
  • Catch DBConcurrencyException to identify any conflicts.
Don’t Force and Revert Concurrency conflicts are detected, and the data source is not modified when a conflict occurs.
Revert the DataSet to the original (reject your changes, but do not get the changes from the data source).
  • Create the DataAdapter with Use optimistic concurrency checked.
  • Call the Update method to update the data source.
  • Catch DBConcurrencyException to identify any conflicts.
  • Call the DataSet.RejectChanges method to revert to the original.
Don’t Force and Refresh Concurrency conflicts are detected, and the data source is not modified when a conflict occurs.
Refresh the DataSet (get the changes from the data source).
  • Create the DataAdapter with Use optimistic concurrency checked.
  • Call the Update method to update the data source.
  • Catch DBConcurrencyException to identify any conflicts.
  • Call the DataSet.Clear method, then refill the DataSet using the DataAdapter.Fill method.

Note: In every case when a conflict is detected, you should add code to let the user know what error occurred. You can also add code that analyzes the conflict and performs additional resolution steps. Read the Row property of the exception arguments to identify the exact location of the conflict and respond accordingly.

Pizza Programming

Saturday, February 9th, 2008

Crystal reports databind

Monday, February 4th, 2008

WPF

Wednesday, January 30th, 2008

WebApp Tracing

Saturday, January 26th, 2008

Protected: Debugging Facts

Saturday, January 26th, 2008

Debugging Web Appz

Saturday, January 26th, 2008

Protected: Testing Facts

Saturday, January 26th, 2008

Rebooting IIS

Saturday, January 19th, 2008

Event Viewer

Saturday, January 19th, 2008

Sending Email via ASP.Net

Friday, January 18th, 2008

Virtual SMTP

Thursday, January 17th, 2008