Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
111 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: linyulin | ||
* Date: 2019/4/13 | ||
* Time: 8:33 PM | ||
*/ | ||
|
||
namespace Im050\WeChat\Component\Database; | ||
|
||
use Im050\WeChat\Component\Console; | ||
use Medoo\Medoo; | ||
|
||
class Database extends Medoo | ||
{ | ||
|
||
private $name = 'default'; | ||
|
||
private $maxRetryTimes = 3; | ||
|
||
public function __construct(array $options, string $name) | ||
{ | ||
parent::__construct($options); | ||
$this->name = $name; | ||
} | ||
|
||
public function reconnect() | ||
{ | ||
$config = config('db.' . $this->name); | ||
$this->__construct($config, $this->name); //init. | ||
Console::log("Triggered reconnection database mechanism.", Console::DEBUG); | ||
} | ||
|
||
public function exec($query, $map = []) | ||
{ | ||
if ($this->maxRetryTimes <= 0) { | ||
throw new \PDOException("MySQL has gone away", 'HY000'); | ||
} | ||
try { | ||
$result = parent::exec($query, $map); | ||
$this->maxRetryTimes < 5 && $this->maxRetryTimes++; | ||
return $result; | ||
} catch (\PDOException $e) { | ||
if ($e->getCode() == 'HY000') { | ||
$this->reconnect(); | ||
$this->maxRetryTimes--; | ||
return $this->exec($query, $map); | ||
} | ||
throw $e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: linyulin | ||
* Date: 2019/4/22 | ||
* Time: 4:10 AM | ||
*/ | ||
|
||
namespace Im050\WeChat\Component\Database; | ||
|
||
use PDO; | ||
|
||
class DatabaseFactory | ||
{ | ||
private $dbInstances = []; | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return Database | ||
*/ | ||
public function getConnection($name = 'default') { | ||
$realName = $this->getInstanceName($name); | ||
if (array_key_exists($realName, $this->dbInstances)) { | ||
return $this->dbInstances[$realName]; | ||
} | ||
$config = config("db." . $name); | ||
!isset($config['option']) && $config['option'] = []; | ||
$config['option'] = $config['option'] + [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; | ||
$this->dbInstances[$realName] = new Database($config, $name); | ||
return $this->dbInstances[$realName]; | ||
} | ||
|
||
private function getInstanceName($name) { | ||
$pid = posix_getpid(); | ||
$realName = $name . "_" . $pid; | ||
return $realName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters