home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / 32SNIPIT.PAK / TBLLOCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.4 KB  |  183 lines

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