asp.net concurrent processing __.net

Source: Internet
Author: User
Tags date time stamp error handling
ASP. NET concurrency processing

When it comes to concurrent processing, we typically divide it into two aspects: a server-level concurrency control, and B-program-level concurrency control.

concurrency control at the server level:

Ø Adjusts the maximum number of connections in the server application pool.

For a Web server, Dudu wrote the following articles while optimizing the server for the blog park.

Dudu wrote to let Windows Server 2008+iis 7+asp. NET supports 100,000 simultaneous request http://www.cnblogs.com/dudu/archive/2009/11/10/1600062.html

1. Adjust the IIS 7 application pool Queue Length

Change from the original default of 1000 to 65535.

IIS Manager > ApplicationPools > Advanced Settings

Queue length:65535

2. Adjust the Appconcurrentrequestlimit settings for IIS 7

Change from the original default of 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 the setting of Processmodel>requestqueuelimit in Machine.config

Change from the original default of 5000 to 100000.

<configuration>
<system.web>
<processmodel requestqueuelimit= "100000"/>

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

Change from the original default of 5000 to 100000.

REG ADD hklm/system/currentcontrolset/services/http/parameters/v maxconnections/t reg_dword/d 1000000

By completing the 4 settings above, you can support 100,000 simultaneous requests

Ø to the DB server can also adjust the maximum number of connections to do optimization.

After tuning the maximum number of connections, only the hardware and software load is balanced. The hardware load balancing can be realized directly through intelligent switch, and it has no relation with system, but the price is expensive and the configuration is difficult, which can not distinguish the state of practice system and application. So the hardware load balance applies to a large number of devices, large access, simple application. Software load balancing is based on system and application, and can better distribute load according to the condition of system and application. Cost effective. PCL load Balancing software, the LVS software under Linux.

Concurrent control at the program level:

When two users access a page at the same time, a user may update a record of what another user has deleted. Alternatively, when a user loads the page and clicks the Delete button, another user modifies the contents of the record.

The following three concurrency control policies are available to choose from:

Ø do nothing – if the concurrent user modifies the same record, let the last submitted result take effect (default behavior)

Ø optimistic concurrency (optimistic concurrency) -Assuming concurrency conflicts occur only occasionally, most of the time does not occur; So, when a conflict occurs, simply tell the user, The changes he made could not be saved because the other user has modified the same record

Ø pessimistic concurrency (pessimistic concurrency) – Assuming concurrency conflicts occur frequently, and users cannot tolerate the fact that their changes cannot be saved because of concurrent behavior by others; When a user starts editing a record, locks the record, preventing other users from editing or deleting the record until he completes and submits his own changes

When multiple users try to modify data at the same time, a control mechanism is needed to prevent a user's modification from adversely affecting changes made by other users who operate concurrently. The system that handles this situation is called concurrency control. types of concurrency control

In general, there are three common ways to manage concurrency in a database: pessimistic concurrency control -The row is not available to users during the time from which the record was fetched until it was updated in the database. optimistic concurrency control -The row is not available to other users until the data is actually updated. The update checks the row in the database and determines whether any changes have been made. If you attempt to update a record that has changed, you will cause a concurrency violation. The last update takes effect -the row is not available to other users until the data is actually updated. However, the update is not compared to the initial record, but only the record is written, which may overwrite changes made by other users since the last time the record was refreshed. Pessimistic concurrency

Pessimistic concurrency is typically used for two purposes. First, in some cases, there is a large amount of contention for the same record. The cost of placing a lock on the data is less than the cost of rolling back the change when a concurrent conflict occurs.

Pessimistic concurrency is also useful in situations where a record is not appropriate to change during a transaction. The inventory application is a good example. Assume that a company representative is checking inventory for a potential customer. You typically lock records until an order is generated, which typically marks the item as ordered and removes it from available inventory. If an order is not generated, the lock is freed so that other users who check inventory get an accurate count of available inventory.

However, pessimistic concurrency control cannot be performed in a disconnected structure. The connection is open only enough to read or update data, so locks cannot be kept for long periods of time. In addition, applications that retain locks for a long time will not be able to scale. Note If the underlying data source supports transactions, you can simulate pessimistic concurrency by updating the data in the transaction. For more information, see Transactions in Ado.net. optimistic concurrency

In optimistic concurrency, locks are set and maintained only when the database is accessed. These locks will prevent other users from updating records at the same time. Data is always available in addition to the exact time of the update. For more information, see optimistic concurrency.

When an attempt is made to update, the original version of the changed row is compared to the existing rows in the database. If they are different, the update fails and a concurrency error is raised. At this point, you will coordinate the two lines by using the business logic that you created. the last update takes effect

When the last update takes effect, the initial data is not checked, but only the update is written to the database. Obviously, the following situation may occur: User A obtains a record from the database. User B gets the same record from the database, modifies it, and then writes the updated record back to the database. User A modifies the "old" record and writes it back to the database.

In the above scenario, 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 that this is acceptable. concurrency control in ado.net and Visual Studio. NET

Ado.net and Visual Studio. NET use optimistic concurrency because the data structure is based on disconnected information. Therefore, you need to add business logic to take advantage of optimistic concurrency to resolve problems.

If you choose to use optimistic concurrency, there are two general ways to determine whether changes have occurred: The version method (the actual version number or date time stamp), and the Save all value methods. Version number method

In the version number method, the record to be updated must have a column that contains a date-time stamp or a version number. When the record is read, the date-time stamp or version number is saved on the client. The value is then partially updated.

One way to handle concurrency is to update only if the value in the WHERE clause matches the value on the record. The SQL representation of the method is:

UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2
WHERE datetimestamp = @origDateTimeStamp

Alternatively, you can compare using the version number:

UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2
WHERE rowversion = @origRowVersionValue

If the date-time stamp or version number matches, the record in the data store is not changed, and the record can be safely updated with the new values in the dataset. If it does not match, an error is returned. You can write code to implement this form of concurrency checking in Visual Studio. NET. You must also write code to respond to any update conflicts. To ensure the accuracy of the date-time stamp or version number, you need to set up a trigger on the table to update the date-time stamp or version number when changes to the row occur. Save All Values method

An alternative to using a date-time stamp or version number is to get a copy of all the fields when the record is read. The DataSet object in Ado.net maintains two versions of each modified record: The initial version (the version originally read from the data source) and the modified version (which represents the user update). When you attempt to write records back to a data source, the initial values in the data row are compared to the records in the data source. If they match, the database record has not been changed since it was read. In this case, the changed values in the dataset are successfully written to the database.

For the four commands for the data adapter (DELETE, INSERT, SELECT, and UPDATE), each command has a collection of parameters. Each command has parameters for both the initial value and the current value (or modify the value). Note because no initial records exist, adding a new record (INSERT command) requires only the current value, and removing records (the Delete command) requires only the initial value to locate the record to be deleted.

The following example displays the command text for a dataset command that updates a typical customer table. This command is specified for dynamic SQL and optimistic concurrency.

UPDATE Customers SET CustomerID = @currCustomerID, CompanyName = @currCompanyName, ContactName = @currContactName, contact Title = currcontacttitle, address = @currAddress, city = @currCity, PostalCode = @currPostalCode, Phone = @currPhone, Fax = @currFax WHERE (CustomerID = @origCustomerID) and (address = @origAddress OR @origAddress are null and address is null) A ND (city = @origCity or @origCity are null and City are null) and (CompanyName = @origCompanyName OR @origCompanyName is NUL L and CompanyName is null) and (ContactName = @origContactName OR @origContactName are null and ContactName is null) and (C Ontacttitle = @origContactTitle or @origContactTitle is null and ContactTitle are null) and (Fax = @origFax OR @origFax is NULL and FAX is null) and (Phone = @origPhone OR @origPhone are null and Phone is null) and (PostalCode = @origPostalCode O
R @origPostalCode is null and PostalCode is null); SELECT CustomerID, CompanyName, ContactName, ContactTitle, address, City, PostalCode, Phone, FaX from Customers WHERE (CustomerID = @currCustomerID) 

Note that the nine SET statement parameters represent the current value that will be written to the database, while the nine WHERE statement parameters represent the initial values used to locate the initial record.

The first nine SET statement parameters correspond to the first nine parameters in the parameter collection. These parameters set their sourceversion property to current.

The next nine WHERE statement parameters are used for optimistic concurrency. These placeholders correspond to the next nine parameters in the parameter collection, each of which sets its SourceVersion property to Original.

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 Build Options dialog box. Note The above SQL uses named parameters, while the OleDbDataAdapter command uses a question mark (?) as the parameter placeholder.

By default, Visual Studio creates these parameters for you if you select the optimistic concurrency option in the Data Adapter Configuration Wizard. You will be adding error handling code according to your business requirements at this time. Ado.net provides a DBConcurrencyException object that returns a row that violates the concurrency rule. For more information, see Handling concurrency errors.

Related Article

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.