- public volatile 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 is only very little number of requests can reach below codes.
- if (currentValidSessionID == -1)
- { // now there will be 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
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2508201614991.html
来源: http://www.codesnippet.cn/detail/2508201614991.html