11.07.2015 Views

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

namespace, are taken out in the manager's dataset on the local machine. This occursregardless of whether the default mapping for globals is a local or a remote dataset.• Local (non-careted) locks acquired in the context of an implied namespace or throughan explicit reference to an implied namespace on the local machine, are taken out usingthe manager's dataset of the local machine. An implied namespace is a directory path orOpenVMS file specification preceded by two caret characters: "^^dir".Referencing explicit and implied namespaces is further described in Global Structure in Using<strong>Caché</strong> Multi-Dimensional Storage.Locks on Global VariablesLocking is typically used with global variables to synchronize the activities of multiple processesthat may access these variables simultaneously. Global variables differ from localvariables in that they reside on disk and are available to all processes. The potential exists,then, for two processes to write to the same global at the same time. In fact, <strong>Caché</strong> processesone update before the other, so that one update overwrites and, in effect, discards the other.Global lock names begin with a caret (^) character.To illustrate locking with global variables, consider the case in which two data entry clerksare concurrently running the same student admissions application to add records for newlyenrolled students. The records are stored in a global array named ^student. To ensure a uniquerecord for each student, the application increments the simple global variable ^index for eachstudent added. The application includes the LOCK command to ensure that each studentrecord is added at a unique location in the array, and that one student record does not overwriteanother.The relevant code in the application is shown below. In this case, the LOCK controls not theglobal array ^student but the simple global variable ^index. ^index is a scratch global that isshared by the two processes. Before a process can write a record to the array, it must lock^index and update its current value (SET ^index=^index+1). If the other process is alreadyin this section of the code, ^index will be locked and the process will have to wait until theother process releases the lock (with the argumentless LOCK command).READ !,"Last name: ",!,lname QUIT:lname="" SET lname=lname_","READ !,"First name: ",!,fname QUIT:fname="" SET fname=fname_","READ !,"Middle initial: ",!,minit QUIT:minit="" SET minit=minit_":"READ !,"Student ID Number: ",!,sid QUIT:sid=""SET rec = lname_fname_minit_sidLOCK ^indexSET ^index = ^index + 1SET ^student(^index)=recLOCKLOCK<strong>Caché</strong> <strong>ObjectScript</strong> <strong>Reference</strong> 87

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!