PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/xbase64-3.1.2/html/xblock.htm

#
HTML | 281 lines | 224 code | 57 blank | 0 comment | 0 complexity | 68ce23f936f641553309d9e95a8185c8 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. <!DOCTYPE HTML PUBLIC>
  2. <HTML>
  3. <TITLE>Xbase DBMS Chapter 10</TITLE>
  4. <BODY BGCOLOR=#FFFFFF>
  5. <H1><p align="center">Xbase DBMS Record and File Locking</p></H1>
  6. <p align="center">Chapter Updated 4/8/98</p><hr>
  7. <h3>Locking Overview</h3>
  8. Xbase DBMS supports multi-user processing through file and record locks.
  9. Record locking restricts multiple cooperating programs from simultaneously
  10. accessing the same data and corrupting it. Without record and file locking
  11. in a multi-user environment, simultaneous access to the data and index files
  12. can cause the files to become inaccurate and unusable.<br><br>
  13. Record locking is on by default in the Xbase DBMS library. To disable it,
  14. comment out the LOCKING_ON option in the <em>options.h</em> file in the
  15. xbase/src directory.<br><br>
  16. The current Xbase DBMS record locking does not co-exist with other Xbase
  17. products and there is not yet support for locking in a DOS/Windows environment.
  18. The locking functions do work correctly for a Xbase DBMS only configuration.
  19. Future version of Xbase DBMS will have enhanced locking features for
  20. co-existing with other Xbase products and also include DOS/Windows support.
  21. <br><br>
  22. The locking methods return either LOCK_FAILED or NO_ERROR. If they return
  23. LOCK_FAILED the actual reason can be found in the global variable
  24. <em>errno</em> or function <em>perror()</em> can be executed to view the
  25. results.
  26. <br><br>
  27. The errno field may contain one of the following values if the lock was not
  28. successful.<br><br>
  29. <TABLE BORDER>
  30. <TR VALIGN="BASELINE">
  31. <TR><TH ALIGN="LEFT">Error Code<TD>Description
  32. <TR><TH ALIGN="LEFT">EBADF<TD>Invalid file descriptor
  33. <TR><TH ALIGN="LEFT">EINVAL<TD>Invalid lock information or file does not support locks
  34. <TR><TH ALIGN="LEFT">EACCESS<BR>EAGAIN<TD>Lock can not be set because it is blocked by an existing lock on the file.
  35. <TR><TH ALIGN="LEFT">ENOLCK<TD>The system is out of lock resources, too many file locks in place.
  36. <TR><TH ALIGN="LEFT">EDEADLK<TD>Deadlock condition
  37. <TR><TH ALIGN="LEFT">EINTR<TD>Process was interrupted by a signal while it was waiting
  38. </TABLE>
  39. <br><br>
  40. <h3>Types of Locks</h3>
  41. <li><em>Write or Exclusive Locks</em> provide exclusive access to a
  42. particular file location. No other process can lock the same location.<br><br>
  43. <li><em>Read or Shared Locks</em> prohibit any process from requesting a write
  44. lock on a specified part of the file. Other processes can request
  45. simultaneous read locks.<br><br><br>
  46. <h3>DBF File Locking Techniques</h3>
  47. Xbase DBMS uses the following protocol for DBF file and record locking:
  48. <br><br>
  49. To lock a record - the first byte of the record is locked.<br>
  50. To lock the file - the header bytes of the file are locked.<br><br>
  51. When a record is being appended to the file, the header bytes are locked.<br>
  52. When a record is being updated, the header bytes and the specific record are
  53. locked.<br><br>
  54. This locking protocol is probably not compatable with other Xbase type products.
  55. However, Xbase can be safely used for multi-user access when it is not
  56. simultaneously updating DBF or NDX files while other products/programs are.
  57. <br><br><br>
  58. <h3>NDX File Locking Techniques</h3>
  59. Xbase DBMS locks indexes by locking the first 512 bytes
  60. of the index file.
  61. The entire index is locked because any updates to the index potentially
  62. can modify significant portions of the index tree.
  63. <br><br><br>
  64. <h3>DBT File Locking Techniques</h3>
  65. Xbase DBMS locks memo files by locking the first 4 bytes
  66. of the memo file. This effectively locks the entire file. The entire file
  67. is locked because any updates to the free block chain can significantly
  68. change the structure of the file.
  69. <br><br><br>
  70. <h3>AutoLocking Features</h3>
  71. If LOCKING_ON is set in the <em>options.h</em> file, the locking methods
  72. execute any appropriate locking logic. If LOCKING_ON is not set in the
  73. <em>options.h</em> file, all locking methods return NO_ERROR without
  74. performing any actual record or file locking. This enables the application
  75. program to always call locking routines regardless of the LOCKING_ON switch
  76. in the <em>options.h</em> file.
  77. <br><br>
  78. By leaving the autolocking features enabled, the application program does
  79. not need to address record, file or index locking. All locking is handled
  80. automatically by the Xbase routines. However, if access to the locking
  81. routines is required, they are available to the applciation programmer.
  82. <br><br>
  83. When the files are automatically locked by the Xbase routines, the database
  84. file is locked first, then it locks the indexes in alphabetical order. To
  85. avoid deadlock conditions, files and record locks should always be done in
  86. the same order. When the files are unlocked, then indexes are unlocked
  87. first, then the database is unlocked.
  88. <br><br>
  89. Auto-locking works well in an on-line transaction based environment.
  90. However, it does not function efficiently in batch mode. If you
  91. will be writing programs which process files in a batch mode, disabling
  92. auto-lock and locking the entire file at the beginning of the process
  93. and unlocking the file at the end of the process will significantly
  94. reduce process time. On a 586-200 class machine, a file with 45000 records
  95. can be read thru in a few seconds with the file locked in batch mode.
  96. In record-lock mode it takes about six minutes with the same processor.
  97. <br><br>For processing large files, locking the file instead of locking each
  98. record is far more efficient. This is how you do it.<br><br>
  99. For reading the file in batch mode:<br>
  100. DBF.AutoLockOff();<br>
  101. DBF.LockDatabase( F_SETLKW, F_RDLCK, 0L );<br><br>
  102. For updating the file in batch mode:<br>
  103. DBF.AutoLockOff();<br>
  104. DBF.LockDatabase( F_SETLKW, F_WRLCK, 0L );<br><br>
  105. <br>
  106. <hr><br>
  107. <h3>Method Table</h3>
  108. <TABLE BORDER>
  109. <CAPTION ALIGN="TOP"><h3><Xbase Locking Method List</h3></CAPTION>
  110. <TR VALIGN="BASELINE">
  111. <TR><TH ALIGN="LEFT">Method<TD>Description
  112. <TR><TH ALIGN="LEFT">DBF::AutoLockOn<TD>Turns autolocking on
  113. <TR><TH ALIGN="LEFT">DBF::AutoLockOff<TD>Turns autolocking off
  114. <TR><TH ALIGN="LEFT">DBF::ExclusiveLock<TD>Lock file and indexes in exclusive mode
  115. <TR><TH ALIGN="LEFT">DBF::ExclusiveUnlock<TD>Unlock files and indexes
  116. <TR><TH ALIGN="LEFT">DBF::LockDatabase<TD>Locks or unlocks a DBF database
  117. <TR><TH ALIGN="LEFT">NDX::LockIndex<TD>Locks or unlocks an NDX index
  118. <TR><TH ALIGN="LEFT">NDX::LockMemoFile<TD>Locks or unlocks a DBT memo field file
  119. </TABLE>
  120. <BR><HR>
  121. <h4>Method Descriptions</h4>
  122. <h4>Method VOID DBF::AutoLockOn( VOID )</h4><br>
  123. This method turns automatic record locking on. Auto record locking is on
  124. by default if LOCKING_ON is set in the options.h file.<br><br>
  125. <h4>Example Program:</h4>
  126. See program <A HREF="/zips/loadzips.cpp">loadzips.cpp</A> for an example of
  127. how to use this method.
  128. <hr>
  129. <h4>Method VOID DBF::AutoLockOff( VOID )</h4><br>
  130. This method turns automatic record locking off. Auto record locking is on
  131. by default if LOCKING_ON is set in the options.h file.
  132. <br><br>
  133. Turning auto locking off will result in slightly better execution speeds
  134. but should not be used in multi-user environments when multiple users can
  135. update files simultanteously. If multiple users are accessing a file which
  136. is read only then it is safe to turn off auto-locking for a particular file.
  137. <br><br>
  138. Turning autolocking off will disable any index file locking which is
  139. particularly dangerous in a multi-user environment if updates on the files
  140. are permitted.
  141. <h4>Example Program:</h4>
  142. See program <A HREF="/zips/loadzips.cpp">loadzips.cpp</A> for an example of
  143. how to use this method.
  144. <hr>
  145. <h4>Method SHORT DBF::ExclusiveLock( SHORT WaitOption )</h4>
  146. <h4>Method SHORT DBF::ExclusiveUnlock( VOID )</h4><br>
  147. ExclusiveLock and ExclusiveUnclock will lock the data file, memo file (if applicable)
  148. and any associated indexes in an exclusive mode. They also turn auto-lock
  149. on and off as appropriate.<br><br>
  150. WaitOption is either:<br><br>
  151. <li>F_SETLK - returns immediately regardless if success or failure<br>
  152. <li>F_SETLKW - waits until lock function executes<br><br>
  153. <h4>Example Program:</h4>
  154. See program <A HREF="/XbaseSamples/sample4.cpp">sample4.cpp</A> for an example of
  155. how to use this method.
  156. <hr>
  157. <h3>Method SHORT DBF::LockDatabase( SHORT WaitOption, SHORT LockType, LONG LRecNo )
  158. </h3><br>
  159. This method locks or unlocks an Xbase (.DBF) file which was previously opened.<br>
  160. <br>
  161. WaitOption is either:<br><br>
  162. <li>F_SETLK - returns immediately regardless if success or failure<br>
  163. <li>F_SETLKW - waits until lock function executes<br><br>
  164. LockType is one of:<br><br>
  165. <li>F_RDLCK - Perform a Read or Shared Lock<br>
  166. <li>F_WRLCK - Perform a Write or Exclusive Lock<br>
  167. <li>F_UNLCK - Unlock it<br><br>
  168. LRecNo is:<br><br>
  169. 0 - Lock the header section of the file (use this to lock the file)<br>
  170. 1 through n - Lock a particular record<br><br>
  171. <TABLE BORDER>
  172. <CAPTION ALIGN="TOP"<h4>Method Return Codes</h4></CAPTION>
  173. <TR><TH ALIGN="LEFT">Return Code<TD>Description
  174. <TR><TH ALIGN="LEFT">INVALID_RECORD<TD>An invalid record given
  175. <TR><TH ALIGN="LEFT">LOCK_FAILED<TD>The lock action failed, see errno
  176. <TR><TH ALIGN="LEFT">NO_ERROR<TD>The lock was successful
  177. </TABLE>
  178. <h4>Example Program:</h4>
  179. See program <A HREF="/zips/loadzips.cpp">loadzips.cpp</A> for an example of
  180. how to use this method.
  181. <hr>
  182. <h3>Method SHORT DBF::LockIndex( SHORT WaitOption, SHORT LockType )
  183. </h3><br>
  184. This method locks or unlocks an Index (.NDX) file which was previously opened.<br>
  185. <br>
  186. WaitOption is either:<br><br>
  187. <li>F_SETLK - returns immediately regardless if success or failure<br>
  188. <li>F_SETLKW - waits until lock function executes<br><br>
  189. LockType is one of:<br><br>
  190. <li>F_RDLCK - Perform a Read or Shared Lock<br>
  191. <li>F_WRLCK - Perform a Write or Exclusive Lock<br>
  192. <li>F_UNLCK - Unlock it<br><br>
  193. <TABLE BORDER>
  194. <CAPTION ALIGN="TOP"<h4>Method Return Codes</h4></CAPTION>
  195. <TR><TH ALIGN="LEFT">Return Code<TD>Description
  196. <TR><TH ALIGN="LEFT">LOCK_FAILED<TD>The lock action failed, see errno
  197. <TR><TH ALIGN="LEFT">NO_ERROR<TD>The lock was successful
  198. </TABLE>
  199. <h4>Example Program:</h4>
  200. See program <A HREF="/zips/loadzips.cpp">loadzips.cpp</A> for an example of
  201. how to use this method.
  202. <hr>
  203. <h3>Method SHORT DBF::LockMemoFile( SHORT WaitOption, SHORT LockType )
  204. </h3><br>
  205. This method locks or unlocks a memo (.DBT) file which was previously opened.
  206. It is not necessary for an application to call this method as locking is
  207. handled automatically by other routines.<br><br>
  208. WaitOption is either:<br><br>
  209. <li>F_SETLK - returns immediately regardless if success or failure<br>
  210. <li>F_SETLKW - waits until lock function executes<br><br>
  211. LockType is one of:<br><br>
  212. <li>F_RDLCK - Perform a Read or Shared Lock<br>
  213. <li>F_WRLCK - Perform a Write or Exclusive Lock<br>
  214. <li>F_UNLCK - Unlock it<br><br>
  215. <TABLE BORDER>
  216. <CAPTION ALIGN="TOP"<h4>Method Return Codes</h4></CAPTION>
  217. <TR><TH ALIGN="LEFT">Return Code<TD>Description
  218. <TR><TH ALIGN="LEFT">LOCK_FAILED<TD>The lock action failed, see errno
  219. <TR><TH ALIGN="LEFT">NO_ERROR<TD>The lock was successful
  220. </TABLE>
  221. <hr>
  222. <p><img src="xbase.jpg"><br><hr>
  223. </BODY>
  224. </HTML>