README.explicit_locks 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. SQL Language Extension: WITH LOCK clause in the SELECT statement
  2. Syntax
  3. ========
  4. SELECT ... FROM <sometable> [WHERE ...] [FOR UPDATE [OF ...]] WITH LOCK;
  5. The WITH LOCK clause lets you lock the selected rows so that
  6. other users cannot lock or update the rows until you end your
  7. transaction. You can specify this clause only in a top-level
  8. SELECT statement (not in subqueries). This clause can be used
  9. in DSQL and PSQL.
  10. Restrictions:
  11. - You cannot specify this clause with the following other constructs:
  12. the DISTINCT operator, group_by_clause, or aggregate functions.
  13. - from_clause of SELECT statement must specify single database table
  14. (views and external tables are not allowed)
  15. Author:
  16. Nickolay Samofatov <[email protected]>
  17. N O T E S
  18. =========
  19. When engine processes each record falling under explicit lock
  20. statement it either returns most current committed record version
  21. or raises exception.
  22. Wait behaviour and conflict reporting depends on the transaction
  23. parameters specified in the TPB block.
  24. TPB mode Behavior
  25. -------------------------------------------------------------
  26. isc_tpb_consistency Explicit locks are overriden by
  27. implicit or explicit table-level locks
  28. and are ignored
  29. isc_tpb_concurrency + If record is modified by any committed
  30. isc_tpb_nowait transaction since transaction attempting
  31. to get explicit lock started or there is
  32. active transaction that performed
  33. modifiction of this record, update
  34. conflict exception is raised immediately
  35. isc_tpb_concurrency + If record is modified by any committed
  36. isc_tpb_wait transaction since transaction attempting
  37. to get explicit lock started, update conflict
  38. exception is raised immediately.
  39. If there is active transaction holding ownership
  40. on this record (via explicit lock or normal update)
  41. locking transaction waits for outcome of
  42. blocking transation and when it finishes
  43. attempts to get lock on record again.
  44. This means that if blocking transaction
  45. commits modification of this record,
  46. update conflict exception will be raised.
  47. isc_tpb_read_committed + If there is active transaction holding ownership
  48. isc_tpb_nowait on this record (via explicit lock or normal update),
  49. update conflict exception is raised immediately.
  50. isc_tpb_read_committed + If there is active transaction holding ownership
  51. isc_tpb_wait on this record (via explicit lock or normal update),
  52. locking transaction waits for outcome of
  53. blocking transation and when it finishes
  54. attempts to get lock on record again.
  55. Update conflict exceptions can never be raised
  56. by this kind of explicit lock statement.
  57. When UPDATE statement steps on a record that is locked by another transaction
  58. it either raises update conflict exception or waits for the end of locking
  59. transaction depending on TPB mode. Engine behaviour here is the same as if this
  60. record was already modified by locking transaction.
  61. Engine guaranties that all records returned by explicit lock statement
  62. are actually locked and DO fall under search condition specified in WHERE clause
  63. if this search condition depends only on a record to be returned (for example, it
  64. contains no subqueries reading mutable tables, etc). It also guaranties that
  65. no rows not falling under search condition are locked by the statement. It doesn't
  66. guaranty that there are no rows falling under search condition, but not locked
  67. (this may happen if other parallel transactions commit their changes during
  68. execution of locking statement).
  69. Engine locks rows at fetch time. This has important consequences if you lock
  70. several rows at once. By default, access methods for Firebird databases
  71. fetch results in packs of a few hundred rows. Most access components may not
  72. give you the rows contained in the last fetched packed where error happened.
  73. You may use FOR UPDATE clause to prevent usage of buffered fetches (and may
  74. use positioned updates feature) or set fetch buffer size to 1 in your access
  75. components in case you need to process locked row before next row is locked
  76. or if you want actually process all rows that can be locked before first row
  77. producing error.
  78. Commit and rollback retaining release explicit and implicit locks
  79. exactly as normal commit and rollback. Be careful when using buffered
  80. fetches and retaining operations. Records contained in the client-side
  81. buffer may be unlocked even before returned to the application.
  82. Rolling back of implicit or explicit savepoint releases record locks that
  83. were taken under this savepoint, but doesn't notify waiting transactions.
  84. Applications should not depend on this behaviour as it may get changed in
  85. the future.
  86. While explicit locks can be used to prevent and/or handle update conflict
  87. errors amount of deadlock errors will grow unless you carefully design your
  88. locking strategy. Most applications do not need explicit locks at all. The main
  89. purposes of explicit locks are (1) to prevent expensive handling of update
  90. conflict errors in heavily loaded applications and (2) to maintain integrity
  91. of objects mapped to relational database in clustered environment.
  92. While solutions for this problems may be very important for web sites
  93. handling thousands of concurrent users or ERP/CRM solutions
  94. working in large corporations most application programs do not
  95. need to work in such conditions. Explicit locking is an advanced feature,
  96. do not misuse it ! If your use of explicit locking doesn't fall in
  97. one of two categories above think of another way to do the task.
  98. Examples:
  99. A) (simple)
  100. SELECT * FROM DOCUMENT WHERE ID=? WITH LOCK
  101. B) (multiple rows, one-by-one processing with DSQL cursor)
  102. SELECT * FROM DOCUMENT WHERE PARENT_ID=? FOR UPDATE WITH LOCK