Well I was trying to Implement Record Locking apex but Salesforce Document doesn't seem to help much. Even tried searching stackexchange there was a old question but the answer form StackExchange and details from document seems to be conflicting.

 - **Salesforce Document Link :** http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_locking_statements.htm


 - **StackExchange Question Link :** http://salesforce.stackexchange.com/questions/9205/soql-record-locking-for-update

According to Salesforce Doc whenever a second transaction tries to lock a record that is already locked it will throw an "QueryException" whereas from the Stackexchange it seems like the second thread will actually wait.

> **Q1:** I am bit confused about what will happen ? Will it fail(throw exception) or will it wait for 4-5 seconds and then if it doesn't get
> access it will throw an exception ?

In production we are facing a issue where second thread seems to get access to the record even though we have put a lock, second transaction seems to get access, *may be after sometime but doesn't seems to throw any exception immediately*. For example we have a method

    public static void updateMyAccount(){
	Account acc = [SELECT Id FROM Account WHERE isValid = TRUE AND Id='SOME ID' FOR UPDATE];
    //some time consuming process...callouts etc here
     acc.isValid__c = false;
     update acc;
  
}

**Scenario:**(assuming the system waits) 

 - Lets assume **Thread 1** calls the above method and acquires a **lock** on the RECORD.
 - Now while **Transaction 1** is processing the record another **Thread Transaction 2** tries to access the record and waits for the lock to release
 - While the **transaction 2** is waiting thread one changes "isValid__c" to false and the record no more matches the query condition.

> **Q2** : In the above case will the second thread get access to the record ? or the query will not return any record ?



> **Q3** : When does the lock gets  released ? the Salesforce doc doesn't seems to provide much insight into it. does the lock get
> released after a DML ? or it waits for full transaction to complete ?