home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / TBLLOCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  6.4 KB  |  182 lines

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // tbllock.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "customer";
  7. static const char szTblType[] = szPARADOX;
  8.  
  9. //=====================================================================
  10. //  Function:
  11. //          TblLock();
  12. //
  13. //  Description:
  14. //          This example shows how to lock a table.  The
  15. //          example will simulate multi-user access by opening a table from
  16. //          multiple sessions. The example will show both working and
  17. //          failing locks.
  18. //=====================================================================
  19. void
  20. TblLock (void)
  21. {
  22.     DBIResult   rslt;       // Value returned from IDAPI functions
  23.     CURProps    TblProps;   // Properties of the table.  Used to determine
  24.                             // the size of the record.
  25.  
  26.     hDBIDb      hDb1;       // Handle to the first database
  27.     hDBICur     hCur1;      // Handle to the first table
  28.     hDBISes     hSession1;  // Handle to the first session
  29.     pBYTE       pRecBuf1;   // Pointer to the record buffer
  30.  
  31.     hDBIDb      hDb2;       // Handle to the second database
  32.     hDBICur     hCur2;      // Handle to the second table
  33.     hDBISes     hSession2;  // Handle to the second session
  34.     pBYTE       pRecBuf2;   // Pointer to the record buffer
  35.  
  36.     Screen("*** Table Locking Example ***\r\n");
  37.  
  38.     BREAK_IN_DEBUGGER();
  39.  
  40.     Screen("    Initializing IDAPI...\r\n");
  41.     if (InitAndConnect(&hDb1) != DBIERR_NONE)
  42.     {                                       
  43.         Screen("\r\n*** End of Example ***");
  44.         return;
  45.     }
  46.  
  47.     // Acquire a session handle.  Initializing IDAPI opens a session.
  48.     Screen("    Acquiring the current session's handle...");
  49.     rslt = DbiGetCurrSession(&hSession1);
  50.     ChkRslt(rslt, "GetCurrSession");
  51.  
  52.     Screen("    Setting the database directory in session #1...");
  53.     rslt = DbiSetDirectory(hDb1, (pCHAR) szTblDirectory);
  54.     ChkRslt(rslt, "SetDirectory");
  55.  
  56.     Screen("    Opening the \"%s\" table in session #1...", szTblName);
  57.     rslt = DbiOpenTable(hDb1, (pCHAR) szTblName, (pCHAR) szTblType,
  58.                         NULL, NULL, NULL, dbiREADWRITE, dbiOPENSHARED,
  59.                         xltFIELD, FALSE, NULL, &hCur1);
  60.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  61.     {
  62.         CloseDbAndExit(&hDb1);
  63.         Screen("\r\n*** End of Example ***");
  64.         return;
  65.     }
  66.  
  67.     Screen("    Place a write lock on the table in session #1...");
  68.     rslt = DbiAcqTableLock(hCur1, dbiWRITELOCK);
  69.     ChkRslt(rslt, "AcqTableLock");
  70.  
  71.     // Create a new session to simulate multi-user table access.
  72.     Screen("\r\n    Opening session #2...");
  73.     rslt = DbiStartSession("", &hSession2, NULL);
  74.     ChkRslt(rslt, "StartSession");
  75.  
  76.     Screen("\r\n    Opening Standard database in session #2...");
  77.     rslt = DbiOpenDatabase("", 0, dbiREADWRITE, dbiOPENSHARED,
  78.                            NULL, NULL, NULL, NULL, &hDb2);
  79.     if (ChkRslt(rslt, "OpenDatabase") != DBIERR_NONE)
  80.     {
  81.         rslt = DbiCloseCursor(&hCur1);
  82.         ChkRslt(rslt, "CloseCursor");
  83.         CloseDbAndExit(&hDb1);
  84.         Screen("\r\n*** End of Example ***");
  85.         return;
  86.     }
  87.  
  88.     Screen("    Setting the database directory in session #2...");
  89.     rslt = DbiSetDirectory(hDb2, (pCHAR) szTblDirectory);
  90.     ChkRslt(rslt, "SetDirectory");
  91.  
  92.     Screen("    Opening the \"%s\" table in session #2...", szTblName);
  93.     rslt = DbiOpenTable(hDb2, (pCHAR) szTblName, (pCHAR) szTblType,
  94.                         NULL, NULL, NULL, dbiREADWRITE, dbiOPENSHARED,
  95.                         xltFIELD, FALSE, NULL, &hCur2);
  96.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  97.     {
  98.         rslt = DbiCloseCursor(&hCur1);
  99.         ChkRslt(rslt, "CloseCursor");
  100.         rslt = DbiCloseDatabase(&hDb2);
  101.         ChkRslt(rslt, "CloseDatabase");
  102.         CloseDbAndExit(&hDb1);
  103.         Screen("\r\n*** End of Example ***");
  104.         return;
  105.     }
  106.  
  107.     // Create the record buffer.
  108.     rslt = DbiGetCursorProps(hCur2, &TblProps);
  109.     ChkRslt(rslt, "GetCursorProps");
  110.  
  111.     pRecBuf1 = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
  112.     pRecBuf2 = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
  113.     if ((!pRecBuf1) || (!pRecBuf2))
  114.     {
  115.         if (pRecBuf1) free(pRecBuf1);
  116.         if (pRecBuf2) free(pRecBuf2);
  117.         rslt = DbiCloseCursor(&hCur1);
  118.         ChkRslt(rslt, "CloseCursor");
  119.         rslt = DbiCloseDatabase(&hDb2);
  120.         ChkRslt(rslt, "CloseDatabase");
  121.         CloseDbAndExit(&hDb1);
  122.         Screen("\r\n*** End of Example ***");
  123.         return;
  124.     }
  125.  
  126.     // Initialize the record buffer.
  127.     rslt = DbiInitRecord(hCur2, pRecBuf2);
  128.     ChkRslt(rslt, "InitRecord");
  129.  
  130.     // Go to the top of the table.
  131.     rslt = DbiSetToBegin(hCur2);
  132.     ChkRslt(rslt, "SetToBegin");
  133.  
  134.     Screen("    Session #2 is getting and locking the first record...");
  135.     Screen("        Error expected - file is locked...");
  136.     rslt = DbiGetNextRecord(hCur2, dbiWRITELOCK, pRecBuf2, NULL);
  137.     ChkRslt(rslt, "GetRecord");
  138.  
  139.     Screen("\r\n    Session #2 is getting the first record without locking...");
  140.     rslt = DbiGetNextRecord(hCur2, dbiNOLOCK, pRecBuf2, NULL);
  141.     ChkRslt(rslt, "GetRecord");
  142.  
  143.     Screen("\r\n    Session #2 is attempting to modify the record...");
  144.     Screen("        Error expected - file is locked...");
  145.     rslt = DbiModifyRecord(hCur2, pRecBuf2, TRUE);
  146.     ChkRslt(rslt, "ModifyRecord");
  147.  
  148.     // Clean up.
  149.     free(pRecBuf1);
  150.     free(pRecBuf2);
  151.  
  152.     rslt = DbiSetCurrSession(hSession2);
  153.     ChkRslt(rslt, "SetCurrSession");
  154.  
  155.     Screen("\r\n    Close the cursor to the table in session #2...");
  156.     rslt = DbiCloseCursor(&hCur2);
  157.     ChkRslt(rslt, "CloseCursor");
  158.  
  159.     Screen("    Close the database handle in session #2...");
  160.     rslt = DbiCloseDatabase(&hDb2);
  161.     ChkRslt(rslt, "CloseDatabase");
  162.  
  163.     Screen("    Closing session #2...");
  164.     rslt = DbiCloseSession(hSession2);
  165.     ChkRslt(rslt, "CloseSession");
  166.  
  167.     rslt = DbiSetCurrSession(hSession1);
  168.     ChkRslt(rslt, "SetCurrSession");
  169.  
  170.     rslt = DbiRelTableLock(hCur1, TRUE, dbiWRITELOCK);
  171.     ChkRslt(rslt, "RelTableLock");
  172.  
  173.     Screen("\r\n    Close the cursor to the table in session #1...");
  174.     rslt = DbiCloseCursor(&hCur1);
  175.     ChkRslt(rslt, "CloseCursor");
  176.  
  177.     Screen("    Close the database and exit IDAPI...");
  178.     CloseDbAndExit(&hDb1);
  179.     
  180.     Screen("\r\n*** End of Example ***");
  181. }
  182.