@@ -159,6 +159,51 @@ This component also provides two useful methods related to expiring locks:
159159``getExpiringDate() `` (which returns ``null `` or a ``\DateTimeImmutable ``
160160object) and ``isExpired() `` (which returns a boolean).
161161
162+ The Owner of The Lock
163+ ---------------------
164+
165+ Locks that are acquired for the first time are owned[1]_ by the ``Lock `` instance that acquired
166+ it. If you need to check whether the current ``Lock `` instance is (still) the owner of
167+ a lock, you can use the ``isAcquired() `` method::
168+
169+ if ($lock->isAcquired()) {
170+ // We (still) own the lock
171+ }
172+
173+ Because of the fact that some lock stores have expiring locks (as seen and explained
174+ above), it is possible for an instance to lose the lock it acquired automatically::
175+
176+ // If we cannot acquire ourselves, it means some other process is already working on it
177+ if (!$lock->acquire()) {
178+ return;
179+ }
180+
181+ $this->beginTransaction();
182+
183+ // Perform a very long process that might exceed TTL of the lock
184+
185+ if ($lock->isAcquired()) {
186+ // Still all good, no other instance has acquired the lock in the meantime, we're safe
187+ $this->commit();
188+ } else {
189+ // Bummer! Our lock has apparently exceeded TTL and another process has started in
190+ // the meantime so it's not safe for us to commit.
191+ $this->rollback();
192+ throw new \Exception('Process failed');
193+ }
194+
195+ .. caution ::
196+
197+ A common pitfall might be to use the ``isAcquired() `` method to check if
198+ a lock has already been acquired by any process. As you can see in this example
199+ you have to use ``acquire() `` for this. The ``isAcquired() `` method is used to check
200+ if the lock has been acquired by the **current process ** only!
201+
202+ .. [1 ] Technically, the true owners of the lock are the ones that share the same instance of ``Key ``,
203+ not ``Lock ``. But from a user perspective, ``Key `` is internal and you will likely only be working
204+ with the ``Lock `` instance so it's easier to think of the ``Lock `` instance as being the one that
205+ is the owner of the lock.
206+
162207 Available Stores
163208----------------
164209
0 commit comments