From 84fd81e33f9334f13d4912edf79570131dd18939 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Mon, 12 Nov 2018 17:36:35 +0100 Subject: [PATCH] Fix UniqueConstraintViolationException while insert into oc_groups * fixes race condition in insert * fixes potentiaol deadlock Signed-off-by: Morris Jobke --- lib/private/Group/Database.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/private/Group/Database.php b/lib/private/Group/Database.php index c77ae9e2ee862..c6599839a7523 100644 --- a/lib/private/Group/Database.php +++ b/lib/private/Group/Database.php @@ -41,6 +41,7 @@ namespace OC\Group; +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Group\Backend\ABackend; use OCP\Group\Backend\IAddToGroupBackend; @@ -97,10 +98,15 @@ private function fixDI() { public function createGroup(string $gid): bool { $this->fixDI(); - // Add group - $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [ - 'gid' => $gid, - ]); + try { + // Add group + $builder = $this->dbConn->getQueryBuilder(); + $result = $builder->insert('groups') + ->setValue('gid', $builder->createNamedParameter($gid)) + ->execute(); + } catch(UniqueConstraintViolationException $e) { + $result = 0; + } // Add to cache $this->groupCache[$gid] = $gid;