]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix UniqueConstraintViolationException while insert into oc_file_locks 12433/head
authorMorris Jobke <hey@morrisjobke.de>
Mon, 12 Nov 2018 14:13:10 +0000 (15:13 +0100)
committerMorris Jobke <hey@morrisjobke.de>
Tue, 13 Nov 2018 12:59:24 +0000 (13:59 +0100)
* fixes #9305 by not being prone to the race condition in insertIfNotExists
* fixes #6899 by not using a query that can result in a deadlock
* replaces the insertIfNotExists call with an insert which is wrapped into a try-catch block
* followup to #12371

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
lib/private/Lock/DBLockingProvider.php

index 6adb7488217f7033b6e467a478f1a39f80b7574b..79f2ff274cff98b691c72eab7343560cfe0878a3 100644 (file)
@@ -26,6 +26,7 @@
 
 namespace OC\Lock;
 
+use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
 use OC\DB\QueryBuilder\Literal;
 use OCP\AppFramework\Utility\ITimeFactory;
 use OCP\DB\QueryBuilder\IQueryBuilder;
@@ -133,7 +134,17 @@ class DBLockingProvider extends AbstractLockingProvider {
 
        protected function initLockField(string $path, int $lock = 0): int {
                $expire = $this->getExpireTime();
-               return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']);
+
+               try {
+                       $builder = $this->connection->getQueryBuilder();
+                       return $builder->insert('file_locks')
+                               ->setValue('key', $builder->createNamedParameter($path))
+                               ->setValue('lock', $builder->createNamedParameter($lock))
+                               ->setValue('ttl', $builder->createNamedParameter($expire))
+                               ->execute();
+               } catch(UniqueConstraintViolationException $e) {
+                       return 0;
+               }
        }
 
        /**