Synchronizing Shared Resources
The .NET Framework includes a rich set of classes within the System.Threading namespace that allow the programmer to control access to shared resources using a myriad of techniques. One of the WaitHandle derived classes, Mutex, provided a perfect solution to a problem I faced synchronizing access to a legacy database from an XML Web Service.
My client has a rather large legacy desktop application written in C++ using the MFC. The backend database is a propriety 3rd party package that is apparently implemented for single user access only. My task was to develop an XML Web Service and a web application front end to replace much of the existing desktop application.
The client had already written a .NET Managed C++ interface library to provide access to the unmanaged backend database. This made it very easy to implement the Web Service--I mostly just wrapped the Managed C++ interface exposing the appropriate methods as [WebMethod] in the C# Web Service code.
As we moved from single user unit testing to more extensive testing with multiple users, we discovered very quickly that the unmanaged code would crash frequently. It wasn't much of leap to figure out that the unmanaged code needed to be synchronized.
There are several synchronization locks and techniques available in the Framework, but one in particular seemed perfect for this scenario. A Mutex is a named synchronization object derived from the WaitHandle class that allows for creation of mutually exclusive regions of code. Since a Mutex has a name, it can be used across processes--a requirement in our case because we are crossing several boundaries venturing into the unmanaged legacy code.
I first created a class to encapsulate the System.Threading.Mutex object. During construction, the object obtains a "timeout" parameter from the Web Services' web.conifg file. If the specified timeout value is missing or in error, it defaults to 15 seconds. If the timeout value is zero, then the Mutex is not implemented and the operation using the DBMutex object will not wait for exclusive access.
private class DBMutex
private int timeout;
private Mutex mutex;
string val =
if ( val == null )
val = "15";
timeout = Convert.ToInt32( val );
timeout = 15;
if ( timeout < 1 )
timeout = 0;
timeout *= 1000; // milliseconds
if ( timeout == 0 )
mutex = null;
mutex = new Mutex( false, "DBMutex" );
public bool WaitOne()
if ( timeout == 0 )
else return mutex.WaitOne( timeout, false );
public void Release()
if ( timeout > 0 )
Other than contruction, the only other methods implemented mirror the Mutex classes WaitOne and Release methods. The WaitOne method will block the caller if the resource is already locked. If the timeout period is exhausted while waiting, this method will return false; otherwise true is returned to indicate that the resource is now locked exclusively for the caller.
The remaining task is to insure that each Web Service [WebMethod] that tries to access the legacy database first obtains the exlusive DBMutex lock, and then releases the lock as soon as the function call into the managed and unmanaged code returns.
public Results GetDBInfo( int infoID )
DBMutex mutex = new DBMutex();
if ( ! mutex.WaitOne() )
return new Results( "Exclusive Lock Failure" );
LegacySystem.DBAccess db = new LegacySystem.DBAccess();
Results info = db.GetDBInfo( infoID );
Without getting into implementation details, the Results (return) class encapsulates a DataSet and can indicate an error condition as illustrated when the mutex.WaitOne() fails. The LegacySystem.DBAccess class is part of the interface library written by the client in Managed C++.
This was a very simple solution to our problem that seems to be working just fine. By adjusting the timeout values for both the Web Service and the Mutex WaitOne() method call, we have balanced the user load that we experience, while still not causing the web application and Web Service to fail if the backend database should become unavailable.
Although we don't need it in our application (as yet), one side effect of the Mutex crossing process boundaries is that the legacy desktop application could use the same named Mutex with the Windows API to provide the same synchronized access employed by the Web Service and managed library interface.
Send your comments and let me know what you think of this article: firstname.lastname@example.org
Steve Sharrock - www.AspAlliance.com/shark / www.SharkCode.com