final long serverTime = getServerTime(pMongo, pSvcOptions);
final long startTime = System.currentTimeMillis();
// The doc was not there so we are going to try and insert a new doc.
if (lockDoc == null) {
final ObjectId lockId
= tryInsertNew(pMongo, pLockName, pSvcOptions, pLockOptions,serverTime, startTime);
if (lockId != null) return lockId;
}
if (lockDoc == null) lockDoc = findById(pMongo, pLockName, pSvcOptions);
// Get the state.
final LockState lockState = LockState.findByCode(lockDoc.getString(LockDef.STATE.field));
// If it is unlocked, then try and lock.
if (lockState.isUnlocked()) {
final ObjectId lockId
= tryLockingExisting( pMongo, pLockName, pSvcOptions, pLockOptions, serverTime, startTime);
if (lockId != null) return lockId;
}
final ObjectId lockId = (ObjectId)lockDoc.get(LockDef.LOCK_ID.field);
// Could not get the lock.
incrementLockAttemptCount(pMongo, pLockName, lockId, pSvcOptions);
return null;