This commit is contained in:
2020-09-18 16:42:28 +08:00
parent 5fbecde9a5
commit b5d3f5fe7d
+21 -6
View File
@@ -5,6 +5,7 @@ namespace Snowflake\Pool;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use PDO; use PDO;
use Exception; use Exception;
use Snowflake\Abstracts\Config;
use Swoole\Coroutine; use Swoole\Coroutine;
use Snowflake\Abstracts\Pool; use Snowflake\Abstracts\Pool;
use Swoole\Timer; use Swoole\Timer;
@@ -238,7 +239,7 @@ class Connection extends Pool
private function nowClient($coroutineName, $config) private function nowClient($coroutineName, $config)
{ {
$this->success('create db client -> ' . $config['cds'] . ':' . $this->hasCreate[$coroutineName] . ':' . $this->size($coroutineName)); $this->success('create db client -> ' . $config['cds'] . ':' . $this->hasCreate[$coroutineName] . ':' . $this->size($coroutineName));
$client = $this->createConnect($coroutineName, $config['cds'], $config['username'], $config['password']); $client = $this->createConnect($coroutineName, ...$this->parseConfig($config));
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) { if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
$number > 0 && $client->beginTransaction(); $number > 0 && $client->beginTransaction();
} }
@@ -246,6 +247,16 @@ class Connection extends Pool
} }
/**
* @param $config
* @return array
*/
private function parseConfig($config)
{
return [$config['cds'], $config['username'], $config['password'], $config['charset'] ?? 'utf8mb4'];
}
/** /**
* @param $coroutineName * @param $coroutineName
* @param $isMaster * @param $isMaster
@@ -343,10 +354,11 @@ class Connection extends Pool
* @param $cds * @param $cds
* @param $username * @param $username
* @param $password * @param $password
* @param string $charset
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function createConnect($coroutineName, $cds, $username, $password) public function createConnect($coroutineName, $cds, $username, $password, $charset = 'utf8mb4')
{ {
try { try {
$link = new PDO($cds, $username, $password, [ $link = new PDO($cds, $username, $password, [
@@ -358,16 +370,19 @@ class Connection extends Pool
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$link->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); $link->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$link->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING); $link->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
if (!empty($charset)) {
$link->query('SET NAMES ' . $charset);
}
$this->incr($coroutineName); $this->incr($coroutineName);
return $link; return $link;
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
if ($exception->getCode() !== 2006) { $this->error($cds . ' -> ' . $exception->getMessage());
if ($exception->getCode() === 2006) {
return $this->createConnect($coroutineName, $cds, $username, $password, $charset);
}
$this->addError($cds . ' -> ' . $exception->getMessage()); $this->addError($cds . ' -> ' . $exception->getMessage());
throw new Exception($exception->getMessage()); throw new Exception($exception->getMessage());
} }
$this->error($cds . ' -> ' . $exception->getMessage());
return $this->createConnect($coroutineName, $cds, $username, $password);
}
} }
/** /**