Serialization enables you to test the effects of transactions executed one at a time. This is useful, for instance in connecting to a server that is not able to handle a large number of concurrent logins.
Serializing users and transactions requires the use of a token or mutex object. Named mutex objects are created by calling the CreateMutex function. The users to be synchronized have to wait until they receive the mutex object. This guarantees that only one user executes a transaction at a time.
After a user has completed the transaction, the user must release the token by calling the ReleaseMutex function. This makes the mutex object available for other users.
The following functions are used in serializing transactions and users:
The following example is excerpted from a sample script called Mutexlogin.bdf, located in the Samples\Database folder in the Silk Performer installation directory. The script consists of three transactions. TMain waits until the user possesses the token, and then connects to the DBMS; Selling executes the database transaction; and CleanUp closes the connection to the database and releases all the resources.
Initially, a mutex object called MyConnectMutex is created to serialize access to critical sections inside transactions for multiple concurrent users. Calling the WaitForSingleObject function effects a delay until the mutex object is in the signaled state. This means that from that point forward, only the current transaction is executed, and this condition remains until the token is released. During execution time a connection to a database system is established. After successfully connecting, the ownership of the mutex object is released. After these steps have been completed successfully, the transaction is committed. In this example, only connecting to the database system is serialized.
benchmark MutexLogin use "dbapi.bdh" var gHdbc1 : number; // handle for database connection gHMutex : number; // mutex handle dcluser user Seller transactions TMain : begin; Selling : 50; CleanUp : end; dcltrans transaction TMain var str: string; begin // SYNC: serialize connect transaction with mutex object // (enter critical section) gHMutex := CreateMutex("MyConnectMutex"); WaitForSingleObject(gHMutex, INFINITE); gHdbc1 := DB_Connect("dsn=sqs_purple;uid=u1;pwd=u1"); Print("Connected!", 1, 1); // SYNC: leave critical section and pass control to other user ReleaseMutex(gHMutex); end TMain; // process database transaction transaction Selling begin ... end Selling; transaction CleanUp begin DB_Disconnect(gHdbc1); end CleanUp;