From 13e622f9e75f2bc6cce2f3bb930ce032d6f88dd1 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 11 Nov 2014 19:24:05 +0100 Subject: [PATCH] Fixed race condition in entity preparation --- src/Dao/AbstractDao.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Dao/AbstractDao.php b/src/Dao/AbstractDao.php index f46aeec2..6ee1dce1 100644 --- a/src/Dao/AbstractDao.php +++ b/src/Dao/AbstractDao.php @@ -131,14 +131,15 @@ abstract class AbstractDao implements ICrudDao, IBatchDao public function create(Entity $entity) { - $sequencerQuery = $this->pdo->from('sequencer')->where('tableName', $this->tableName); - $lastUsedId = intval(iterator_to_array($sequencerQuery)[0]['lastUsedId']); - $lastUsedId ++; + $sql = 'UPDATE sequencer SET lastUsedId = (@lastUsedId := (lastUsedId + 1)) WHERE tableName = :tableName'; + $query = $this->pdo->prepare($sql); + $query->bindValue(':tableName', $this->tableName); + $query->execute(); + $lastUsedId = $this->pdo->query('SELECT @lastUsedId')->fetchColumn(); $entity->setId($lastUsedId); $arrayEntity = $this->entityConverter->toArray($entity); $this->pdo->insertInto($this->tableName)->values($arrayEntity)->execute(); - $this->pdo->update('sequencer')->set(['lastUsedId' => $lastUsedId])->where('tableName', $this->tableName)->execute(); return $entity; }