Skip to content

Commit

Permalink
Fix UniqueConstraintViolationException while insert into oc_filecache
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
MorrisJobke committed Nov 12, 2018
1 parent 859dd1e commit 89ba561
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

namespace OC\Files\Cache;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Driver\Statement;
use OCP\Files\Cache\ICache;
Expand Down Expand Up @@ -268,12 +269,20 @@ public function insert($file, array $data) {
return trim($item, "`");
}, $queryParts);
$values = array_combine($queryParts, $params);
if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
'storage',
'path_hash',
])
) {
return (int)$this->connection->lastInsertId('*PREFIX*filecache');

try {
$builder = $this->connection->getQueryBuilder();
$builder->insert('filecache');

foreach ($values as $column => $value) {
$builder->setValue($column, $builder->createNamedParameter($value));
}

if ($builder->execute()) {
return (int)$this->connection->lastInsertId('*PREFIX*filecache');
}
} catch(UniqueConstraintViolationException $e) {
// entry exists already
}

// The file was created in the mean time
Expand Down

0 comments on commit 89ba561

Please sign in to comment.