Skip to content

Commit 89ba561

Browse files
committed
Fix UniqueConstraintViolationException while insert into oc_filecache
* fixes #6160 by not being prone to the race condition in insertIfNotExists * fixes #12228 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 Signed-off-by: Morris Jobke <hey@morrisjobke.de>
1 parent 859dd1e commit 89ba561

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

lib/private/Files/Cache/Cache.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
namespace OC\Files\Cache;
3939

40+
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
4041
use OCP\DB\QueryBuilder\IQueryBuilder;
4142
use Doctrine\DBAL\Driver\Statement;
4243
use OCP\Files\Cache\ICache;
@@ -268,12 +269,20 @@ public function insert($file, array $data) {
268269
return trim($item, "`");
269270
}, $queryParts);
270271
$values = array_combine($queryParts, $params);
271-
if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
272-
'storage',
273-
'path_hash',
274-
])
275-
) {
276-
return (int)$this->connection->lastInsertId('*PREFIX*filecache');
272+
273+
try {
274+
$builder = $this->connection->getQueryBuilder();
275+
$builder->insert('filecache');
276+
277+
foreach ($values as $column => $value) {
278+
$builder->setValue($column, $builder->createNamedParameter($value));
279+
}
280+
281+
if ($builder->execute()) {
282+
return (int)$this->connection->lastInsertId('*PREFIX*filecache');
283+
}
284+
} catch(UniqueConstraintViolationException $e) {
285+
// entry exists already
277286
}
278287

279288
// The file was created in the mean time

0 commit comments

Comments
 (0)