home *** CD-ROM | disk | FTP | other *** search
- // BDE - (C) Copyright 1995 by Borland International
-
- // reclock.c
- #include "snipit.h"
-
- static const char szTblName[] = "cust";
- static const char szTblType[] = szDBASE;
-
- //=====================================================================
- // Function:
- // RecLock();
- //
- // Description:
- // This example shows how to do record locking. The
- // example will simulate multi-user access by creating multiple
- // sessions. The example will show both working and failing locks.
- //=====================================================================
- void
- RecLock (void)
- {
- DBIResult rslt; // Value returned from IDAPI functions
- CURProps TblProps; // Properties of the table. Used to determine
- // the size of the record
- hDBIDb hDb1; // Handle to the first database
- hDBICur hCur1; // Handle to the first table
- hDBISes hSession1; // Handle to the first session
- pBYTE pRecBuf1; // Pointer to the record buffer
- hDBIDb hDb2; // Handle to the second database
- hDBICur hCur2; // Handle to the second table
- hDBISes hSession2; // Handle to the second session
- pBYTE pRecBuf2; // Pointer to the record buffer
-
- Screen("*** Record Locking Example ***\r\n");
-
- BREAK_IN_DEBUGGER();
-
- Screen(" Initializing IDAPI...\r\n");
- if (InitAndConnect(&hDb1) != DBIERR_NONE)
- {
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Acquire a session handle. Initializing IDAPI opens a session.
- Screen(" Acquiring the current session's handle...");
- rslt = DbiGetCurrSession(&hSession1);
- ChkRslt(rslt, "GetCurrSession");
-
- Screen(" Setting the database directory in session #1...");
- rslt = DbiSetDirectory(hDb1, (pCHAR) szTblDirectory);
- ChkRslt(rslt, "SetDirectory");
-
- Screen(" Opening the %s table in session #1...", szTblName);
- rslt = DbiOpenTable(hDb1, (pCHAR) szTblName, (pCHAR) szTblType,
- NULL, NULL, NULL, dbiREADWRITE, dbiOPENSHARED,
- xltFIELD, FALSE, NULL, &hCur1);
- if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
- {
- CloseDbAndExit(&hDb1);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Aquire a new session. This is to simulate multi-user table access.
- Screen("\r\n Opening session #2...");
- rslt = DbiStartSession("", &hSession2, NULL);
- ChkRslt(rslt, "StartSession");
-
- Screen("\r\n Opening standard database in session #2...");
- rslt = DbiOpenDatabase("", 0, dbiREADWRITE, dbiOPENSHARED,
- NULL, NULL, NULL, NULL, &hDb2);
- if (ChkRslt(rslt, "OpenDatabase") != DBIERR_NONE)
- {
- rslt = DbiCloseCursor(&hCur1);
- ChkRslt(rslt, "CloseCursor");
- CloseDbAndExit(&hDb1);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- Screen(" Setting the database directory in session #2...");
- rslt = DbiSetDirectory(hDb2, (pCHAR) szTblDirectory);
- ChkRslt(rslt, "SetDirectory");
-
- Screen(" Opening the %s table in session #2...", szTblName);
- rslt = DbiOpenTable(hDb2, (pCHAR) szTblName, (pCHAR) szTblType,
- NULL, NULL, NULL, dbiREADWRITE, dbiOPENSHARED,
- xltFIELD, FALSE, NULL, &hCur2);
- if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
- {
- rslt = DbiCloseCursor(&hCur1);
- ChkRslt(rslt, "CloseCursor");
- rslt = DbiCloseDatabase(&hDb2);
- ChkRslt(rslt, "CloseDatabase");
- CloseDbAndExit(&hDb1);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Create the record buffers.
- rslt = DbiGetCursorProps(hCur2, &TblProps);
- ChkRslt(rslt, "GetCursorProps");
-
- pRecBuf1 = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
- pRecBuf2 = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
- if ((!pRecBuf1) || (!pRecBuf2))
- {
- if (pRecBuf1) free(pRecBuf1);
- if (pRecBuf2) free(pRecBuf2);
- rslt = DbiCloseCursor(&hCur1);
- ChkRslt(rslt, "CloseCursor");
- rslt = DbiCloseCursor(&hCur2);
- ChkRslt(rslt, "CloseCursor");
- rslt = DbiCloseDatabase(&hDb2);
- ChkRslt(rslt, "CloseDatabase");
- CloseDbAndExit(&hDb1);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Initialize the record buffer.
- rslt = DbiInitRecord(hCur2, pRecBuf2);
- ChkRslt(rslt, "InitRecord");
-
- Screen(" Session #2 is getting and locking the first record...");
- rslt = DbiSetToBegin(hCur2);
- ChkRslt(rslt, "SetToBegin");
- rslt = DbiGetNextRecord(hCur2, dbiWRITELOCK, pRecBuf2, NULL);
- ChkRslt(rslt, "GetRecord");
-
- // Set the current session to session #1.
- rslt = DbiSetCurrSession(hSession1);
- ChkRslt(rslt, "SetCurrSession");
-
- Screen("\r\n Session #1 is getting the first record without"
- " locking it...");
- rslt = DbiSetToBegin(hCur1);
- ChkRslt(rslt, "SetToBegin");
- rslt = DbiGetNextRecord(hCur1, dbiNOLOCK, pRecBuf1, NULL);
- ChkRslt(rslt, "GetRecord");
-
- // Attempt to modify the record in session #1 - get an error as
- // session #2 has the record locked.
- Screen(" Session #1 is modifying the record...");
- Screen(" Error Expected - record is locked...");
- rslt = DbiModifyRecord(hCur1, pRecBuf1, TRUE);
- ChkRslt(rslt, "GetRecord");
-
- // Switch the current session to session #2.
- rslt = DbiSetCurrSession(hSession2);
- ChkRslt(rslt, "SetCurrSession");
-
- // Modify the record in session #2. Session #2 locked the record,
- // so it will succeed this time. Note that the lock is released
- // after the record is modified.
- Screen("\r\n Session #2 is modifying the record...");
- rslt = DbiModifyRecord(hCur2, pRecBuf2, TRUE);
- ChkRslt(rslt, "ModifyRecord");
-
- // Clean Up
- free(pRecBuf1);
- free(pRecBuf2);
-
- rslt = DbiSetCurrSession(hSession2);
- ChkRslt(rslt, "SetCurrSession");
-
- Screen("\r\n Close the %s table in session #2...", szTblName);
- rslt = DbiCloseCursor(&hCur2);
- ChkRslt(rslt, "CloseCursor");
-
- Screen(" Close the database handle in session #2...");
- rslt = DbiCloseDatabase(&hDb2);
- ChkRslt(rslt, "CloseDatabase");
-
- Screen(" Closing session #2...");
- rslt = DbiCloseSession(hSession2);
- ChkRslt(rslt, "CloseSession");
-
- rslt = DbiSetCurrSession(hSession1);
- ChkRslt(rslt, "SetCurrSession");
-
- Screen("\r\n Close the %s table in session #1...", szTblName);
- rslt = DbiCloseCursor(&hCur1);
- ChkRslt(rslt, "CloseCursor");
-
- Screen(" Close the database and exit IDAPI...");
- CloseDbAndExit(&hDb1);
-
- Screen("\r\n*** End of Example ***");
- }
-
-