Skip to content

Commit

Permalink
connection: ping() doesn't change the connection state (BC break!)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Jul 22, 2017
1 parent dbbc310 commit e5554d2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
8 changes: 2 additions & 6 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,10 @@ public function rollbackSavepoint(string $name)
/** @inheritdoc */
public function ping(): bool
{
try {
$this->connected || $this->connect();
return $this->driver->ping();

} catch (DriverException $e) {
$this->reconnect();
if (!$this->connected) {
return false;
}
return $this->driver->ping();
}


Expand Down
7 changes: 6 additions & 1 deletion src/Drivers/Sqlsrv/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ public function getServerVersion(): string

public function ping(): bool
{
return sqlsrv_begin_transaction($this->connection);
try {
$this->query('SELECT 1');
return true;
} catch (DriverException $e) {
return false;
}
}


Expand Down
6 changes: 5 additions & 1 deletion src/IConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ public function rollbackSavepoint(string $name);


/**
* Pings a database connection and tries to reconnect it if it is broken.
* Pings a database connection and returns true if the connection is alive.
* @example
* if (!$connection->ping()) {
* $connection->reconnect();
* }
* @return bool
*/
public function ping(): bool;
Expand Down
7 changes: 4 additions & 3 deletions tests/cases/integration/connection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class ConnectionTest extends IntegrationTestCase
public function testPing()
{
Assert::false($this->connection->getDriver()->isConnected());
$this->connection->ping();
Assert::false($this->connection->ping());
Assert::false($this->connection->getDriver()->isConnected());
$this->connection->connect();
Assert::true($this->connection->getDriver()->isConnected());
Assert::true($this->connection->ping());
}


Expand All @@ -41,8 +44,6 @@ class ConnectionTest extends IntegrationTestCase
Assert::same([
'connect',
'disconnect',
'connect',
'disconnect',
], $log);
}

Expand Down

0 comments on commit e5554d2

Please sign in to comment.