Asp.net concurrent processing

Source: Internet
Author: User
ASP. NET concurrent processing

When it comes to concurrency processing, we generally have two aspects: a) server-level concurrency control; B) Program-level concurrency control.

Server-level concurrency control:

Adjust the maximum number of connections in the server application pool.

For Web servers, Dudu wrote the following articles when optimizing the server in the blog garden.

Dudu has written to allow Windows Server 2008 + IIS 7 + ASP. NET to support 0.1 million simultaneous request http://www.cnblogs.com/dudu/archive/2009/11/10/1600062.html

1. Adjust the length of the IIS 7 Application pool queue

Changed from the default 1000 to 65535.

IIS Manager> applicationpools> Advanced Settings

Queue Length: 65535

2. Adjust the appconcurrentrequestlimit settings of IIS 7

Changed from the default 5000 to 100000.

Appcmd.exe set config/section: serverruntime/appconcurrentrequestlimit: 100000

You can view this setting in % SystemRoot % \ system32 \ inetsrv \ config \ applicationhost. config.

3. Adjust processmodel> requestqueuelimit settings in machine. config.

Changed from the default 5000 to 100000.

<Configuration>
<System. Web>
<Processmodel requestqueuelimit = "100000" type = "codeph" text = "/codeph"/>

4. Modify the registry and adjust the number of TCPIP connections supported by IIS 7.

Changed from the default 5000 to 100000.

Reg add HKLM \ System \ CurrentControlSet \ Services \ HTTP \ Parameters/V maxconnections/T REG_DWORD/d 1000000

After completing the above four settings, you can support 0.1 million simultaneous requests.

The maximum number of connections can also be adjusted for the DB server for optimization.

After adjusting and optimizing the maximum number of connections, only Software and Hardware load balancing is available. Hardware Server Load balancer can be directly implemented through smart switches. It has strong processing capabilities and has nothing to do with the system. However, it is expensive and difficult to configure. It cannot distinguish the status of the practice system from the application. Therefore, hardware Server Load balancer is suitable for a large number of devices, large traffic volumes, and simple applications. Software load balancing is based on systems and applications, which can better distribute loads according to the system and application conditions. High cost effectiveness. PCL load balancing software, Linux LVS software.

Program-level concurrency control:

When two users access a page at the same time, one user may update the records that another user has deleted. Alternatively, the content of this record is modified by another user during the time when a user loads the page and clicks the delete button.

The following three concurrent control policies are available:

ØDo nothing-If the concurrent user modifies the same record, the final submitted result will take effect (default action)

ØOpen concurrency (Optimistic Concurrency)-Assume that a concurrency conflict only occurs occasionally and does not occur in most cases. In case of a conflict, simply inform the user that the changes made by the user cannot be saved, because other users have modified the same record

ØConcurrent (Pessimistic concurrency)-Assume that concurrency conflicts often occur, and users cannot tolerate being notified that their modifications cannot be saved because of others' concurrent behaviors. Then, when a user starts to edit a record and locks the record, this prevents other users from editing or deleting the record until the record is completed and submitting their own changes.

When multiple users attempt to modify data at the same time, a control mechanism should be established to prevent the modifications of one user from adversely affecting the modifications made by other users simultaneously. The system that handles this situation is called "concurrency control ".

Concurrency Control Type

Generally, there are three common methods to manage concurrency in a database:

  • Concurrent Concurrency Control-This row is unavailable to users during the period from the time the record is retrieved to the time when the record is updated in the database.
  • Open Concurrency Control-This row is unavailable to other users only when the data is actually updated. The update checks the row in the database and determines whether any changes have been made. If you try to update a modified record, a concurrency conflict occurs.
  • The last update takes effect.-This row is unavailable to other users only when the data is actually updated. However, it does not compare updates with initial records, but only writes records, which may overwrite the changes made by other users since the last refresh record.
Concurrent write

Concurrent concurrency is usually used for two purposes. First, in some cases, there are a large number of contention for the same record. The cost of storing locks on data is less than the cost of rolling back and changing when a concurrency conflict occurs.

In the case that the record cannot be changed during the transaction process, the parallel concurrency is also very useful. The inventory application is a good example. Assume that a company representative is checking inventory for a potential customer. You usually need to lock the record until the order is generated, which usually marks the item as "ordered" and removes it from the available stock. If no order is generated, the lock is released so that other users who check the inventory can obtain an accurate available inventory count.

However, concurrent control cannot be performed in the disconnected structure. The connection can only be opened for reading or updating data, so the lock cannot be kept for a long time. In addition, applications that keep the lock for a long time cannot be scaled.

Note:If the basic data source supports transactions, you can simulate concurrent transactions by updating data in the transaction. For more information, see transactions in ADO. net.

Open concurrency

In open concurrency, the lock is set and kept only when the database is accessed. These locks prevent other users from updating records at the same time. In addition to the exact Update time, data is always available. For more information, see open concurrency.

When an update attempt is made, the initial version of the changed row is compared with the existing row in the database. If the two are different, the update will fail and cause a concurrent error. At this time, you will use the created business logic to coordinate the two lines.

The last update takes effect.

When "the last update takes effect" is used, the initial data is not checked, but the update is only written to the database. Obviously, the following situations may occur:

  • User A obtains a record from the database.
  • User B obtains the same record from the database, modifies it, and writes the updated record back to the database.
  • User A modifies the "old" record and writes it back to the database.

In the above case, user a will never see the changes made by user B. If you plan to use the "Last Update effective" method of concurrency control, make sure this situation is acceptable.

Concurrency Control in ADO. NET and Visual Studio. NET

Because the data structure is based on disconnected data, ADO. NET and Visual Studio. NET use open concurrency. Therefore, you need to add business logic to solve problems with open concurrency.

If you choose to use open concurrency, you can use two common methods to determine whether the change has occurred: Version method (actual version number or date timestamp) and save all value methods.

Version Number Method

In the version number method, the record to be updated must have a column containing the date timestamp or version number. When this record is read, The timestamp or version number is saved on the client. Then, the value is partially updated.

One way to process concurrency is to update only when the value in the WHERE clause matches the value in the record. The SQL representation of this method is:

UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2WHERE DateTimeStamp = @origDateTimeStamp

Alternatively, you can use the version number for comparison:

UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2WHERE RowVersion = @origRowVersionValue

If the date and time stamp or version number match, it indicates that the record in the data storage area has not been changed, and the record can be safely updated using the new value in the dataset. If not, an error is returned. You can write code to implement this type of concurrent check in Visual Studio. NET. You must also write code to respond to any update conflicts. To ensure the accuracy of the date timestamp or version number, you need to set a trigger on the table to update the date timestamp or version number when a row is changed.

Save all value methods

An alternative to a date timestamp or version number is to retrieve copies of all fields when reading a record. The DataSet object in ADO. Net maintains two versions of each modification record: the initial version (the version that was initially read from the data source) and the modified version (indicating the user update ). When you try to write records back to the data source, the initial values in the Data row will be compared with the records in the data source. If they match, it indicates that the database records have not been changed after being read. In this case, the changed values in the dataset are successfully written to the database.

For the data adapter's four commands (delete, insert, select, and update), each command has a set of parameters. Each Command has parameters used for the initial value and the current value (or modified value.

Note:Because no initial record exists, adding a new record (INSERT command) only requires the current value; removing a record (delete command) only requires the initial value to locate the record to be deleted.

The following example shows the command text of a dataset command, which updates a typical customer table. This command is specified for dynamic SQL and open concurrency.

UPDATE Customers SET CustomerID = @currCustomerID, CompanyName = @currCompanyName, ContactName = @currContactName,ContactTitle = currContactTitle, Address = @currAddress, City = @currCity,PostalCode = @currPostalCode, Phone = @currPhone, Fax = @currFaxWHERE (CustomerID = @origCustomerID) AND (Address = @origAddress OR @origAddress IS NULL AND Address IS NULL) AND (City = @origCity OR @origCity IS NULL AND City IS NULL)AND (CompanyName = @origCompanyName OR @origCompanyName IS NULL AND CompanyName IS NULL) AND (ContactName = @origContactName OR @origContactName IS NULL AND ContactName IS NULL) AND (ContactTitle = @origContactTitle OR @origContactTitle IS NULL AND ContactTitle IS NULL)AND (Fax = @origFax OR @origFax IS NULL AND Fax IS NULL) AND (Phone = @origPhone OR @origPhone IS NULL AND Phone IS NULL) AND (PostalCode = @origPostalCode OR @origPostalCode IS NULL AND PostalCode IS NULL);SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City,PostalCode, Phone, FaxFROM Customers WHERE (CustomerID = @currCustomerID)

Note that the nine set statement parameters indicate the current value to be written to the database, while the nine where statement parameters indicate the initial values used to locate the initial record.

The first nine set statement parameters correspond to the first nine parameters in the parameter set. These parameters willSourceversionSet propertyCurrent.

The next nine where statement parameters are used for open concurrency. These placeholders correspond to the next nine parameters in the parameter set.SourceversionSet propertyOriginal.

The SELECT statement is used to refresh the dataset after an update occurs. It is generated when you set the "Refresh dataset" option in the "advanced SQL Generation Options" dialog box.

Note:The preceding SQL statements use named parameters, whileOledbdataadapterThe command uses the question mark (?) As a placeholder parameter.

By default, if you select the "open concurrency" option in the "data adapter Configuration Wizard", Visual Studio will create these parameters for you. At this time, you will add the error handling code according to your business requirements. ADO. NET provides a dbconcurrencyexception object that returns rows that violate concurrency rules. For more information, see handle concurrent errors.

When it comes to concurrency processing, we generally have two aspects: a) server-level concurrency control; B) Program-level concurrency control.

Server-level concurrency control:

Adjust the maximum number of connections in the server application pool.

For Web servers, Dudu wrote the following articles when optimizing the server in the blog garden.

Dudu has written to allow Windows Server 2008 + IIS 7 + ASP. NET to support 0.1 million simultaneous request http://www.cnblogs.com/dudu/archive/2009/11/10/1600062.html

1. Adjust the length of the IIS 7 Application pool queue

Changed from the default 1000 to 65535.

IIS Manager> applicationpools> Advanced Settings

Queue Length: 65535

2. Adjust the appconcurrentrequestlimit settings of IIS 7

Changed from the default 5000 to 100000.

Appcmd.exe set config/section: serverruntime/appconcurrentrequestlimit: 100000

You can view this setting in % SystemRoot % \ system32 \ inetsrv \ config \ applicationhost. config.

3. Adjust processmodel> requestqueuelimit settings in machine. config.

Changed from the default 5000 to 100000.

<Configuration>
<System. Web>
<Processmodel requestqueuelimit = "100000" type = "codeph" text = "/codeph"/>

4. Modify the registry and adjust the number of TCPIP connections supported by IIS 7.

Changed from the default 5000 to 100000.

Reg add HKLM \ System \ CurrentControlSet \ Services \ HTTP \ Parameters/V maxconnections/T REG_DWORD/d 1000000

After completing the above four settings, you can support 0.1 million simultaneous requests.

The maximum number of connections can also be adjusted for the DB server for optimization.

After adjusting and optimizing the maximum number of connections, only Software and Hardware load balancing is available. Hardware Server Load balancer can be directly implemented through smart switches. It has strong processing capabilities and has nothing to do with the system. However, it is expensive and difficult to configure. It cannot distinguish the status of the practice system from the application. Therefore, hardware Server Load balancer is suitable for a large number of devices, large traffic volumes, and simple applications. Software load balancing is based on systems and applications, which can better distribute loads according to the system and application conditions. High cost effectiveness. PCL load balancing software, Linux LVS software.

Program-level concurrency control:

When two users access a page at the same time, one user may update the records that another user has deleted. Alternatively, the content of this record is modified by another user during the time when a user loads the page and clicks the delete button.

The following three concurrent control policies are available:

ØDo nothing-If the concurrent user modifies the same record, the final submitted result will take effect (default action)

ØOpen concurrency (Optimistic Concurrency)-Assume that a concurrency conflict only occurs occasionally and does not occur in most cases. In case of a conflict, simply inform the user that the changes made by the user cannot be saved, because other users have modified the same record

ØConcurrent (Pessimistic concurrency)-Assume that concurrency conflicts often occur, and users cannot tolerate being notified that their modifications cannot be saved because of others' concurrent behaviors. Then, when a user starts to edit a record and locks the record, this prevents other users from editing or deleting the record until the record is completed and submitting their own changes.

When multiple users attempt to modify data at the same time, a control mechanism should be established to prevent the modifications of one user from adversely affecting the modifications made by other users simultaneously. The system that handles this situation is called "concurrency control ".

Concurrency Control Type

Generally, there are three common methods to manage concurrency in a database:

  • Concurrent Concurrency Control-This row is unavailable to users during the period from the time the record is retrieved to the time when the record is updated in the database.
  • Open Concurrency Control-This row is unavailable to other users only when the data is actually updated. The update checks the row in the database and determines whether any changes have been made. If you try to update a modified record, a concurrency conflict occurs.
  • The last update takes effect.-This row is unavailable to other users only when the data is actually updated. However, it does not compare updates with initial records, but only writes records, which may overwrite the changes made by other users since the last refresh record.
Concurrent write

Concurrent concurrency is usually used for two purposes. First, in some cases, there are a large number of contention for the same record. The cost of storing locks on data is less than the cost of rolling back and changing when a concurrency conflict occurs.

In the case that the record cannot be changed during the transaction process, the parallel concurrency is also very useful. The inventory application is a good example. Assume that a company representative is checking inventory for a potential customer. You usually need to lock the record until the order is generated, which usually marks the item as "ordered" and removes it from the available stock. If no order is generated, the lock is released so that other users who check the inventory can obtain an accurate available inventory count.

However, concurrent control cannot be performed in the disconnected structure. The connection can only be opened for reading or updating data, so the lock cannot be kept for a long time. In addition, applications that keep the lock for a long time cannot be scaled.

Note:If the basic data source supports transactions, you can simulate concurrent transactions by updating data in the transaction. For more information, see transactions in ADO. net.

Open concurrency

In open concurrency, the lock is set and kept only when the database is accessed. These locks prevent other users from updating records at the same time. In addition to the exact Update time, data is always available. For more information, see open concurrency.

When an update attempt is made, the initial version of the changed row is compared with the existing row in the database. If the two are different, the update will fail and cause a concurrent error. At this time, you will use the created business logic to coordinate the two lines.

The last update takes effect.

When "the last update takes effect" is used, the initial data is not checked, but the update is only written to the database. Obviously, the following situations may occur:

  • User A obtains a record from the database.
  • User B obtains the same record from the database, modifies it, and writes the updated record back to the database.
  • User A modifies the "old" record and writes it back to the database.

In the above case, user a will never see the changes made by user B. If you plan to use the "Last Update effective" method of concurrency control, make sure this situation is acceptable.

Concurrency Control in ADO. NET and Visual Studio. NET

Because the data structure is based on disconnected data, ADO. NET and Visual Studio. NET use open concurrency. Therefore, you need to add business logic to solve problems with open concurrency.

If you choose to use open concurrency, you can use two common methods to determine whether the change has occurred: Version method (actual version number or date timestamp) and save all value methods.

Version Number Method

In the version number method, the record to be updated must have a column containing the date timestamp or version number. When this record is read, The timestamp or version number is saved on the client. Then, the value is partially updated.

One way to process concurrency is to update only when the value in the WHERE clause matches the value in the record. The SQL representation of this method is:

UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2WHERE DateTimeStamp = @origDateTimeStamp

Alternatively, you can use the version number for comparison:

UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2WHERE RowVersion = @origRowVersionValue

If the date and time stamp or version number match, it indicates that the record in the data storage area has not been changed, and the record can be safely updated using the new value in the dataset. If not, an error is returned. You can write code to implement this type of concurrent check in Visual Studio. NET. You must also write code to respond to any update conflicts. To ensure the accuracy of the date timestamp or version number, you need to set a trigger on the table to update the date timestamp or version number when a row is changed.

Save all value methods

An alternative to a date timestamp or version number is to retrieve copies of all fields when reading a record. The DataSet object in ADO. Net maintains two versions of each modification record: the initial version (the version that was initially read from the data source) and the modified version (indicating the user update ). When you try to write records back to the data source, the initial values in the Data row will be compared with the records in the data source. If they match, it indicates that the database records have not been changed after being read. In this case, the changed values in the dataset are successfully written to the database.

For the data adapter's four commands (delete, insert, select, and update), each command has a set of parameters. Each Command has parameters used for the initial value and the current value (or modified value.

Note:Because no initial record exists, adding a new record (INSERT command) only requires the current value; removing a record (delete command) only requires the initial value to locate the record to be deleted.

The following example shows the command text of a dataset command, which updates a typical customer table. This command is specified for dynamic SQL and open concurrency.

UPDATE Customers SET CustomerID = @currCustomerID, CompanyName = @currCompanyName, ContactName = @currContactName,ContactTitle = currContactTitle, Address = @currAddress, City = @currCity,PostalCode = @currPostalCode, Phone = @currPhone, Fax = @currFaxWHERE (CustomerID = @origCustomerID) AND (Address = @origAddress OR @origAddress IS NULL AND Address IS NULL) AND (City = @origCity OR @origCity IS NULL AND City IS NULL)AND (CompanyName = @origCompanyName OR @origCompanyName IS NULL AND CompanyName IS NULL) AND (ContactName = @origContactName OR @origContactName IS NULL AND ContactName IS NULL) AND (ContactTitle = @origContactTitle OR @origContactTitle IS NULL AND ContactTitle IS NULL)AND (Fax = @origFax OR @origFax IS NULL AND Fax IS NULL) AND (Phone = @origPhone OR @origPhone IS NULL AND Phone IS NULL) AND (PostalCode = @origPostalCode OR @origPostalCode IS NULL AND PostalCode IS NULL);SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City,PostalCode, Phone, FaxFROM Customers WHERE (CustomerID = @currCustomerID)

Note that the nine set statement parameters indicate the current value to be written to the database, while the nine where statement parameters indicate the initial values used to locate the initial record.

The first nine set statement parameters correspond to the first nine parameters in the parameter set. These parameters willSourceversionSet propertyCurrent.

The next nine where statement parameters are used for open concurrency. These placeholders correspond to the next nine parameters in the parameter set.SourceversionSet propertyOriginal.

The SELECT statement is used to refresh the dataset after an update occurs. It is generated when you set the "Refresh dataset" option in the "advanced SQL Generation Options" dialog box.

Note:The preceding SQL statements use named parameters, whileOledbdataadapterThe command uses the question mark (?) As a placeholder parameter.

By default, if you select the "open concurrency" option in the "data adapter Configuration Wizard", Visual Studio will create these parameters for you. At this time, you will add the error handling code according to your business requirements. ADO. NET provides a dbconcurrencyexception object that returns rows that violate concurrency rules. For more information, see handle concurrent errors.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.