diff --git a/src/ReconnectingPDO.php b/src/ReconnectingPDO.php index 9383b4a..4620688 100644 --- a/src/ReconnectingPDO.php +++ b/src/ReconnectingPDO.php @@ -40,7 +40,7 @@ class ReconnectingPDO /** * @var PDO */ - protected $db; + protected $connection; /** * @var string DSN @@ -110,42 +110,44 @@ public function __call($method, $arguments) */ protected function call($method, $arguments) { - if (!($this->db instanceof PDO)) { + if (!($this->connection instanceof PDO)) { throw new ReconnectingPDOException('No PDO connection is set'); } try { - $returnValue = call_user_func_array([$this->db, $method], $arguments); + $returnValue = call_user_func_array([$this->connection, $method], + $arguments); } catch (\PDOException $ex) { if (!stristr($ex->getMessage(), "server has gone away") || $ex->getCode() != 'HY000') { throw $ex; } - if ($this->reconnectCounter < $this->maxReconnection) { - $this->reconnectDb(); - $returnValue = $this->call($method, $arguments); // Retry - $this->resetCounter(); - } else { + if ($this->reconnectCounter >= $this->maxReconnection) { throw new ExceededMaxReconnectionException('ReconnectingPDO has exceeded max reconnection limit', $ex->getCode(), $ex); } + $this->reconnectDb(); + $returnValue = $this->call($method, $arguments); // Retry + $this->resetCounter(); } if ($returnValue instanceof \PDOStatement) { - return new ReconnectingPDOStatement($returnValue, $this, $method === 'query'); + return new ReconnectingPDOStatement($returnValue, $this, + $method === 'query'); } return $returnValue; } protected function reconnectDb() { - unset($this->db); + unset($this->connection); $this->connectDb(); $this->reconnectCounter++; } protected function connectDb() { - $this->db = new PDO($this->dsn, $this->username, $this->passwd, + $this->connection = new PDO($this->dsn, $this->username, $this->passwd, $this->options); - $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->connection->setAttribute(PDO::ATTR_ERRMODE, + PDO::ERRMODE_EXCEPTION); } /** @@ -173,16 +175,13 @@ public function setMaxReconnection($max) * @param array $parameters * @param bool $autoconnect */ - public function setConnectionParameters($parameters, $autoconnect = false) + public function setConnectionParameters($parameters) { foreach ($parameters as $key => $param) { if (property_exists($this, $key)) { $this->$key = $param; } } - if ($autoconnect) { - $this->connectDb(); - } } public function setPDO(PDO $pdoObject) @@ -190,7 +189,7 @@ public function setPDO(PDO $pdoObject) if (!$this->connectionParametersAreSet()) { throw new ConnectionParametersMissingException(); } - $this->db = $pdoObject; + $this->connection = $pdoObject; } protected function connectionParametersAreSet() @@ -200,4 +199,5 @@ protected function connectionParametersAreSet() } return false; } + } diff --git a/test/ReconnectingPDOTest.php b/test/ReconnectingPDOTest.php index e159fd6..9e35f0a 100644 --- a/test/ReconnectingPDOTest.php +++ b/test/ReconnectingPDOTest.php @@ -37,7 +37,7 @@ public function testConstruct() $this->assertAttributeEquals('test', 'passwd', $rpdo); $this->assertAttributeEquals([], 'options', $rpdo); $this->assertAttributeEquals(3, 'maxReconnection', $rpdo); - $this->assertAttributeInstanceOf(\PDO::class, 'db', $rpdo); + $this->assertAttributeInstanceOf(\PDO::class, 'connection', $rpdo); } public function testSetters() @@ -73,15 +73,6 @@ public function testSetters() } $this->assertNull($exception); - unset($rpdo); - $rpdo = new ReconnectingPDO(); - $rpdo->setConnectionParameters([ - 'dsn' => $dsn, - 'passwd' => $passwd, - 'username' => $username - ], true); - - $this->assertAttributeInstanceOf(\PDO::class, 'db', $rpdo); } public function testReconnection() @@ -100,7 +91,7 @@ public function testReconnection() $rpdo = new ReconnectingPDO($dsn, $username, $passwd); $rpdo->setPDO($mockPDO); - $this->assertAttributeEquals($mockPDO, 'db', $rpdo); + $this->assertAttributeEquals($mockPDO, 'connection', $rpdo); $exception = null; try {