Berkeley DB Reference Guide:
Locking Subsystem

PrevRefNext

Locking and non-Berkeley DB applications

The Lock subsystem is useful outside the context of Berkeley DB. It can be used to manage concurrent access to any collection of either ephemeral or persistent objects. That is, the lock region can persist across invocations of an application, so it can be used to provide long-term locking (for example, conference room scheduling).

(¶ô¼­ºê½Ã½ºÅÛÀº ¹öŬ¸®µðºñ¿Ü¿¡µµ À¯¿ëÇÏ°Ô »ç¿ëµÈ´Ù.ÀÌ°ÍÀº ¾î¶² ¿ÀºêÁ§Æ®¿¡ ´ëÇÑ µ¿½Ã¼ºÁ¦¾î¸¦ °ü¸®ÇÒ ¼ö ÀÖ´Ù.Áï ¶ô±¸¿ªÀº ¾ÖÇø®ÄÉÀÌ¼Ç È£Ãâ¿¡ ´ëÇØ Áö¼ÓÀûÀ¸·Î °ü¸®µÉ ¼ö ÀÕ´Ù. ±×¸®°í ÀÌ°ÍÀº ±ä±â°£ÀÇ ¶ôÅ·(¿¹,ȸÀÇ½Ç ½ºÄÉÁ층)À» Á¦°øÇϱâ À§ÇØ »ç¿ëµÉ ¼ö ÀÖ´Ù.)

In order to use the locking subsystem in such a general way, the applications must adhere to a convention for identifying objects and lockers. Consider a conference room scheduling problem, in which there are three conference rooms scheduled in half-hour intervals. The scheduling application must then select a way to identify each conference room/time slot combination. In this case, we could describe the objects being locked as bytestrings consisting of the conference room name, the date when it is needed, and the beginning of the appropriate half-hour slot.

(ÀϹÝÀû ¹æ½ÄÀ¸·Î ¶ô¼­ºê½Ã½ºÅÛÀ» »ç¿ëÇϱâ À§Çؼ­´Â ¾ÖÇø®ÄÉÀ̼ÇÀº ¿ÀºêÁ§Æ®¿Í ¶ô±¸ºÐÀ» À§ÇÑ ÄÁº¥¼ÇÀ» ÁöÄÑ¾ß ÇÑ´Ù.30ºÐ °£°ÝÀ¸·Î 3°³ÀÇ È¸ÀÇ½Ç ½ºÄÉÁ층 ¹®Á¦¸¦ »ý°¢ÇÒ¶§ ½ºÄÉÁ층 ¾ÖÇø®ÄÉÀ̼ÇÀº °¢ ȸÀǽÇ/ŸÀÓ ½½·Ô Á¶ÇÕÀ» ±¸º°ÇÏ´Â ¹æ¹ýÀ» ¼±ÅÃÇØ¾ß ÇÑ´Ù. ÀÌ°æ¿ì¿¡ ¿ì¸®´Â ȸÀǽÇÀ̸§,ÇÊ¿äÇÑ ³¯Â¥,ÀûÀýÇÑ 30ºÐ´ÜÀ§ÀÇ ½½·ÔÀ¸·Î ±¸¼ºµÈ ¹ÙÀÌÆ®½ºÆ®¸µÀ¸·Î ¶ôµÇ´Â ¿ÀºêÁ§Æ®¸¦ ¹¦»çÇÒ ¼ö ÀÖ´Ù.)

Lockers are 32-bit numbers, so we might choose to use the User ID of the individual running the scheduling program. To schedule half-hour slots, all the application needs to do is issue a DB_ENV->lock_get call for the appropriate locker/object pair. To schedule a longer slot, the application needs to issue a DB_ENV->lock_vec call, with one DB_ENV->lock_get operation per half-hour -- up to the total length. If the DB_ENV->lock_vec call fails, the application would have to release the parts of the time slot that were obtained.

(¶ôÄ¿´Â 32ºñÆ® ¼ýÀÚ´Ù.±×·¡¼­ ¿ì¸®´Â ½ºÄÉÁ층 ÇÁ·Î±×·¥À» ½ÇÇØÇÏ´Â »ç¿ëÀÚ ÀÇ »ç¿ëÀÚID¸¦ ¶ôÄ¿·Î »ç¿ëÇÒ ¼ö ÀÖ´Ù.30ºÐ´ÜÀ§ ½½·ÔÀ» ½ºÄÉÁ층Çϱâ À§ÇØ ¾ÖÇø®ÄÉÀ̼ÇÀº ÀûÀýÇÑ ¶ôÄ¿/¿ÀºêÁ§Æ® ½Ö¿¡ ´ëÇØ  DB_ENV->lock_get ¸¦ È£ÃâÇϱ⸸ ÇÏ¸é µÈ´Ù.´õ±ä½½·ÔÀ» ½ºÄÉÁ층ÇϱâÀ§Çؼ­´Â ¾ÖÇø®ÄÉÀ̼ÇÀº  DB_ENV->lock_vecÀ» 30ºÐ´ÜÀ§ÀÇ DB_ENV->lock_get°ú ÇÔ²² Àüü±æÀÌ¿¡ À̸¦¶§±îÁö ¹ß»ý½ÃÄÑ¾ß ÇÑ´Ù.¸¸¾à DB_ENV->lock_vec ÀÌ ½ÇÆÐÇÏ¸é ¾ÖÇø®ÄÉÀ̼ÇÀº ¾ò¾ú´ø ŸÀÔ½½·Ô ºÎºÐÀ» ¸±¸®ÁîÇØ¾ß ÇÑ´Ù.)

To cancel a reservation, the application would make the appropriate DB_ENV->lock_put calls. To reschedule a reservation, the DB_ENV->lock_get and DB_ENV->lock_put calls could all be made inside of a single DB_ENV->lock_vec call. The output of DB_ENV->lock_stat could be post-processed into a human-readable schedule of conference room use.

(ȸÀÇ½Ç ¿¹¾àÀ» Ãë¼ÒÇϱâ À§Çؼ­ ¾ÖÇø®ÄÉÀ̼ÇÀº ÀûÀýÇÑ DB_ENV->lock_put À» È£ÃâÇØ¾ß ÇÑ´Ù.¿¹¾àÀ» ´Ù½Ã ½ºÄ³Á층Çϱâ À§Çؼ­  ´ÜÀÏ DB_ENV->lock_vec ¾È¿¡¼­ DB_ENV->lock_get and DB_ENV->lock_putÀÌ È£ÃâµÉ¼ö ÀÖ´Ù.  DB_ENV->lock_stat ÀÇ Ãâ·Â°ªÀº »ç¶÷ÀÌ ÀÐÀ»¼ö ÀÖ´Â ÇüÅ·Πó¸®ÇÏ¿© º¯ÇüµÉ ¼ö ÀÖ´Ù.)


PrevRefNext

Copyright (c) 1996-2003 Sleepycat Software, Inc. - All rights reserved.