Scope: managed applications, ordinary applications.
1.1. Before you change an existing infobase object from code in 1C:Enterprise language, lock it (set either "data lock" or "object lock"). In this case, you make sure that the object is not locked by other objects and prevent it from being changed in other user sessions or by different object instances in the same session.
Otherwise, if an object is not locked for editing upon changing or writing from 1C:Enterprise language, users might not be able to save their changes if the same data is changed in another session.
Data lock from editing does not restrict writing of locked data in other user sessions or in other object instances in the same session. It only prevents multiple objects from locking the same data. Unlike transaction data locks, a pessimistic data lock for editing provides parallel user operations with 1C:Enterprise infobase objects (catalog items, documents, and so on). For more information on locking data, see 1C:Enterprise 8 platform documentation.
1.2. To lock data from editing from 1C:Enterprise language, call the Lock object method or the LockDataForEdit global context method.
Example 1. It is required to lock an object and if possible modify data. Otherwise, notify users of the operation cancellation by displaying the following message:
"Cannot lock the record. The operation (change, deletion, or record lock) failed. Object lock error. The object is already locked: computer: <computer name>, user: <username>, session: <session ID>, started: <date and time>, application: <client application type>."
FileObject= FileData.Ref.GetObject();
// Preventing the object from being changed by other modes
// or other users; in case of lock,
// display an exception message to users.
FileObject.Lock();
// Changing and writing the object
FileObject.Edits = Catalogs.Users.EmptyRef();
FileObject.Write();
Similarly, use the LockDataForEdit global context method:
FileObject= FileData.Ref.GetObject();
// Preventing the object from being changed by other modes
// or other users; in case of lock,
// display an exception message to users.
LockDataForEdit(FileData.Ref);
// Changing and writing the object
FileObject.Edits = Catalogs.Users.EmptyRef();
FileObject.Write();
Example 2. Skip object processing if it is locked from editing. Upon the next procedure call (for example, from a scheduled or background job), another attempt to change the object is made.
Object= CurrentVersion.GetObject();
// Preventing the object from being changed by other modes
// or other users
SetFullDescription = True;
Try
FileObject.Lock();
Exception
// In case of lock, do not change the object
SetFullDescription = False;
// Writing a warning to the event log
WriteLogEvent(NStr("en = "Background update of file names'", Metadata.DefaultLanguage.LanguageCode),
EventLogLevel.Warning,, FileObject, ErrorDescription());
EndTry;
// Skipping the object processing if it is locked.
If SetFullDescription Then
Object.FullDescr = FullDescr;
Object.Write();
EndIf;
1.3. When editing data in forms, 1C:Enterprise platform automatically sets locks for the objects specified as main form attributes.
2. We do not recommended that you check object locks in the following cases:
- Upon performing operations with a higher priority than interactive user actions. For example, data import upon data exchange.
- Upon operations performed in the exclusive mode only. For example, in procedures of update and initial data filling of the infobase.