Skip to content

Commit 243516d

Browse files
committed
Fix UniqueConstraintViolationException while insert into oc_file_locks
* 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 <[email protected]>
1 parent 0737a6f commit 243516d

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

lib/private/Lock/DBLockingProvider.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace OC\Lock;
2828

29+
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
2930
use OC\DB\QueryBuilder\Literal;
3031
use OCP\AppFramework\Utility\ITimeFactory;
3132
use OCP\DB\QueryBuilder\IQueryBuilder;
@@ -116,7 +117,17 @@ public function __construct(IDBConnection $connection, ILogger $logger, ITimeFac
116117

117118
protected function initLockField($path, $lock = 0) {
118119
$expire = $this->getExpireTime();
119-
return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']);
120+
121+
try {
122+
$builder = $this->connection->getQueryBuilder();
123+
return $builder->insert('file_locks')
124+
->setValue('key', $builder->createNamedParameter($path))
125+
->setValue('lock', $builder->createNamedParameter($lock))
126+
->setValue('ttl', $builder->createNamedParameter($expire))
127+
->execute();
128+
} catch(UniqueConstraintViolationException $e) {
129+
return 0;
130+
}
120131
}
121132

122133
/**

0 commit comments

Comments
 (0)