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

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