Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Reconnect on lost DB connection #307

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Adapter/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ protected function createDriver($parameters)
$driver = new Driver\Oci8\Oci8($parameters);
break;
case 'pgsql':
$driver = new Driver\Pgsql\Pgsql($parameters);
$driver = new Driver\Pgsql\Pgsql($parameters, null, null, $options);
break;
case 'ibmdb2':
$driver = new Driver\IbmDb2\IbmDb2($parameters);
Expand Down
13 changes: 12 additions & 1 deletion src/Adapter/Driver/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Zend\Db\Adapter\Driver;

use Zend\Db\Adapter\Driver\Mysqli\Statement;

interface DriverInterface
{
const PARAMETERIZATION_POSITIONAL = 'positional';
Expand Down Expand Up @@ -65,7 +67,7 @@ public function getPrepareType();
* Format parameter name
*
* @param string $name
* @param mixed $type
* @param mixed $type
* @return string
*/
public function formatParameterName($name, $type = null);
Expand All @@ -76,4 +78,13 @@ public function formatParameterName($name, $type = null);
* @return mixed
*/
public function getLastGeneratedValue();

/**
* Check current connection to DB.
* If connection lost try reconnect.
*
* @param StatementInterface|null $statement
* @return $this
*/
public function checkConnection(StatementInterface $statement = null);
}
11 changes: 11 additions & 0 deletions src/Adapter/Driver/IbmDb2/IbmDb2.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Db\Adapter\Driver\IbmDb2;

use Zend\Db\Adapter\Driver\DriverInterface;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Exception;
use Zend\Db\Adapter\Profiler;

Expand Down Expand Up @@ -211,4 +212,14 @@ public function getLastGeneratedValue()
{
return $this->connection->getLastGeneratedValue();
}

/**
* @inheritdoc
* @param StatementInterface|null $statement
* @return $this
*/
public function checkConnection(StatementInterface $statement = null)
{
return $this;
}
}
2 changes: 2 additions & 0 deletions src/Adapter/Driver/Mysqli/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ public function disconnect()
$this->resource->close();
}
$this->resource = null;

return $this;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/Adapter/Driver/Mysqli/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use mysqli_stmt;
use Zend\Db\Adapter\Driver\DriverInterface;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Exception;
use Zend\Db\Adapter\Profiler;

Expand Down Expand Up @@ -61,6 +62,7 @@ public function __construct(
$connection = new Connection($connection);
}

$this->options = array_merge($this->options, $options);
$options = array_intersect_key(array_merge($this->options, $options), $this->options);

$this->registerConnection($connection);
Expand Down Expand Up @@ -259,4 +261,39 @@ public function getLastGeneratedValue()
{
return $this->getConnection()->getLastGeneratedValue();
}

/**
* Check connection if not exists -> try to reconnect.
* Depends on configuration value `reconnect_tries`
*
* @param StatementInterface|null $statement
* @return $this
*/
public function checkConnection(StatementInterface $statement = null)
{

$reconnectTries = array_key_exists('reconnect_tries', $this->options)
? $this->options['reconnect_tries'] : 0;

/**
* @var \mysqli $mysqli
*/
$mysqli = $this->connection->getResource();

for ($i = 0; $i < $reconnectTries; ++$i) {
if ($mysqli->ping()) {
if ($statement instanceof Statement) {
$statement->initialize($mysqli);
}

return $this;
}

$mysqli = $this->connection
->disconnect()
->connect()
->getResource();
}
return $this;
}
}
1 change: 1 addition & 0 deletions src/Adapter/Driver/Mysqli/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public function prepare($sql = null)
}

$sql = ($sql) ?: $this->sql;
$this->driver->checkConnection($this);

$this->resource = $this->mysqli->prepare($sql);
if (! $this->resource instanceof \mysqli_stmt) {
Expand Down
11 changes: 11 additions & 0 deletions src/Adapter/Driver/Oci8/Oci8.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Db\Adapter\Driver\Oci8;

use Zend\Db\Adapter\Driver\DriverInterface;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Exception;
use Zend\Db\Adapter\Profiler;
use Zend\Db\Adapter\Driver\Feature\AbstractFeature;
Expand Down Expand Up @@ -299,4 +300,14 @@ public function getLastGeneratedValue()
{
return $this->getConnection()->getLastGeneratedValue();
}

/**
* @inheritdoc
* @param StatementInterface|null $statement
* @return $this
*/
public function checkConnection(StatementInterface $statement = null)
{
return $this;
}
}
11 changes: 11 additions & 0 deletions src/Adapter/Driver/Pdo/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Zend\Db\Adapter\Driver\DriverInterface;
use Zend\Db\Adapter\Driver\Feature\AbstractFeature;
use Zend\Db\Adapter\Driver\Feature\DriverFeatureInterface;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Exception;
use Zend\Db\Adapter\Profiler;

Expand Down Expand Up @@ -328,4 +329,14 @@ public function getLastGeneratedValue($name = null)
{
return $this->connection->getLastGeneratedValue($name);
}

/**
* @inheritdoc
* @param StatementInterface|null $statement
* @return $this
*/
public function checkConnection(StatementInterface $statement = null)
{
return $this;
}
}
40 changes: 39 additions & 1 deletion src/Adapter/Driver/Pgsql/Pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Db\Adapter\Driver\Pgsql;

use Zend\Db\Adapter\Driver\DriverInterface;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Exception;
use Zend\Db\Adapter\Profiler;

Expand Down Expand Up @@ -54,11 +55,12 @@ public function __construct(
$connection,
Statement $statementPrototype = null,
Result $resultPrototype = null,
$options = null
$options = []
) {
if (! $connection instanceof Connection) {
$connection = new Connection($connection);
}
$this->options = array_merge($this->options, $options);

$this->registerConnection($connection);
$this->registerStatementPrototype(($statementPrototype) ?: new Statement());
Expand Down Expand Up @@ -234,4 +236,40 @@ public function getLastGeneratedValue($name = null)
{
return $this->connection->getLastGeneratedValue($name);
}

/**
* @inheritdoc
* @param StatementInterface|null $statement
* @return $this
*/
public function checkConnection(StatementInterface $statement = null)
{

$reconnectTries = array_key_exists('reconnect_tries', $this->options)
? $this->options['reconnect_tries'] : 0;

$pg_sql = $this->connection->getResource();

for ($i = 0; $i < $reconnectTries; ++$i) {
if (pg_connection_status($pg_sql) == PGSQL_CONNECTION_OK) {
if ($statement instanceof Statement) {
$statement
->initialize($pg_sql)
->prepare();
}

return $this;
}

try {
$pg_sql = $this->connection
->disconnect()
->connect()
->getResource();
} catch (Exception\RuntimeException $e) {
print $e->getMessage();
}
}
return $this;
}
}
4 changes: 3 additions & 1 deletion src/Adapter/Driver/Pgsql/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getProfiler()
* Initialize
*
* @param resource $pgsql
* @return void
* @return $this
* @throws Exception\RuntimeException for invalid or missing postgresql connection
*/
public function initialize($pgsql)
Expand All @@ -101,6 +101,7 @@ public function initialize($pgsql)
));
}
$this->pgsql = $pgsql;
return $this;
}

/**
Expand Down Expand Up @@ -202,6 +203,7 @@ public function execute($parameters = null)
if (! $this->isPrepared()) {
$this->prepare();
}
$this->driver->checkConnection($this);

/** START Standard ParameterContainer Merging Block */
if (! $this->parameterContainer instanceof ParameterContainer) {
Expand Down
11 changes: 11 additions & 0 deletions src/Adapter/Driver/Sqlsrv/Sqlsrv.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Db\Adapter\Driver\Sqlsrv;

use Zend\Db\Adapter\Driver\DriverInterface;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Exception;
use Zend\Db\Adapter\Profiler;

Expand Down Expand Up @@ -212,4 +213,14 @@ public function getLastGeneratedValue()
{
return $this->getConnection()->getLastGeneratedValue();
}

/**
* @inheritdoc
* @param StatementInterface|null $statement
* @return $this
*/
public function checkConnection(StatementInterface $statement = null)
{
return $this;
}
}