home *** CD-ROM | disk | FTP | other *** search
- // BDE - (C) Copyright 1995 by Borland International
-
- // tbllock.c
- #include "snipit.h"
-
- static const char szTblName[] = "customer";
- static const char szTblType[] = szPARADOX;
-
- //=====================================================================
- // Function:
- // TblLock();
- //
- // Description:
- // This example shows how to lock a table. The
- // example will simulate multi-user access by opening a table from
- // multiple sessions. The example will show both working and
- // failing locks.
- //=====================================================================
- void
- TblLock (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("*** Table 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;
- }
-
- Screen(" Place a write lock on the table in session #1...");
- rslt = DbiAcqTableLock(hCur1, dbiWRITELOCK);
- ChkRslt(rslt, "AcqTableLock");
-
- // Create a new session 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 buffer.
- 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 = 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");
-
- // Go to the top of the table.
- rslt = DbiSetToBegin(hCur2);
- ChkRslt(rslt, "SetToBegin");
-
- Screen(" Session #2 is getting and locking the first record...");
- Screen(" Error expected - file is locked...");
- rslt = DbiGetNextRecord(hCur2, dbiWRITELOCK, pRecBuf2, NULL);
- ChkRslt(rslt, "GetRecord");
-
- Screen("\r\n Session #2 is getting the first record without locking...");
- rslt = DbiGetNextRecord(hCur2, dbiNOLOCK, pRecBuf2, NULL);
- ChkRslt(rslt, "GetRecord");
-
- Screen("\r\n Session #2 is attempting to modify the record...");
- Screen(" Error expected - file is locked...");
- 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 cursor to the table in session #2...");
- 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");
-
- rslt = DbiRelTableLock(hCur1, TRUE, dbiWRITELOCK);
- ChkRslt(rslt, "RelTableLock");
-
- Screen("\r\n Close the cursor to the table in session #1...");
- rslt = DbiCloseCursor(&hCur1);
- ChkRslt(rslt, "CloseCursor");
-
- Screen(" Close the database and exit IDAPI...");
- CloseDbAndExit(&hDb1);
-
- Screen("\r\n*** End of Example ***");
- }
-