Sometimes curious, if a foreigner send a technical article, you will not be around someone squalling? Making personal attacks? Chinese people like to hit people, foreigners do not know is this is not a character? Curious to ask you.
Often we in the development process, the debugger can not simulate the actual environment of the operation under multi-user simultaneous operation.
To simulate multi-user concurrent operation, let's write a multi-threaded example to fully simulate multi-user concurrent
{
/// <Summary> /// define delegates /// </ Summary> /// <param name = "User"> user </ param> the delegate void MakeSequenceDelegate ( String User); /// <Summary > /// this is the test sequence /// </ Summary> /// <param name = "user"> user </ param> Private void MakeSequence ( String user) { for ( int I = 0 ; I < 10 ; i++
)
{
BaseSequenceManager sequenceManager = new new BaseSequenceManager (); // analog July 2010 generates the order number the System.Console.WriteLine (User + " : " + sequenceManager.GetSequence ( " Order201007 " )); } } /// <Summary > /// this is simulated multiple users click /// </ Summary> public void DoTest () { // simulate three concurrent users operating MakeSequenceDelegate sequenceDelegate1 = new new
MakeSequenceDelegate(MakeSequence);
sequenceDelegate1.BeginInvoke( " user1 " , null , null );
MakeSequenceDelegate sequenceDelegate2 = new MakeSequenceDelegate(MakeSequence);
sequenceDelegate2.BeginInvoke( " user2 " , null , null );
MakeSequenceDelegate sequenceDelegate3 = new MakeSequenceDelegate(MakeSequence);
sequenceDelegate3.BeginInvoke( " user3 " , null , null );
}
}
Sequence Listing below design effect, is stored in the table number of the current sequence is much like what information.
Since no concurrency control, the output of the program follows the case, of course, when the single-user operating test, the test is unlikely operating state of the concurrency.
There will be repeats of acute loss of a sequence that will happen, and can not guarantee multi-user, can produce completely unique order number.
Why concurrency problems occur? Because you read, I also read, when you update sequence, I also update sequence, because the same program running multiple copies, 1,2 while users have read 0007 this number.
Operational effects concurrency control as follows:
This number is continuous, and the situation is not lost, there is no duplication occurs.
Here's how to avoid concurrency? BaseSequenceManager performed following exclusion concurrent processing.
private static readonly object SequenceLock = new object();
string returnValue = string.Empty;
// use the lock mechanism here, to improve concurrency control
Lock (SequenceLock)
{
returnValue = read the current value of the sequence (a) in the database
Update sequence database (ii)
}
return returnValue;
Because the database is read, update, requires two-step operation, is led to where concurrency problems.
The above article is mainly related to the following technical problems:
1: The need to be able to write multi-threaded simulation program.
2: How to multi-thread function to pass parameters need to learn.
3: This could be considered the basis of automated testing so-called core component of it.
4: lock Statement (C # Reference) http://msdn.microsoft.com/zh-cn/library/c5kehkcz(VS.80).aspx
Up test easy to use management software, to really put a lot of unexpected errors in the actual production environment will often occur, this is often not able to repeat the test operation of a multi-user concurrency cause accounted for part of it.
Although the above procedure is nothing difficult afternoon spent nearly 2-3 hours before adjusting the hope that the reader assess the workload to have a reference.
Estimated in the domestic management software category, more than 90% did not carry out a rigorous multi-user concurrent test, more than 90% did not consider the application concurrency issues and database concurrency problems, if not more simple and more easy way is compelling enough, why now against themselves then, do so many complicated concurrent processing.
Reproduced in: https: //my.oschina.net/iwenr/blog/227972