From 3348e7cb7bc10070e655bd84bc038a3a380a7397 Mon Sep 17 00:00:00 2001 From: Raymond Perez Date: Mon, 30 Jan 2017 14:54:09 -0700 Subject: [PATCH] Backporting fix of issue #9251 to 5.1 branch. Original commit by Taylor Otwell: d9e0a6a --- .../Session/DatabaseSessionHandler.php | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Session/DatabaseSessionHandler.php b/src/Illuminate/Session/DatabaseSessionHandler.php index dbc83bb777cf..87c1e7d3d00e 100644 --- a/src/Illuminate/Session/DatabaseSessionHandler.php +++ b/src/Illuminate/Session/DatabaseSessionHandler.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use SessionHandlerInterface; +use Illuminate\Database\QueryException; use Illuminate\Database\ConnectionInterface; class DatabaseSessionHandler implements SessionHandlerInterface, ExistenceAwareInterface @@ -95,16 +96,45 @@ public function read($sessionId) public function write($sessionId, $data) { if ($this->exists) { - $this->getQuery()->where('id', $sessionId)->update([ - 'payload' => base64_encode($data), 'last_activity' => time(), - ]); + $this->performUpdate($sessionId, $data); } else { - $this->getQuery()->insert([ + $this->performInsert($sessionId, $data); + } + + $this->exists = true; + } + + + /** + * Perform an insert operation on the session ID. + * + * @param string $sessionId + * @param string $data + * @return void + */ + protected function performInsert($sessionId, $data) + { + try { + return $this->getQuery()->insert([ 'id' => $sessionId, 'payload' => base64_encode($data), 'last_activity' => time(), ]); + } catch (QueryException $e) { + $this->performUpdate($sessionId, $data); } + } - $this->exists = true; + /** + * Perform an update operation on the session ID. + * + * @param string $sessionId + * @param string $data + * @return int + */ + protected function performUpdate($sessionId, $data) + { + return $this->getQuery()->where('id', $sessionId)->update([ + 'payload' => base64_encode($data), 'last_activity' => time(), + ]); } /**