High concurrency Database Control access code written in C #

Source: Internet
Author: User
Tags access database

The role of code is to ensure that the upper end of the cache service failure (generally the probability is relatively low), the formation of an inverted bottleneck, so as to protect the database, the database is down, is the big problem (such as affecting other applications).

Assumptions (not exactly the correct data, only examples):

Support 10,000,000 queries per second (tens of thousands of times);

One read library takes time: 1ms;

Modifying memory variables takes time: 0.001ms;

So:

The number of requests to the database that is last accessed per second < 1000

The other 9,900,000 requests are returned to other pages. That's why a lot of grab-list sites are accessible to someone who gets busy in the page.

Microscopic to 1ms, in the Currentvalidsessionid = =-1 of the time is 1ms, thus an average of 10,000 records influx.

Currentvalidsessionid from 1 to another value in 0.001ms, this time,


The code is as follows:


Lock (Databasedoor)

{

Now there are only one request can reach below codes.

if (Currentvalidsessionid = =-1)

{

Currentvalidsessionid = Currentrequest.sessionid;

}

}

An average of 10000x0.001=10 records will be executed to the above code, and the operating system will form a wait sequence for the lock.

So our goal is to allow only one read library per millisecond (because other apps will use it too), so we just want this to go into the 10, and eventually only one can move on.

So this is


The code is as follows:


if (Currentvalidsessionid = =-1)

{

}

The role of the. Once again, a request to the Atomic Protection queue is made, and only one can continue.

A little thought:

In fact, for a server with a frequency of N GHz, one memory is assigned to another memory data is the 1~4 instruction (average 2, two MOV operation), that is, 2/n ns time, not our hypothesis 1000ns (0.001ms). In fact, without atoms, we can already control the number of according requests in single digit.

However, an architect, if you can use a 99.99% security scheme, there is absolutely no 99.9%. So.


The code is as follows:


public static Long Currentvalidsessionid =-1;

public static Object Databasedoor = new Object ();

void Readdatabase (Request currentrequest)

{

Use Currentvalidsessionid to filter out other requests came in during the Execute time gap

if (Currentvalidsessionid = =-1)

{

Use Object-lock to filter out other requests came in during the variable change time gap.

Lock (Databasedoor)

{

Now there are only very little number of requests can reach below codes.

if (Currentvalidsessionid = =-1)

{//Now there would be is only one request can access the database

Currentvalidsessionid = Currentrequest.sessionid;

}

}

}

if (Currentvalidsessionid = = Currentrequest.sessionid)

{//here is the one!

Try

{

Use transaction to guarantee the execute time to void block

Access database codes go here

}

catch ()

{

Exception codes go here

}

Finally

{

Currentvalidsessionid =-1; Recover to original state

}

}

}

The above is the whole content of this article, I hope that you learn C # High concurrency programming can be helpful.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
High concurrency Database Control access code written in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23106.html






Related content C#éè??? £ê??? Builderéú3é?÷?£ê?? A?? ′?à??????? Μ???? êìaêμàyc# MVC Payment Tutorial Series public number payment Code C # Implementation Create desktop shortcut with Add Web page to Favorites Sample C # Use the XmlDocument class to create and modify XML-formatted data files
Example of using the @ declaration variable in C # (verbatim identifier) C # uses the Equals () method to compare two objects for Equality method C # to get the initials of a Chinese character string C # and SQL implemented string similarity calculation code sharing

High concurrency Database Control access code written in C #

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.