function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cbarry9cbarry9 

FOR UPDATE - locking

When performing a SOQL query from Apex using the FOR UPDATE option, is the locking mechanism optimistic or pessimistic? In other words, if another user attempts to write to the same record, while it is locked FOR UPDATE by another user, will they wait (optimistic), or will they be returned an error immediately (pessimistic).

 

thanks in advance,

 

Chris

Anand@SAASAnand@SAAS
FOR UPDATE AFAIK is a "pessimistic" (i.e lock before you do anything) type of locking. So another user that's trying to update the same record might receive an error.
cbarry9cbarry9

Sheldon from support helped me find the following quote in the Force.com Developer Guide:

 

"You can specifically lock a set of records in a result set by adding the keywords for update
to a SOQL query. This approach protects the integrity of your data, but can also cause other
processes to wait until the locking transaction completes, so carefully consider the use of these
options."

Ken_KoellnerKen_Koellner

Can you post a link to where you found that in the manual?  It always seems that no one of the manual contains all the detailfs.  For eample, the SOQL manual doesn't even mention FOR UPDATE in the syntax.

 

 

Dan Blackhall.ax1171Dan Blackhall.ax1171

So the 2 posts about the locking mechanism seem to be contradictory. I posted a question on the Salesforce Stack Exchange site, and got a very helpful answer from superfell:

 

SOQL Record Locking (FOR UPDATE)