Added locks to tag and domain creation during short URL creation

This commit is contained in:
Alejandro Celaya
2021-05-15 12:11:45 +02:00
parent 3ff4ac84c4
commit f82e103bc5
6 changed files with 46 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Repository;
use Doctrine\DBAL\LockMode;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Happyr\DoctrineSpecification\Spec;
use Shlinkio\Shlink\Core\Entity\Tag;
@@ -62,4 +63,16 @@ class TagRepository extends EntitySpecificationRepository implements TagReposito
return $result > 0;
}
public function findOneByNameWithLock(string $name): ?Tag
{
$qb = $this->createQueryBuilder('t');
$qb->where($qb->expr()->eq('t.name', ':name'))
->setParameter('name', $name)
->setMaxResults(1);
$query = $qb->getQuery()->setLockMode(LockMode::PESSIMISTIC_WRITE);
return $query->getOneOrNullResult();
}
}

View File

@@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Repository;
use Doctrine\Persistence\ObjectRepository;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepositoryInterface;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Tag\Model\TagInfo;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
@@ -19,4 +20,6 @@ interface TagRepositoryInterface extends ObjectRepository, EntitySpecificationRe
public function findTagsWithInfo(?ApiKey $apiKey = null): array;
public function tagExists(string $tag, ?ApiKey $apiKey = null): bool;
public function findOneByNameWithLock(string $name): ?Tag;
}