Operations on persistent classes in a database are always performed in the context of a transaction. A transaction is an ordered sequence of operations that transforms a database from one state of consistency to another state of consistency. The data in Telerik Data Access database is maintained in a transactional consistent state according to the standard definition of ACID transactions. Telerik Data Access assures that changes to the values of persistent instances are consistent with changes to other values in the same instance. Telerik Data Access supports several different approaches for optimistic concurrency control - using Timestamp column, DateTime column, Version Number or Checking for any changes. For more information, refer to the Optimistic Concurrency Control topic.
In this topic, you will learn how to check for data conflicts when you update data by using an OpenAccessLinqDataSource control. It will be demonstrated how to use a Timestamp column with the OpenAccessLinqDataSource control for concurrency control. The simplest mechanism for detecting that anything in a database row has changed is to use a timestamp (also known as rowversion) field. A timestamp is a binary field that is automatically updated whenever changes are made to any columns in the row. Many databases have a specific data type that is used for this. SQL Server uses the timestamp data type. The OpenAccessLinqDataSource control stores the timestamp value in the view state. When the user updates data, the OpenAccessLinqDataSource control checks the timestamp value against the current timestamp value in the database. If the data source control detects that the value in the timestamp column has changed, the control does not update the record. Instead, a new OptimisticVerificationException will be thrown.
This section shows you how to configure an OpenAccessLinqDataSource control to use a Timestamp column for concurrency control.
The first step is to create a database table (e.g. Categories) that contains a timestamp column.
It is supposed that you have included the table in your domain model.
- Configure your OpenAccessLinqDataSource control to use the table with the Timestamp column.
Connect the OpenAccessLinqDataSource control to a data-bound control as shown in the Getting Started with OpenAccessLinqDataSource topic.
To see how the timestamp column prevents updates when the data has changed, you could perform the following test:
- Run your page in the browser.
- Select a record and then click the Edit link (button). Make some modifications, but do not update the record yet.
- In Visual Studio, open the Server Explorer pane. Right-click the underlying table and click Show Table Data.
- Change a value in the same record that you opened in the Web browser. Press Enter to write the change to the database. The timestamp column for the records is updated automatically.
- Go back to your page in the Web browser and update the record by clicking the Update link.
- You should receive an exception saying "Concurrent update detected".
If you want to perform an action when an OptimisticVerificationException is thrown, you need to handle the Updating event of the OpenAccessLinqDataSource control.
The OpenAccessLinqDataSource control exposes the StoreOriginalValuesInViewState property. It specifies whether to store original data values in ViewState or not. This property is used for conflict detection. The default value for the StoreOriginalValuesInViewState property is True, which means that the concurrency control is enabled by default. To disable the concurrency control, you need to set the StoreOriginalValuesInViewState property of the OpenAccessLinqDataSource control to False.