swoole-framwork/core/lib/PDOPool.php
2024-06-26 17:13:45 +08:00

46 lines
1.3 KiB
PHP

<?php
namespace Core\lib;
use function DI\get;
class PDOPool extends DBPool
{
public function __construct($min = 5, $max = 10)
{
global $GLOBAL_CONFIGS;
$poolconfig = $GLOBAL_CONFIGS["dbpool"]["default"];
parent::__construct($poolconfig['min'], $poolconfig['max']);
//\Swoole\Runtime::enableCoroutine(true);
}
protected function newDB()
{
global $GLOBAL_CONFIGS;
$default = $GLOBAL_CONFIGS["db"]["default"];
{
$driver = $default["driver"];
$host = $default["host"];
$dbname = $default['database'];
$username = $default["username"];
$password = $default["password"];
$dsn = "$driver:host=$host;dbname=$dbname";
}
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
];
$options1 = array(
\PDO::ATTR_PERSISTENT => true
);
try {
$pdo = new \PDO($dsn, $username, $password);
return $pdo;
}catch (\Exception $e) {
echo $e->getMessage() . PHP_EOL;
throw new \Exception('数据库链接失败');
}
}
}