protected function lockAllDocuments($subjectsOfChange, $transaction_id, $contextAlias)
    {
        for($retry=1; $retry <= $this->retriesToGetLock; $retry++)
        {
            $originalCBDs = array();
            $lockedSubjects = array();
            foreach ($subjectsOfChange as $s)
            {
                $this->debugLog(MONGO_LOCK,
                    array(
                        'description'=>'Driver::lockAllDocuments - Attempting to get lock',
                        'transaction_id'=>$transaction_id,
                        'subject'=>$s,
                        'attempt' => $retry
                    )
                );

                $document = $this->lockSingleDocument($s, $transaction_id, $contextAlias);
                if(!empty($document)){

                    $this->debugLog(MONGO_LOCK,
                        array(
                            'description'=>'Driver::lockAllDocuments - Got the lock',
                            'transaction_id'=>$transaction_id,
                            'subject'=>$s,
                            'retry' => $retry
                        )
                    );

                    $this->getStat()->increment(MONGO_LOCK);
                    $originalCBDs[] = $document;
                    $lockedSubjects[] = $s;
                }
            }

            if(count($subjectsOfChange) == count($lockedSubjects))
            {
                //if all subjects of change locked, we are good.
                return $originalCBDs;
            }
            else
            {
                // If any subject was locked, unlock it
                if(count($lockedSubjects))
                {
                    $this->unlockAllDocuments($transaction_id);
                }
                $this->debugLog(MONGO_LOCK,
                    array(
                        'description'=>"Driver::lockAllDocuments - Unable to lock all ". count($subjectsOfChange) ."  documents, unlocked  " . count($lockedSubjects) . " locked documents",
                        'transaction_id'=>$transaction_id,
                        'documentsToLock' => implode(",", $subjectsOfChange),
                        'documentsLocked' => implode(",", $lockedSubjects),
                        'retry' => $retry
                    )
                );
                $n = mt_rand (25,40); usleep($n*1000); //do a retry
            }
        }

        $this->errorLog(MONGO_LOCK,
            array(
                'description'=>'Unable to lock all required documents. Exhausted retries',
                'retries' => $this->retriesToGetLock,
                'transaction_id'=>$transaction_id,
                'subjectsOfChange'=>implode(", ",$subjectsOfChange),
                'mongoDriverError' => $this->getLastDBError($this->getDatabase()),
            )
        );
        return NULL;
    }
protected function lockAllDocuments($subjectsOfChange, $transaction_id, $contextAlias)
    {
        for($retry=1; $retry <= $this->retriesToGetLock; $retry++)
        {
            $originalCBDs = array();
            $lockedSubjects = array();
            foreach ($subjectsOfChange as $s)
            {
                $this->debugLog(MONGO_LOCK,
                    array(
                        'description'=>'Driver::lockAllDocuments - Attempting to get lock',
                        'transaction_id'=>$transaction_id,
                        'subject'=>$s,
                        'attempt' => $retry
                    )
                );

                $document = $this->lockSingleDocument($s, $transaction_id, $contextAlias);
                if(!empty($document)){

                    $this->debugLog(MONGO_LOCK,
                        array(
                            'description'=>'Driver::lockAllDocuments - Got the lock',
                            'transaction_id'=>$transaction_id,
                            'subject'=>$s,
                            'retry' => $retry
                        )
                    );

                    $this->getStat()->increment(MONGO_LOCK);
                    $originalCBDs[] = $document;
                    $lockedSubjects[] = $s;
                }
            }

            if(count($subjectsOfChange) == count($lockedSubjects))
            {
                //if all subjects of change locked, we are good.
                return $originalCBDs;
            }
            else
            {
                // If any subject was locked, unlock it
                if(count($lockedSubjects))
                {
                    $this->unlockAllDocuments($transaction_id);
                }
                $this->debugLog(MONGO_LOCK,
                    array(
                        'description'=>"Driver::lockAllDocuments - Unable to lock all ". count($subjectsOfChange) ."  documents, unlocked  " . count($lockedSubjects) . " locked documents",
                        'transaction_id'=>$transaction_id,
                        'documentsToLock' => implode(",", $subjectsOfChange),
                        'documentsLocked' => implode(",", $lockedSubjects),
                        'retry' => $retry
                    )
                );
                $n = mt_rand (25,40); usleep($n*1000); //do a retry
            }
        }

        $this->errorLog(MONGO_LOCK,
            array(
                'description'=>'Unable to lock all required documents. Exhausted retries',
                'retries' => $this->retriesToGetLock,
                'transaction_id'=>$transaction_id,
                'subjectsOfChange'=>implode(", ",$subjectsOfChange),
                'mongoDriverError' => $this->getLastDBError($this->getDatabase()),
            )
        );
        return NULL;
    }
protected function lockAllDocuments ( $ subjectsOfChange , $ transaction_id , $ contextAlias ) { for ( $ retry = 1 ; $ retry <= $ this -> retriesToGetLock ; $ retry ++ ) { $ originalCBDs = array ( ) ; $ lockedSubjects = array ( ) ; foreach ( $ subjectsOfChange as $ s ) { $ this -> debugLog ( MONGO_LOCK , array ( 'description' => 'Driver::lockAllDocuments - Attempting to get lock' , 'transaction_id' => $ transaction_id , 'subject' => $ s , 'attempt' => $ retry ) ) ; $ document = $ this -> lockSingleDocument ( $ s , $ transaction_id , $ contextAlias ) ; if ( ! empty ( $ document ) ) { $ this -> debugLog ( MONGO_LOCK , array ( 'description' => 'Driver::lockAllDocuments - Got the lock' , 'transaction_id' => $ transaction_id , 'subject' => $ s , 'retry' => $ retry ) ) ; $ this -> getStat ( ) -> increment ( MONGO_LOCK ) ; $ originalCBDs [ ] = $ document ; $ lockedSubjects [ ] = $ s ; } } if ( count ( $ subjectsOfChange ) == count ( $ lockedSubjects ) ) { //if all subjects of change locked, we are good. return $ originalCBDs ; } else { // If any subject was locked, unlock it if ( count ( $ lockedSubjects ) ) { $ this -> unlockAllDocuments ( $ transaction_id ) ; } $ this -> debugLog ( MONGO_LOCK , array ( 'description' => "Driver::lockAllDocuments - Unable to lock all " . count ( $ subjectsOfChange ) . "  documents, unlocked  " . count ( $ lockedSubjects ) . " locked documents" , 'transaction_id' => $ transaction_id , 'documentsToLock' => implode ( "," , $ subjectsOfChange ) , 'documentsLocked' => implode ( "," , $ lockedSubjects ) , 'retry' => $ retry ) ) ; $ n = mt_rand ( 25 , 40 ) ; usleep ( $ n * 1000 ) ; //do a retry } } $ this -> errorLog ( MONGO_LOCK , array ( 'description' => 'Unable to lock all required documents. Exhausted retries' , 'retries' => $ this -> retriesToGetLock , 'transaction_id' => $ transaction_id , 'subjectsOfChange' => implode ( ", " , $ subjectsOfChange ) , 'mongoDriverError' => $ this -> getLastDBError ( $ this -> getDatabase ( ) ) , ) ) ; return NULL ; }
Attempts to lock all subjects of change in a pass, if failed unlocked locked subjects and do a retry of all again.
@param array $subjectsOfChange array of the subjects that are part of this transaction
@param string $transaction_id id for this transaction
@param string $contextAlias
@return array|null returns an array of CBDs, each CBD is the version at the time at which the lock was attained
@throws \Exception
Attempts to lock all subjects of change in a pass if failed unlocked locked subjects and do a retry of all again .